Skip to main content

Some Structlog configuration and wrappers to easily use structlog.

Project description

Structlog logging

Setup the python logging system using structlog. This module configures both structlog and the standard library logging module. So your code can either use a structlog logger or keep working with the standard logging library. This way all third-party packages that is producing logs (which use the stdlib logging module) will follow your logging setup to generate structured logging.

Usage

This library should behave mostly as a drop-in import instead of the logging library import.

So instead of

import logging

logger = logging.getLogger(__name__)

logger.info('hey')

you can do

import mh_structlog as logging

logger = logging.getLogger(__name__)

logger.info('hey')

One big advantage is that you can pass arbitrary keyword arguments to our loggers when producing logs. E.g.

import mh_structlog as logging

logger = logging.getLogger(__name__)

logger.info('some message', hey='ho', a_list=[1,2,3])

These extra key-value pairs will be included in the produced logs; either pretty-printed to the console or as data in the json entries.

Configuration via setup()

To configure your logging, call the setup function, which should be called once as early as possible in your program execution. This function configures all loggers.

import mh_structlog as logging

logging.setup()

This will work out of the box with sane defaults: it logs to stdout in a pretty colored output when running in an interactive terminal, else it defaults to producing json output. See the next section for information on the arguments to this method.

Configuration options

For a setup which logs everything to the console in a pretty (colored) output, simply do:

from mh_structlog import *

setup(
    log_format='console',
)

getLogger().info('hey')

To log as json:

from mh_structlog import *

setup(
    log_format='json',
)

getLogger().info('hey')

To filter everything out up to a certain level:

from mh_structlog import *

setup(
    log_format='console',
    global_filter_level=WARNING,
)

getLogger().info('hey')  # this does not get printed
getLogger().error('hey')  # this does get printed

To write logs to a file additionally (next to stdout):

from mh_structlog import *

setup(
    log_format='console',
    log_file='myfile.log',
)

getLogger().info('hey')

To silence named loggers specifically (instead of setting the log level globally it can be done per named logger):

from mh_structlog import *

setup(
    log_format='console',
    logging_configs=[
        filter_named_logger('some_named_logger', WARNING),
    ],
)

getLogger('some_named_logger').info('hey')  # does not get logged
getLogger('some_named_logger').warning('hey')  # does get logged

getLogger('some_other_named_logger').info('hey')  # does get logged
getLogger('some_other_named_logger').warning('hey')  # does get logged

To include the source information about where a log was produced:

from mh_structlog import *

setup(
    include_source_location=True
)

getLogger().info('hey')

To choose how many frames you want to include in stacktraces on logging exceptions:

from mh_structlog import *

setup(
    log_format='json',
    max_frames=3,
)

try:
    5 / 0
except Exception as e:
    getLogger().exception(e)

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

mh_structlog-0.0.26.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

mh_structlog-0.0.26-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file mh_structlog-0.0.26.tar.gz.

File metadata

  • Download URL: mh_structlog-0.0.26.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for mh_structlog-0.0.26.tar.gz
Algorithm Hash digest
SHA256 a0b40f71cf64f0f99bd163ad83c54c28272cc3a83be6ff6027370ce78f74e50d
MD5 6e9894ec7a99a1f4ab449ab29719ea2f
BLAKE2b-256 c2859751bb475fc60ce8abda96e4eba2ac3d146636dd9ad41ca54d53bdca4165

See more details on using hashes here.

File details

Details for the file mh_structlog-0.0.26-py3-none-any.whl.

File metadata

File hashes

Hashes for mh_structlog-0.0.26-py3-none-any.whl
Algorithm Hash digest
SHA256 043f76b134f3ffd67efefe48ca7f184cce1bc32e7e9551a52d11f23b5d970001
MD5 37e2349b59162a00a133a8c5d40f5c67
BLAKE2b-256 a5c999ad918f9a7d36f7c030b4686707da2c9fd956b3b1e351ca0ca6d5f89324

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