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 you use, which use the stdlib logging module, will follow your logging setup for e.g. structured logging in json.

Import

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

So instead of

import logging

logging.getLogger().info('hey)

you can do

import mh_structlog as logging

logging.getLogger().info('hey)

Usage

The main function of this package is the setup function which should be called once as early as possible in your code. This function configures the 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. See the section below for options to this method.

Setup 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('some_named_logger').info('hey')

To log as json:

from mh_structlog import *

setup(
    log_format='json',
)

getLogger('some_named_logger').info('hey')

To filter everything to a certain level:

from mh_structlog import *

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

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

To write to a file additionally, next to stdout:

from mh_structlog import *

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

getLogger('some_named_logger').info('hey')

To silence one named logger specifically (instead of setting the log level globally):

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

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.17.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

mh_structlog-0.0.17-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mh_structlog-0.0.17.tar.gz
  • Upload date:
  • Size: 10.4 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.17.tar.gz
Algorithm Hash digest
SHA256 18c6bdf3f0748efe76587ecf7188feb5bfd8f7ff589fd316b96c985dec3176aa
MD5 f353abc8424c4df99b4066fa814811a4
BLAKE2b-256 95b490c8050b2ee9c8a6ab93a7696dc411badb6e749740acaa1b7618bc22b747

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for mh_structlog-0.0.17-py3-none-any.whl
Algorithm Hash digest
SHA256 3e87923550e91c197701de7307358d11441a9af1753f934ee924c16e1c0dacbe
MD5 01639fe3377a1c18e95f31449a195e8c
BLAKE2b-256 d9be73327fb3bf1d1a76216a18236601f4ea0f641dbcbc58047e8f8aa0625115

See more details on using hashes here.

Provenance

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