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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mh_structlog-0.0.22.tar.gz
.
File metadata
- Download URL: mh_structlog-0.0.22.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | defe588aaadc3db9d105d326be1faab86ea084bd40259957d742918da7e0c192 |
|
MD5 | e3b64117c2bf05f8d98e616aec6580e9 |
|
BLAKE2b-256 | 0b1ecc9c17ac71f4ef2a1bfaf076de6727f517d8932400db367749f949034288 |
Provenance
File details
Details for the file mh_structlog-0.0.22-py3-none-any.whl
.
File metadata
- Download URL: mh_structlog-0.0.22-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ccd39671a00f014218abc32d022549698c8ac5112500764891593d9d16a3eb1 |
|
MD5 | 8480957f940709c2a5a75ef05659a5c8 |
|
BLAKE2b-256 | c782367e25224c304793e5ff33b68c20e587e53572621167d54ab996da94d185 |