Extensions for the original logging module
Project description
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file logging_extension-0.4.0.tar.gz.
File metadata
- Download URL: logging_extension-0.4.0.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b505298a4065b85e69a68880fdc7c9d38a0f36f402f67324d210a70158743f5b
|
|
| MD5 |
b792ee1b28d2ef3a20366188072a0dbb
|
|
| BLAKE2b-256 |
7b5149a3f8d1c2c2bf5993c47ca4d0e4e4f2665c02d7a3780b8c293b09ac7968
|
File details
Details for the file logging_extension-0.4.0-py3-none-any.whl.
File metadata
- Download URL: logging_extension-0.4.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
379f8d221ce6abf29d2f8ae2c3a9523b745981dfc44e34d0464fa2fda477164d
|
|
| MD5 |
d6d5d2f037f56dd5ef9eb05ebfd606d8
|
|
| BLAKE2b-256 |
7562334f81a0ed46ea4404002b7a25accb09d0a5926be9d383e6efb6e4108d26
|