graypy-0.2.11/0000775000175000017500000000000012403570273013711 5ustar severbseverb00000000000000graypy-0.2.11/graypy.egg-info/0000775000175000017500000000000012403570273016716 5ustar severbseverb00000000000000graypy-0.2.11/graypy.egg-info/SOURCES.txt0000664000175000017500000000042112403570273020577 0ustar severbseverb00000000000000LICENSE MANIFEST.in README.rst setup.py graypy/__init__.py graypy/handler.py graypy/rabbitmq.py graypy.egg-info/PKG-INFO graypy.egg-info/SOURCES.txt graypy.egg-info/dependency_links.txt graypy.egg-info/not-zip-safe graypy.egg-info/requires.txt graypy.egg-info/top_level.txtgraypy-0.2.11/graypy.egg-info/PKG-INFO0000664000175000017500000001707312403570273020023 0ustar severbseverb00000000000000Metadata-Version: 1.0 Name: graypy Version: 0.2.11 Summary: Python logging handler that sends messages in GELF (Graylog Extended Log Format). Home-page: https://github.com/severb/graypy Author: Sever Banesiu Author-email: banesiu.sever@gmail.com License: BSD License Description: Installing ========== Using easy_install:: easy_install graypy Install with requirements for ``GELFRabbitHandler``:: easy_install graypy[amqp] Usage ===== Messages are sent to Graylog2 using a custom handler for the builtin logging library in GELF format:: import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler) my_logger.debug('Hello Graylog2.') Alternately, use ``GELFRabbitHandler`` to send messages to RabbitMQ and configure your Graylog2 server to consume messages via AMQP. This prevents log messages from being lost due to dropped UDP packets (``GELFHandler`` sends messages to Graylog2 using UDP). You will need to configure RabbitMQ with a 'gelf_log' queue and bind it to the 'logging.gelf' exchange so messages are properly routed to a queue that can be consumed by Graylog2 (the queue and exchange names may be customized to your liking):: import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFRabbitHandler('amqp://guest:guest@localhost/%2F', 'logging.gelf') my_logger.addHandler(handler) my_logger.debug('Hello Graylog2.') Tracebacks are added as full messages:: import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler) try: puff_the_magic_dragon() except NameError: my_logger.debug('No dragons here.', exc_info=1) Configuration parameters ======================== GELFHandler: * **host** - the host of the graylog server. * **port** - the port of the graylog server (default 12201). * **chunk_size** - message chunk size. messages larger than this size will be sent to graylog in multiple chunks (default `1420`). * **debugging_fields** - send debug fields if true (the default). * **extra_fields** - send extra fields on the log record to graylog if true (the default). * **fqdn** - use fully qualified domain name of localhost as source host (socket.getfqdn()). * **localname** - use specified hostname as source host. * **facility** - replace facility with specified value. if specified, record.name will be passed as *logger* parameter. GELFRabbitHandler: * **url** - RabbitMQ URL (ex: amqp://guest:guest@localhost:5672/%2F). * **exchange** - RabbitMQ exchange. Default 'logging.gelf'. A queue binding must be defined on the server to prevent log messages from being dropped. * **debugging_fields** - send debug fields if true (the default). * **extra_fields** - send extra fields on the log record to graylog if true (the default). * **fqdn** - use fully qualified domain name of localhost as source host - socket.getfqdn(). * **exchange_type** - RabbitMQ exchange type (default `fanout`). * **localname** - use specified hostname as source host. * **facility** - replace facility with specified value. if specified, record.name will be passed as `logger` parameter. Using with Django ================= It's easy to integrate ``graypy`` with Django's logging settings. Just add a new handler in your ``settings.py`` like this:: LOGGING = { ... 'handlers': { 'graypy': { 'level': 'WARNING', 'class': 'graypy.GELFHandler', 'host': 'localhost', 'port': 12201, }, }, 'loggers': { 'django.request': { 'handlers': ['graypy'], 'level': 'ERROR', 'propagate': True, }, }, } Custom fields ============= A number of custom fields are automatically added if available: * function * pid * process_name * thread_name You can disable these additional fields if you don't want them by adding an argument to the handler:: handler = graypy.GELFHandler('localhost', 12201, debugging_fields=False) graypy also supports additional fields to be included in the messages sent to Graylog2. This can be done by using Python's LoggerAdapter_ and Filter_. In general, LoggerAdapter makes it easy to add static information to your log messages and Filters give you more flexibility, for example to add additional information based on the message that is being logged. Example using LoggerAdapter_:: import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler) my_adapter = logging.LoggerAdapter(logging.getLogger('test_logger'), { 'username': 'John' }) my_adapter.debug('Hello Graylog2 from John.') Example using Filter_:: import logging import graypy class UsernameFilter(logging.Filter): def __init__(self): # In an actual use case would dynamically get this (e.g. from memcache) self.username = "John" def filter(self, record): record.username = self.username return True my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler) my_logger.addFilter(UsernameFilter()) my_logger.debug('Hello Graylog2 from John.') Contributors: * Sever Banesiu * Daniel Miller .. _LoggerAdapter: http://docs.python.org/howto/logging-cookbook.html#using-loggeradapters-to-impart-contextual-information .. _Filter: http://docs.python.org/howto/logging-cookbook.html#using-filters-to-impart-contextual-information .. image:: https://d2weczhvl823v0.cloudfront.net/severb/graypy/trend.png :alt: Bitdeli badge :target: https://bitdeli.com/free Keywords: logging gelf graylog2 graylog udp amqp Platform: UNKNOWN graypy-0.2.11/graypy.egg-info/dependency_links.txt0000664000175000017500000000000112403570273022764 0ustar severbseverb00000000000000 graypy-0.2.11/graypy.egg-info/top_level.txt0000664000175000017500000000000712403570273021445 0ustar severbseverb00000000000000graypy graypy-0.2.11/graypy.egg-info/requires.txt0000664000175000017500000000002712403570273021315 0ustar severbseverb00000000000000 [amqp] amqplib==1.0.2graypy-0.2.11/graypy.egg-info/not-zip-safe0000664000175000017500000000000112403570135021141 0ustar severbseverb00000000000000 graypy-0.2.11/PKG-INFO0000664000175000017500000001707312403570273015016 0ustar severbseverb00000000000000Metadata-Version: 1.0 Name: graypy Version: 0.2.11 Summary: Python logging handler that sends messages in GELF (Graylog Extended Log Format). Home-page: https://github.com/severb/graypy Author: Sever Banesiu Author-email: banesiu.sever@gmail.com License: BSD License Description: Installing ========== Using easy_install:: easy_install graypy Install with requirements for ``GELFRabbitHandler``:: easy_install graypy[amqp] Usage ===== Messages are sent to Graylog2 using a custom handler for the builtin logging library in GELF format:: import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler) my_logger.debug('Hello Graylog2.') Alternately, use ``GELFRabbitHandler`` to send messages to RabbitMQ and configure your Graylog2 server to consume messages via AMQP. This prevents log messages from being lost due to dropped UDP packets (``GELFHandler`` sends messages to Graylog2 using UDP). You will need to configure RabbitMQ with a 'gelf_log' queue and bind it to the 'logging.gelf' exchange so messages are properly routed to a queue that can be consumed by Graylog2 (the queue and exchange names may be customized to your liking):: import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFRabbitHandler('amqp://guest:guest@localhost/%2F', 'logging.gelf') my_logger.addHandler(handler) my_logger.debug('Hello Graylog2.') Tracebacks are added as full messages:: import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler) try: puff_the_magic_dragon() except NameError: my_logger.debug('No dragons here.', exc_info=1) Configuration parameters ======================== GELFHandler: * **host** - the host of the graylog server. * **port** - the port of the graylog server (default 12201). * **chunk_size** - message chunk size. messages larger than this size will be sent to graylog in multiple chunks (default `1420`). * **debugging_fields** - send debug fields if true (the default). * **extra_fields** - send extra fields on the log record to graylog if true (the default). * **fqdn** - use fully qualified domain name of localhost as source host (socket.getfqdn()). * **localname** - use specified hostname as source host. * **facility** - replace facility with specified value. if specified, record.name will be passed as *logger* parameter. GELFRabbitHandler: * **url** - RabbitMQ URL (ex: amqp://guest:guest@localhost:5672/%2F). * **exchange** - RabbitMQ exchange. Default 'logging.gelf'. A queue binding must be defined on the server to prevent log messages from being dropped. * **debugging_fields** - send debug fields if true (the default). * **extra_fields** - send extra fields on the log record to graylog if true (the default). * **fqdn** - use fully qualified domain name of localhost as source host - socket.getfqdn(). * **exchange_type** - RabbitMQ exchange type (default `fanout`). * **localname** - use specified hostname as source host. * **facility** - replace facility with specified value. if specified, record.name will be passed as `logger` parameter. Using with Django ================= It's easy to integrate ``graypy`` with Django's logging settings. Just add a new handler in your ``settings.py`` like this:: LOGGING = { ... 'handlers': { 'graypy': { 'level': 'WARNING', 'class': 'graypy.GELFHandler', 'host': 'localhost', 'port': 12201, }, }, 'loggers': { 'django.request': { 'handlers': ['graypy'], 'level': 'ERROR', 'propagate': True, }, }, } Custom fields ============= A number of custom fields are automatically added if available: * function * pid * process_name * thread_name You can disable these additional fields if you don't want them by adding an argument to the handler:: handler = graypy.GELFHandler('localhost', 12201, debugging_fields=False) graypy also supports additional fields to be included in the messages sent to Graylog2. This can be done by using Python's LoggerAdapter_ and Filter_. In general, LoggerAdapter makes it easy to add static information to your log messages and Filters give you more flexibility, for example to add additional information based on the message that is being logged. Example using LoggerAdapter_:: import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler) my_adapter = logging.LoggerAdapter(logging.getLogger('test_logger'), { 'username': 'John' }) my_adapter.debug('Hello Graylog2 from John.') Example using Filter_:: import logging import graypy class UsernameFilter(logging.Filter): def __init__(self): # In an actual use case would dynamically get this (e.g. from memcache) self.username = "John" def filter(self, record): record.username = self.username return True my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler) my_logger.addFilter(UsernameFilter()) my_logger.debug('Hello Graylog2 from John.') Contributors: * Sever Banesiu * Daniel Miller .. _LoggerAdapter: http://docs.python.org/howto/logging-cookbook.html#using-loggeradapters-to-impart-contextual-information .. _Filter: http://docs.python.org/howto/logging-cookbook.html#using-filters-to-impart-contextual-information .. image:: https://d2weczhvl823v0.cloudfront.net/severb/graypy/trend.png :alt: Bitdeli badge :target: https://bitdeli.com/free Keywords: logging gelf graylog2 graylog udp amqp Platform: UNKNOWN graypy-0.2.11/LICENSE0000664000175000017500000000274412403570051014717 0ustar severbseverb00000000000000Copyright (c) 2011, Sever Băneşiu All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. graypy-0.2.11/setup.py0000775000175000017500000000112512403570113015416 0ustar severbseverb00000000000000#!/usr/bin/env python from setuptools import setup, find_packages setup( name='graypy', version='0.2.11', description="Python logging handler that sends messages in GELF (Graylog Extended Log Format).", long_description=open('README.rst').read(), keywords='logging gelf graylog2 graylog udp amqp', author='Sever Banesiu', author_email='banesiu.sever@gmail.com', url='https://github.com/severb/graypy', license='BSD License', packages=find_packages(), include_package_data=True, zip_safe=False, extras_require={'amqp': ['amqplib==1.0.2']}, ) graypy-0.2.11/README.rst0000664000175000017500000001353712403570051015403 0ustar severbseverb00000000000000Installing ========== Using easy_install:: easy_install graypy Install with requirements for ``GELFRabbitHandler``:: easy_install graypy[amqp] Usage ===== Messages are sent to Graylog2 using a custom handler for the builtin logging library in GELF format:: import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler) my_logger.debug('Hello Graylog2.') Alternately, use ``GELFRabbitHandler`` to send messages to RabbitMQ and configure your Graylog2 server to consume messages via AMQP. This prevents log messages from being lost due to dropped UDP packets (``GELFHandler`` sends messages to Graylog2 using UDP). You will need to configure RabbitMQ with a 'gelf_log' queue and bind it to the 'logging.gelf' exchange so messages are properly routed to a queue that can be consumed by Graylog2 (the queue and exchange names may be customized to your liking):: import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFRabbitHandler('amqp://guest:guest@localhost/%2F', 'logging.gelf') my_logger.addHandler(handler) my_logger.debug('Hello Graylog2.') Tracebacks are added as full messages:: import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler) try: puff_the_magic_dragon() except NameError: my_logger.debug('No dragons here.', exc_info=1) Configuration parameters ======================== GELFHandler: * **host** - the host of the graylog server. * **port** - the port of the graylog server (default 12201). * **chunk_size** - message chunk size. messages larger than this size will be sent to graylog in multiple chunks (default `1420`). * **debugging_fields** - send debug fields if true (the default). * **extra_fields** - send extra fields on the log record to graylog if true (the default). * **fqdn** - use fully qualified domain name of localhost as source host (socket.getfqdn()). * **localname** - use specified hostname as source host. * **facility** - replace facility with specified value. if specified, record.name will be passed as *logger* parameter. GELFRabbitHandler: * **url** - RabbitMQ URL (ex: amqp://guest:guest@localhost:5672/%2F). * **exchange** - RabbitMQ exchange. Default 'logging.gelf'. A queue binding must be defined on the server to prevent log messages from being dropped. * **debugging_fields** - send debug fields if true (the default). * **extra_fields** - send extra fields on the log record to graylog if true (the default). * **fqdn** - use fully qualified domain name of localhost as source host - socket.getfqdn(). * **exchange_type** - RabbitMQ exchange type (default `fanout`). * **localname** - use specified hostname as source host. * **facility** - replace facility with specified value. if specified, record.name will be passed as `logger` parameter. Using with Django ================= It's easy to integrate ``graypy`` with Django's logging settings. Just add a new handler in your ``settings.py`` like this:: LOGGING = { ... 'handlers': { 'graypy': { 'level': 'WARNING', 'class': 'graypy.GELFHandler', 'host': 'localhost', 'port': 12201, }, }, 'loggers': { 'django.request': { 'handlers': ['graypy'], 'level': 'ERROR', 'propagate': True, }, }, } Custom fields ============= A number of custom fields are automatically added if available: * function * pid * process_name * thread_name You can disable these additional fields if you don't want them by adding an argument to the handler:: handler = graypy.GELFHandler('localhost', 12201, debugging_fields=False) graypy also supports additional fields to be included in the messages sent to Graylog2. This can be done by using Python's LoggerAdapter_ and Filter_. In general, LoggerAdapter makes it easy to add static information to your log messages and Filters give you more flexibility, for example to add additional information based on the message that is being logged. Example using LoggerAdapter_:: import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler) my_adapter = logging.LoggerAdapter(logging.getLogger('test_logger'), { 'username': 'John' }) my_adapter.debug('Hello Graylog2 from John.') Example using Filter_:: import logging import graypy class UsernameFilter(logging.Filter): def __init__(self): # In an actual use case would dynamically get this (e.g. from memcache) self.username = "John" def filter(self, record): record.username = self.username return True my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler) my_logger.addFilter(UsernameFilter()) my_logger.debug('Hello Graylog2 from John.') Contributors: * Sever Banesiu * Daniel Miller .. _LoggerAdapter: http://docs.python.org/howto/logging-cookbook.html#using-loggeradapters-to-impart-contextual-information .. _Filter: http://docs.python.org/howto/logging-cookbook.html#using-filters-to-impart-contextual-information .. image:: https://d2weczhvl823v0.cloudfront.net/severb/graypy/trend.png :alt: Bitdeli badge :target: https://bitdeli.com/free graypy-0.2.11/graypy/0000775000175000017500000000000012403570273015224 5ustar severbseverb00000000000000graypy-0.2.11/graypy/rabbitmq.py0000664000175000017500000001026212403570051017372 0ustar severbseverb00000000000000import json from amqplib import client_0_8 as amqp from graypy.handler import make_message_dict from logging import Filter from logging.handlers import SocketHandler try: from urllib.parse import urlparse, unquote except ImportError: from urlparse import urlparse from urllib import unquote _ifnone = lambda v, x: x if v is None else v class GELFRabbitHandler(SocketHandler): """RabbitMQ / Graylog Extended Log Format handler NOTE: this handler ingores all messages logged by amqplib. :param url: RabbitMQ URL (ex: amqp://guest:guest@localhost:5672/). :param exchange: RabbitMQ exchange. Default 'logging.gelf'. A queue binding must be defined on the server to prevent log messages from being dropped. :param debugging_fields: Send debug fields if true (the default). :param extra_fields: Send extra fields on the log record to graylog if true (the default). :param fqdn: Use fully qualified domain name of localhost as source host (socket.getfqdn()). :param exchange_type: RabbitMQ exchange type (default 'fanout'). :param localname: Use specified hostname as source host. :param facility: Replace facility with specified value. If specified, record.name will be passed as `logger` parameter. """ def __init__(self, url, exchange='logging.gelf', debugging_fields=True, extra_fields=True, fqdn=False, exchange_type='fanout', localname=None, facility=None, virtual_host='/'): self.url = url parsed = urlparse(url) if parsed.scheme != 'amqp': raise ValueError('invalid URL scheme (expected "amqp"): %s' % url) host = parsed.hostname or 'localhost' port = _ifnone(parsed.port, 5672) virtual_host = virtual_host if not unquote(parsed.path[1:]) else unquote(parsed.path[1:]) self.cn_args = { 'host': '%s:%s' % (host, port), 'userid': _ifnone(parsed.username, 'guest'), 'password': _ifnone(parsed.password, 'guest'), 'virtual_host': virtual_host, 'insist': False, } self.exchange = exchange self.debugging_fields = debugging_fields self.extra_fields = extra_fields self.fqdn = fqdn self.exchange_type = exchange_type self.localname = localname self.facility = facility self.virtual_host = virtual_host SocketHandler.__init__(self, host, port) self.addFilter(ExcludeFilter('amqplib')) def makeSocket(self, timeout=1): return RabbitSocket(self.cn_args, timeout, self.exchange, self.exchange_type) def makePickle(self, record): message_dict = make_message_dict( record, self.debugging_fields, self.extra_fields, self.fqdn, self.localname, self.facility) return json.dumps(message_dict) class RabbitSocket(object): def __init__(self, cn_args, timeout, exchange, exchange_type): self.cn_args = cn_args self.timeout = timeout self.exchange = exchange self.exchange_type = exchange_type self.connection = amqp.Connection( connection_timeout=timeout, **self.cn_args) self.channel = self.connection.channel() self.channel.exchange_declare( exchange=self.exchange, type=self.exchange_type, durable=True, auto_delete=False, ) def sendall(self, data): msg = amqp.Message(data, delivery_mode=2) self.channel.basic_publish(msg, exchange=self.exchange) def close(self): try: self.connection.close() except Exception: pass class ExcludeFilter(Filter): def __init__(self, name): """Initialize filter. Initialize with the name of the logger which, together with its children, will have its events excluded (filtered out). """ if not name: raise ValueError('ExcludeFilter requires a non-empty name') self.name = name self.nlen = len(name) def filter(self, record): return not (record.name.startswith(self.name) and ( len(record.name) == self.nlen or record.name[self.nlen] == ".")) graypy-0.2.11/graypy/__init__.py0000664000175000017500000000030412403570051017324 0ustar severbseverb00000000000000from graypy.handler import GELFHandler, WAN_CHUNK, LAN_CHUNK try: from graypy.rabbitmq import GELFRabbitHandler, ExcludeFilter except ImportError: pass # amqplib is probably not installed graypy-0.2.11/graypy/handler.py0000664000175000017500000001231312403570051017205 0ustar severbseverb00000000000000import sys import logging import json import zlib import traceback import struct import random import socket import math from logging.handlers import DatagramHandler WAN_CHUNK, LAN_CHUNK = 1420, 8154 PY3 = sys.version_info[0] == 3 if PY3: string_type = str integer_type = int, else: string_type = basestring integer_type = (int, long) class GELFHandler(DatagramHandler): """Graylog Extended Log Format handler :param host: The host of the graylog server. :param port: The port of the graylog server (default 12201). :param chunk_size: Message chunk size. Messages larger than this size will be sent to graylog in multiple chunks. Defaults to `WAN_CHUNK=1420`. :param debugging_fields: Send debug fields if true (the default). :param extra_fields: Send extra fields on the log record to graylog if true (the default). :param fqdn: Use fully qualified domain name of localhost as source host (socket.getfqdn()). :param localname: Use specified hostname as source host. :param facility: Replace facility with specified value. If specified, record.name will be passed as `logger` parameter. """ def __init__(self, host, port=12201, chunk_size=WAN_CHUNK, debugging_fields=True, extra_fields=True, fqdn=False, localname=None, facility=None): self.debugging_fields = debugging_fields self.extra_fields = extra_fields self.chunk_size = chunk_size self.fqdn = fqdn self.localname = localname self.facility = facility DatagramHandler.__init__(self, host, port) def send(self, s): if len(s) < self.chunk_size: DatagramHandler.send(self, s) else: for chunk in ChunkedGELF(s, self.chunk_size): DatagramHandler.send(self, chunk) def makePickle(self, record): message_dict = make_message_dict( record, self.debugging_fields, self.extra_fields, self.fqdn, self.localname, self.facility) return zlib.compress(json.dumps(message_dict).encode('utf-8')) class ChunkedGELF(object): def __init__(self, message, size): self.message = message self.size = size self.pieces = struct.pack('B', int(math.ceil(len(message) * 1.0/size))) self.id = struct.pack('Q', random.randint(0, 0xFFFFFFFFFFFFFFFF)) def message_chunks(self): return (self.message[i:i + self.size] for i in range(0, len(self.message), self.size)) def encode(self, sequence, chunk): return b''.join([ b'\x1e\x0f', self.id, struct.pack('B', sequence), self.pieces, chunk ]) def __iter__(self): for sequence, chunk in enumerate(self.message_chunks()): yield self.encode(sequence, chunk) def make_message_dict(record, debugging_fields, extra_fields, fqdn, localname, facility=None): if fqdn: host = socket.getfqdn() elif localname: host = localname else: host = socket.gethostname() fields = {'version': "1.0", 'host': host, 'short_message': record.getMessage(), 'full_message': get_full_message(record.exc_info, record.getMessage()), 'timestamp': record.created, 'level': SYSLOG_LEVELS.get(record.levelno, record.levelno), 'facility': facility or record.name, } if facility is not None: fields.update({ '_logger': record.name }) if debugging_fields: fields.update({ 'file': record.pathname, 'line': record.lineno, '_function': record.funcName, '_pid': record.process, '_thread_name': record.threadName, }) # record.processName was added in Python 2.6.2 pn = getattr(record, 'processName', None) if pn is not None: fields['_process_name'] = pn if extra_fields: fields = add_extra_fields(fields, record) return fields SYSLOG_LEVELS = { logging.CRITICAL: 2, logging.ERROR: 3, logging.WARNING: 4, logging.INFO: 6, logging.DEBUG: 7, } def get_full_message(exc_info, message): return '\n'.join(traceback.format_exception(*exc_info)) if exc_info else message def add_extra_fields(message_dict, record): # skip_list is used to filter additional fields in a log message. # It contains all attributes listed in # http://docs.python.org/library/logging.html#logrecord-attributes # plus exc_text, which is only found in the logging module source, # and id, which is prohibited by the GELF format. skip_list = ( 'args', 'asctime', 'created', 'exc_info', 'exc_text', 'filename', 'funcName', 'id', 'levelname', 'levelno', 'lineno', 'module', 'msecs', 'message', 'msg', 'name', 'pathname', 'process', 'processName', 'relativeCreated', 'thread', 'threadName') for key, value in record.__dict__.items(): if key not in skip_list and not key.startswith('_'): if isinstance(value, (string_type, float) + integer_type): message_dict['_%s' % key] = value else: message_dict['_%s' % key] = repr(value) return message_dict graypy-0.2.11/setup.cfg0000664000175000017500000000007312403570273015532 0ustar severbseverb00000000000000[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 graypy-0.2.11/MANIFEST.in0000664000175000017500000000004312403570051015436 0ustar severbseverb00000000000000include LICENSE include README.rst