Skip to main content

Integration between structlog and graylog GELF, provided by graypy

Project description

Integration between structlog and graylog GELF, provided by graypy.

Rationale

Structlog provides a rich tool for producing structured log messages from applications. Graypy provides a Python interface to emit logs in the GELF format accepted by graylog. In effect, structlog pre-processes the _inputs_ to Python logging module, while graypy processes the outputs (LogRecord instances), and neither expects the other to be present.

graystruct provides a small integration layer composed of two main components that are used in conjunction with both structlog and graypy. These components minimally alter the behaviour of structlog and graypy at their interface points so that they are able to cooperate in producing structured logs.

Example

>>> import logging
>>> import structlog
>>> from graystruct.encoder import GELFEncoder
>>> from graystruct.handler import GELFHandler
>>> from graystruct.utils import add_app_context
>>> structlog.configure(
...     logger_factory=structlog.stdlib.LoggerFactory(),
...     processors=[
...         # Prevent exception formatting if logging is not configured
...         structlog.stdlib.filter_by_level,
...         # Add file, line, function information of where log occurred
...         add_app_context,
...         # Format positional args to log as in stdlib
...         structlog.stdlib.PositionalArgumentsFormatter(),
...         # Add a timestamp to log message
...         structlog.processors.TimeStamper(fmt='iso', utc=True),
...         # Dump stack if ``stack_info=True`` passed to log
...         structlog.processors.StackInfoRenderer(),
...         # Format exception info is ``exc_info`` passed to log
...         structlog.processors.format_exc_info,
...         # Encode the message in GELF format (this must be the final processor)
...         structlog.processors.GELFEncoder(),
...     ],
... )
>>> std_logger = logging.getLogger()
>>> std_logger.setLevel(logging.WARNING)
>>> gelf_handler = GELFHandler('localhost', 12201)
>>> std_logger.addHandler(gelf_handler)
>>> logger = structlog.get_logger('some.package')
# Will transmit a GELF-encoded message
>>> logger.error('user.login', username='sjagoe')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graystruct-0.1.1.zip (10.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page