Zirconium Logging (ZrLog)
Logging configuration is often complex and Python is missing a few "nice" features that exist in other languages logging systems. This package hopes to simplify the experience of configuring logging while adding a few nice features:
- Loading logging configuration from TOML or YAML (via
zirconium) instead of the olderconfigparserformat TRACElevel for very fine-grained output (more so thatDEBUG)NOTICElevel for normal conditions that should always be outputOUTlevel for logging user messagesAUDITlevel for interacting with Python's audit hooks- A threaded audit hook that logs most audit messages
- The ability to disable stack trace output
- The ability to set context-specific "extra" variables on all loggers and to provide defaults for these so that they can be added to all logging messages within that context (e.g. for usernames)
Basic Usage
# Do this at the start of any run of your code
import zrlog
# Imports logging configuration from TOML and sets up the logging system for you.
zrlog.init_logging()
# Set a default username
zrlog.set_default_extra('username', '**anonymous**')
# logging.getLogger() works as well, this version is a wrapper that (a) makes sure that `zrlog.init_logging()` was
# called and (b) provides a better type hint for the logger class.
logger = zrlog.get_logger(__name__)
Configuration File
By default, logging configuration comes from a TOML file in up to three places:
[HOME_DIRECTORY]/.logging.toml[CURRENT_WORKING_DIRECTORY]/.logging.toml- Value of the
ZRLOG_CONFIG_FILEenvironment variable. This file may also be a YAML or other formats supported byzirconium
If you use zirconium for your project configuration, it can also be in any configuration file specified by your
project.
The logging configuration file is similar to that defined by logging.config.dictConfig(), with a few extensions. See
the .logging.example.toml file for configuration.
Performance impact
Testing suggests that the impact of replacing the typical logging approach with this module is about 14% slower to
get a logger and 10% slower to make a logging call with extras. However, the time to make a log to stdout is
still only 0.013 ms (compared to 0.012 ms for the standard logging package), therefore this performance impact is
probably insignificant in most use cases.
Log Level Recommendations
| Level | Use Case |
|---|---|
| critical | An error so severe has occurred that the application may now crash. |
| error | An error has occurred and may have created unexpected behaviour. |
| warning | Something unexpected happen but it is recoverable, or a problem may occur in the future. |
| notice | Something expected has happened that needs to be tracked in a production environment (e.g. user login). |
| out | Something expected has happened in a command line environment that the user needs to be notified of. |
| info | Something expected has happened that does not need tracking but can be useful to confirm normal operation. |
| debug | Additional detail for debugging an issue |
| trace | Even more detail for debugging an issue |
| audit | Python auditing output only |
Logging Audit Events
This package provides a system for turning sys.audit() events into log records using a thread-based queue. This is
necessary because audit events don't play nicely with the logging subsystem, leading to inconsistent errors if the
logger log() method is called directly from the audit hook. Audit logging must be enabled specifically by setting
the with_audit flag:
# .logging.toml
[logging]
with_audit = true
While the default level is "AUDIT", you can change this to any of the logging level prefixes by specifying the audit_level:
# .logging.toml
[logging]
with_audit = true
audit_level = "INFO"
One specific event can cause further problems: sys._getframe() is called repeatedly from the logging subsystem in Python
(in 3.8 at least). These audit events are NOT logged by default, but logging of them can be enabled by turning off the
omit_logging_frames flag.
# .logging.toml
[logging]
with_audit = true
omit_logging_frames = false
Audit events are logged (by default) at the AUDIT level which is below TRACE; your logger and handler must be set to that level to see these events:
[logging.root]
level = "AUDIT"
handlers = ["console"]
[logging.handlers.console]
class = "logging.StreamHandler"
formatter = "brief"
level = "AUDIT"
stream = "ext://sys.stdout"
Change Log
version 0.3.0
- Added logger extra handling via
set_logger_extraandset_default_logger_extra - Added the
NOTICElevel to correspond with more typical usage.OUTshould be used for user output where it needs to be logged andNOTICEfor normal conditions that need to be logged even in production.
version 0.2.0
- Updated the audit handling thread to use a
threading.Eventto end itself rather than a boolean flag. - Added the
no_configflag, mostly to be used in test suites to ensure everything still works properly. - Several documentation cleanup items
Metadata
Release files for zrlog 0.3.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 | Provenance | Actions |
|---|---|---|---|---|
| zrlog-0.3.0.tar.gz | 7.4 kB | Without provenance | Details for zrlog-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| zrlog-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for zrlog-0.3.0-py3-none-any.whl |
Total release size: 15.3 kB
Release files / zrlog-0.3.0.tar.gz
| Download URL | zrlog-0.3.0.tar.gz |
|---|---|
| Size | 7.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f2ac66a3099abbd466211264e407314bed8de6c8f50864f35ecf7c87e7fb3890
|
|
BLAKE2b-256 checksum How to use checksums |
b5803c34e87d647db3305e339a646148c70534bda3e52ff8414eea9347f7a265
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.6
|
Release files / zrlog-0.3.0-py3-none-any.whl
| Download URL | zrlog-0.3.0-py3-none-any.whl |
|---|---|
| Size | 7.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0dd7cbb2c2b287fb1fea20d3c1c856b4046a6424b11b1cb7195c5d5fdb0c37f5
|
|
BLAKE2b-256 checksum How to use checksums |
9140bea8795bb364dc9e73d40de73aba48cebc6ad9879be494191fa52c006cd8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.6
|
Security for zrlog 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.