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 details)

Uploaded Source

File details

Details for the file graystruct-0.1.1.zip.

File metadata

  • Download URL: graystruct-0.1.1.zip
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for graystruct-0.1.1.zip
Algorithm Hash digest
SHA256 d76181886b0451e633d11e3023ebb4712d3452f944b13163661b775834569059
MD5 c335a54c90021514172267a4656ef510
BLAKE2b-256 0cb26d519b74d1f2e671bdaff707d4425763c9536692544c496a05c0e15fb785

See more details on using hashes here.

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