Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Disable additional python fields #5

Merged
merged 2 commits into from over 2 years ago

2 participants

Gareth Rushgrove Sever Băneșiu
Gareth Rushgrove

This pull allows for disabling the additional python fields (like function, line number, etc.) when you create the hander, while maintaining the existing default behaviour.

My use case for this is that I'm writing a command line tool which, when run, gathers some information and pushes it to graylog. That means the position in the file, the function, the filename, etc. will always be the same and more importantly nothing to do with the information being logged.

I think this is worth including for other cases where python is being used as more a system scripting language rather than for application development.

Any comments or changes let me know. And thanks for graypy, exactly what I was looking for this morning.

Sever Băneșiu
Owner

Everything looks great, thanks for your contribution. I'll release a new version as soon as I handle another pull request regarding the new GELF message format.

Sever Băneșiu severb merged commit 6b814c1 into from
Sever Băneșiu severb closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 21 additions and 14 deletions. Show diff stats Hide diff stats

  1. +4 0 README.rst
  2. +17 14 graypy/handler.py
4 README.rst
Source Rendered
@@ -46,6 +46,10 @@ A number of custom fields are automatically added if available:
46 46 * process_name
47 47 * thread_name
48 48
  49 +You can disable these additional fields if you don't want them by adding an argument to the hander::
  50 +
  51 + handler = graypy.GELFHandler('localhost', 12201, debugging_fields=False)
  52 +
49 53 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.
50 54
51 55 Example using LoggerAdapter_::
31 graypy/handler.py
@@ -12,7 +12,8 @@
12 12
13 13
14 14 class GELFHandler(DatagramHandler):
15   - def __init__(self, host, port, chunk_size=WAN_CHUNK):
  15 + def __init__(self, host, port, chunk_size=WAN_CHUNK, debugging_fields=True):
  16 + self.debugging_fields = debugging_fields
16 17 self.chunk_size = chunk_size
17 18 DatagramHandler.__init__(self, host, port)
18 19
@@ -40,27 +41,29 @@ def get_full_message(self, exc_info):
40 41 return traceback.format_exc(exc_info) if exc_info else ''
41 42
42 43 def make_message_dict(self, record):
43   - return self.add_extra_fields({
44   - 'version': "1.0",
  44 + fields = {'version': "1.0",
45 45 'host': socket.gethostname(),
46 46 'short_message': record.getMessage(),
47 47 'full_message': self.get_full_message(record.exc_info),
48 48 'timestamp': record.created,
49 49 'level': self.convert_level_to_syslog(record.levelno),
50 50 'facility': record.name,
51   - 'file': record.pathname,
52   - 'line': record.lineno,
53   - '_function': record.funcName,
54   - '_pid': record.process,
55   - '_thread_name': record.threadName,
56   - }, record)
  51 + }
  52 + if self.debugging_fields:
  53 + fields.update({
  54 + 'file': record.pathname,
  55 + 'line': record.lineno,
  56 + '_function': record.funcName,
  57 + '_pid': record.process,
  58 + '_thread_name': record.threadName,
  59 + })
  60 + # record.processName was added in Python 2.6.2
  61 + pn = getattr(record, 'processName', None)
  62 + if pn is not None:
  63 + fields['_process_name'] = pn
  64 + return self.add_extra_fields(fields, record)
57 65
58 66 def add_extra_fields(self, message_dict, record):
59   - # record.processName was added in Python 2.6.2
60   - pn = getattr(record, 'processName', None)
61   - if pn is not None:
62   - message_dict['_process_name'] = pn
63   -
64 67 # skip_list is used to filter additional fields in a log message.
65 68 # It contains all attributes listed in
66 69 # http://docs.python.org/library/logging.html#logrecord-attributes

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.