logging-extenson
Extension of built-in python module logging
Instalation
pip install logging-extension
Extensions
JSONFormatter
Formats logs in JSON format
Configuration example
import logging
from logging_extension import JSONFormatter
json_formatter = JSONFormatter(fmt_keys=dict(
logger='name',
level='levelno',
))
logging.basicConfig(level='DEBUG', force=True)
logging.getLogger().handlers[0].setFormatter(json_formatter)
logging.getLogger().info('info_message', extra={'extra': ['value']})
{"logger": "root", "level": 20, "created": "2024-07-29T12:35:21.505616+00:00", "message": "info_message", "extra": ["value"]}
Dictionary-based configuration example
import logging.config
config = {
"version": 1,
"disable_existing_handlers": False,
"formatters": {
"json_formatter": {
"()": "logging_extension.JSONFormatter",
"fmt_keys": {
"name": "name",
"level": "levelno",
}
}
},
"handlers": {
"stream_handler": {
"class": "logging.StreamHandler",
"stream": "ext://sys.stderr",
"formatter": "json_formatter"
},
},
"root": {
"level": "DEBUG",
"handlers": [
"stream_handler"
]
}
}
logging.config.dictConfig(config)
logging.debug('debug_msg', extra={'extra': ['value']})
{"name": "root", "level": 10, "created": "2024-07-29T12:35:21.513188+00:00", "message": "debug_msg", "extra": ["value"]}
LevelFilter
Logging level filter. Allows specifying a comparison function from the built-in operator module or providing your own.
Configuration example
import logging
from logging_extension import LevelFilter
# `compare` argument also can be any function that compares levels
# e.g. compare(record_level: int, filter_level: int) -> bool: ...
only_error_filter = LevelFilter(level='ERROR', compare='eq', name='only_error_filter')
logging.basicConfig(level='DEBUG', force=True)
logging.getLogger().addFilter(only_error_filter)
logging.critical('skip critical')
logging.error('show error')
ERROR:root:show error
Dictionary-based configuration example
import logging.config
config = {
"version": 1,
"disable_existing_handlers": False,
"filters": {
"only_error_filter": {
"()": "logging_extension.LevelFilter",
"level": "ERROR",
"compare": "eq",
}
},
"loggers": {
"root": {
"level": "DEBUG",
"filters": [
"only_error_filter",
]
}
}
}
logging.config.dictConfig(config)
logging.critical('skip critical', extra={'extra': 'value'})
logging.error('show error', extra={'extra': 'value'})
logging.getLogger().filters.clear()
ERROR:root:show error
ThreadedHandler
Container for handlers that perform non-blocking logging in a separate thread.
Essentially, a wrapper around QueueHandler with automatic start of QueueListener using the provided handlers.
Configuration example
import logging
from logging_extension import ThreadedHandler
threaded_handler = ThreadedHandler(
handler_0=logging.StreamHandler(),
handler_2=logging.StreamHandler(),
)
logging.basicConfig(force=True, level='DEBUG')
logging.getLogger().handlers = [threaded_handler]
logging.getLogger().warning('debug msg')
print('in main thread')
in main thread
debug msg
debug msg
Dictionary-based configuration example
import logging.config
config = {
"version": 1,
"disable_existing_handlers": False,
"handlers": {
"stream_handler_0": {
"class": "logging.StreamHandler",
"stream": "ext://sys.stderr",
},
"stream_handler_1": {
"class": "logging.StreamHandler",
"stream": "ext://sys.stderr",
},
"threaded_handler": {
"()": "logging_extension.ThreadedHandler",
"handler_0": "cfg://handlers.stream_handler_0",
"handler_1": "cfg://handlers.stream_handler_1",
},
},
"root": {
"level": "DEBUG",
"handlers": [
"threaded_handler"
]
}
}
logging.config.dictConfig(config)
logging.getLogger().warning('debug msg')
print('in main thread')
debug msg
in main thread
debug msg
Metadata
Release files for logging-extension 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| logging_extension-0.4.0.tar.gz | 7.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| logging_extension-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.9 kB
Release files / logging_extension-0.4.0.tar.gz
| Download URL | logging_extension-0.4.0.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b505298a4065b85e69a68880fdc7c9d38a0f36f402f67324d210a70158743f5b
|
|
BLAKE2b-256 checksum How to use checksums |
7b5149a3f8d1c2c2bf5993c47ca4d0e4e4f2665c02d7a3780b8c293b09ac7968
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12
|
Release files / logging_extension-0.4.0-py3-none-any.whl
| Download URL | logging_extension-0.4.0-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
379f8d221ce6abf29d2f8ae2c3a9523b745981dfc44e34d0464fa2fda477164d
|
|
BLAKE2b-256 checksum How to use checksums |
7562334f81a0ed46ea4404002b7a25accb09d0a5926be9d383e6efb6e4108d26
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12
|