Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #5 from garethr/master

Disable additional python fields
  • Loading branch information...
commit 6b814c1789a74d0ebccc79e15bcd0fd20907fd6c 2 parents f68170f + cc9b6e9
@severb authored
Showing with 21 additions and 14 deletions.
  1. +4 −0 README.rst
  2. +17 −14 graypy/handler.py
View
4 README.rst
@@ -46,6 +46,10 @@ A number of custom fields are automatically added if available:
* process_name
* thread_name
+You can disable these additional fields if you don't want them by adding an argument to the hander::
+
+ 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_::
View
31 graypy/handler.py
@@ -12,7 +12,8 @@
class GELFHandler(DatagramHandler):
- def __init__(self, host, port, chunk_size=WAN_CHUNK):
+ def __init__(self, host, port, chunk_size=WAN_CHUNK, debugging_fields=True):
+ self.debugging_fields = debugging_fields
self.chunk_size = chunk_size
DatagramHandler.__init__(self, host, port)
@@ -40,27 +41,29 @@ def get_full_message(self, exc_info):
return traceback.format_exc(exc_info) if exc_info else ''
def make_message_dict(self, record):
- return self.add_extra_fields({
- 'version': "1.0",
+ fields = {'version': "1.0",
'host': socket.gethostname(),
'short_message': record.getMessage(),
'full_message': self.get_full_message(record.exc_info),
'timestamp': record.created,
'level': self.convert_level_to_syslog(record.levelno),
'facility': record.name,
- 'file': record.pathname,
- 'line': record.lineno,
- '_function': record.funcName,
- '_pid': record.process,
- '_thread_name': record.threadName,
- }, record)
+ }
+ if self.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
+ return self.add_extra_fields(fields, record)
def add_extra_fields(self, message_dict, record):
- # record.processName was added in Python 2.6.2
- pn = getattr(record, 'processName', None)
- if pn is not None:
- message_dict['_process_name'] = pn
-
# 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
Please sign in to comment.
Something went wrong with that request. Please try again.