Package: graypy
DescriptionPython logging handlers that send log messages in the
Graylog Extended Log Format (GELF).graypy supports sending GELF logs to both Graylog2 and Graylog3 servers.InstallingUsing pipInstall the basic graypy python logging handlers:pip install graypyInstall with requirements forGELFRabbitHandler:pip install graypy[amqp]Using easy_installInstall the basic graypy python logging handlers:easy_install graypyInstall with requirements forGELFRabbitHandler:easy_install graypy[amqp]Usagegraypy sends GELF logs to a Graylog server via subclasses of the pythonlogging.Handlerclass.Below is the list of ready to run GELF logging handlers defined by graypy:GELFUDPHandler- UDP log forwardingGELFTCPHandler- TCP log forwardingGELFTLSHandler- TCP log forwarding with TLS supportGELFHTTPHandler- HTTP log forwardingGELFRabbitHandler- RabbitMQ log forwardingUDP LoggingUDP Log forwarding to a locally hosted Graylog server can be easily done with
theGELFUDPHandler:importloggingimportgraypymy_logger=logging.getLogger('test_logger')my_logger.setLevel(logging.DEBUG)handler=graypy.GELFUDPHandler('localhost',12201)my_logger.addHandler(handler)my_logger.debug('Hello Graylog.')UDP GELF ChunkersGELF UDP Chunkingis supported by theGELFUDPHandlerand is defined by
thegelf_chunkerargument within its constructor. By default theGELFWarningChunkeris used, thus, GELF messages that chunk overflow
(i.e. consisting of more than 128 chunks) will issue aGELFChunkOverflowWarningandwill be dropped.Othergelf_chunkeroptions are also available:BaseGELFChunkersilently drops GELF messages that chunk overflowGELFTruncatingChunkerissues aGELFChunkOverflowWarningand
simplifies and truncates GELF messages that chunk overflow in a attempt
to send some content to Graylog. If this process fails to prevent
another chunk overflow aGELFTruncationFailureWarningis issued.RabbitMQ LoggingAlternately, useGELFRabbitHandlerto send messages to RabbitMQ and
configure your Graylog server to consume messages via AMQP. This prevents log
messages from being lost due to dropped UDP packets (GELFUDPHandlersends
messages to Graylog using UDP). You will need to configure RabbitMQ with agelf_logqueue and bind it to thelogging.gelfexchange so messages
are properly routed to a queue that can be consumed by Graylog (the queue and
exchange names may be customized to your liking).importloggingimportgraypymy_logger=logging.getLogger('test_logger')my_logger.setLevel(logging.DEBUG)handler=graypy.GELFRabbitHandler('amqp://guest:guest@localhost/',exchange='logging.gelf')my_logger.addHandler(handler)my_logger.debug('Hello Graylog.')Django LoggingIt’s easy to integrategraypywith Django’s logging settings. Just add a
new handler in yoursettings.py:LOGGING={'version':1,# other dictConfig keys here...'handlers':{'graypy':{'level':'WARNING','class':'graypy.GELFUDPHandler','host':'localhost','port':12201,},},'loggers':{'django.request':{'handlers':['graypy'],'level':'ERROR','propagate':True,},},}Traceback LoggingBy default log captured exception tracebacks are added to the GELF log asfull_messagefields:importloggingimportgraypymy_logger=logging.getLogger('test_logger')my_logger.setLevel(logging.DEBUG)handler=graypy.GELFUDPHandler('localhost',12201)my_logger.addHandler(handler)try:puff_the_magic_dragon()exceptNameError:my_logger.debug('No dragons here.',exc_info=1)Default Logging FieldsBy default a number of debugging logging fields are automatically added to the
GELF log if available:functionpidprocess_namethread_nameYou can disable automatically adding these debugging logging fields by
specifyingdebugging_fields=Falsein the handler’s constructor:handler=graypy.GELFUDPHandler('localhost',12201,debugging_fields=False)Adding Custom Logging Fieldsgraypy also supports including custom fields in the GELF logs sent to Graylog.
This can be done by using Python’sLoggerAdapterandFilterclasses.Using LoggerAdapterLoggerAdaptermakes it easy to add static information to your GELF log
messages:importloggingimportgraypymy_logger=logging.getLogger('test_logger')my_logger.setLevel(logging.DEBUG)handler=graypy.GELFUDPHandler('localhost',12201)my_logger.addHandler(handler)my_adapter=logging.LoggerAdapter(logging.getLogger('test_logger'),{'username':'John'})my_adapter.debug('Hello Graylog from John.')Using FilterFiltergives more flexibility and allows for dynamic information to be
added to your GELF logs:importloggingimportgraypyclassUsernameFilter(logging.Filter):def__init__(self):# In an actual use case would dynamically get this# (e.g. from memcache)self.username='John'deffilter(self,record):record.username=self.usernamereturnTruemy_logger=logging.getLogger('test_logger')my_logger.setLevel(logging.DEBUG)handler=graypy.GELFUDPHandler('localhost',12201)my_logger.addHandler(handler)my_logger.addFilter(UsernameFilter())my_logger.debug('Hello Graylog from John.')ContributorsSever BanesiuDaniel MillerTushar MakkarNathan Klapstein