Skip to main content

MH-Structlog

This package is used to setup the python logging system in combination with structlog. It configures both structlog and the standard library logging module, so your code can either use a structlog logger (which is recommended) or keep working with the standard logging library. This way all third-party packages that are producing logs (which use the stdlib logging module) will follow your logging setup and you will always output structured logging.

It is a fairly opinionated setup but has some configuration options to influence the behaviour. The two output log formats are either pretty-printing (for interactive views) or json. It includes optional reporting to Sentry, and can also log to a file.

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
logging.setup()  # necessary once at program startup, see readme further below

logger = logging.getLogger(__name__)

logger.info('hey')

One big advantage of using the structlog logger over de stdlib logging one, 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 specific 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)

To enable Sentry integration, pass a dict with a config according to the arguments which structlog-sentry allows to the setup function:

from mh_structlog import *
import sentry_sdk

config = {'dsn': '1234'}
sentry_sdk.init(dsn=config['dsn'])

setup(
    sentry_config={'event_level': WARNING}  # pass everything starting from WARNING level to Sentry
)

Development

Install the environment:

uv sync --python-preference only-managed --frozen --all-extras --all-groups

Run the unittests:

uv run pytest -s --pdb --pdbcls=IPython.terminal.debugger:Pdb

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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mh_structlog-0.0.57-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mh_structlog-0.0.57.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for mh_structlog-0.0.57.tar.gz
Algorithm Hash digest
SHA256 14c05485cef78734b8d304522903ee1b280c03450e8a9bf6103a4ee0b382f703
MD5 fd901a725a62c58f878033e10b27bfa9
BLAKE2b-256 ecf8fea3dd015c61cdff42a53e5d9bf9e668add4e31b613ac9864771b84fcc98

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mh_structlog-0.0.57-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for mh_structlog-0.0.57-py3-none-any.whl
Algorithm Hash digest
SHA256 adc3ed4b359e43c834d4cd655295065267d510d8cea8dc695f56da3b120ee616
MD5 768cc38d027188e4bf436be4fc0cf4d3
BLAKE2b-256 644943f8172364c9686e55405498d10fdbe889e24d5a36920317e467739390f4

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.57 This release

2 files

0.0.56

2 files

0.0.55

2 files

0.0.54

2 files

0.0.53

2 files

0.0.52

2 files

0.0.51

2 files

0.0.49

2 files

0.0.48

2 files

0.0.47

2 files

0.0.46

2 files

0.0.45

2 files

0.0.44

2 files

0.0.42

2 files

0.0.41

2 files

0.0.40

2 files

0.0.37

2 files

0.0.36

2 files

0.0.35

2 files

0.0.34

2 files

0.0.33

2 files

0.0.32

2 files

0.0.31

2 files

0.0.30

2 files

0.0.29

2 files

0.0.28

2 files

0.0.27

2 files

0.0.26

2 files

0.0.25

2 files

0.0.22

2 files

0.0.21

2 files

0.0.20

2 files

0.0.19

2 files

0.0.17

2 files

0.0.16

2 files

0.0.14

2 files

0.0.13

2 files

0.0.12

2 files

0.0.11

2 files

0.0.10

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.5

2 files

0.0.4

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page