Skip to content
/ gray-elf Public

Python logging formatter and handlers for Graylog Extended Log Format (GELF)

License

Notifications You must be signed in to change notification settings

ods/gray-elf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gray-elf

Python logging formatter and handlers for Graylog Extended Log Format (GELF)

Simple usage

import logging
from gray_elf import GelfUdpHandler

logging.root.addHandler(
    GelfUdpHandler(host=GRAYLOG_HOST, port=GRAYLOG_PORT)
)

Customizing data serialization

The example below adds the following features to formatter:

  • serializes big integers as strings to avoid rounding;
  • uses more readable representations for protobuf messages;
  • sends extra fields of log record.
import logging
from gray_elf import GelfFormatter, GelfUdpHandler
from google.protobuf.message import Message
from google.protobuf.text_format import MessageToString
import simplejson


class CustomGelfFormatter(GelfFormatter):

    @staticmethod
    def json_default(value):
        if not isinstance(value, Message):
            return str(value)

        text = MessageToString(message, indent=2)

        if not text:
            return f'{message.DESCRIPTOR.full_name}()'

        lines = [f'{message.DESCRIPTOR.full_name}(']
        for line in text.splitlines():
            if len(line) > 79:
                line = f'{line[:75]}...{line[-1:]}'
            lines.append(line)
        lines.append(')')
        return '\n'.join(lines)

    def to_json(self, fields):
        return simplejson.dumps(
            fields, separators=(',', ':'), default=self.json_default,
            bigint_as_string=True, allow_nan=False,
        )


handler = GelfUdpHandler(host=GRAYLOG_HOST, port=GRAYLOG_PORT)
handler.setFormatter(CustomGelfFormatter(include_extra_fields=True))
logging.root.addHandler(handler)

Change log

See CHANGELOG.

About

Python logging formatter and handlers for Graylog Extended Log Format (GELF)

Resources

License

Stars

Watchers

Forks

Languages