bracelogger
A Python library that enables using the brace-style string formatting in log messages.
Features:
- Supports a wide range of Python versions (v2.7+)
- No dependencies
- Easy to use - no special syntax required
- Easy to transition to from stdlib logging - just change the
logging.getLoggercalls and message templates. - Only enables brace-style formatting for loggers created by the library. This allows for gradually transitioning to brace-style formatting without breaking existing loggers or third party packages.
- The formatting of the message is delayed until it is output (or not at all if the log message is filtered).
- The args passed into the log call are stored on the
logging.LogRecordobjects as usual.
Installation
pip install bracelogger
Usage example
# import the library
from bracelogger import get_logger
# set up the logger
__log__ = get_logger(__name__)
# use brace-style formatting in log messages
try:
process(some_obj)
except Exception:
__log__.warning(
"Failed to process object '{0!r}' with name '{0.name}' and path '{0.path}'",
some_obj,
exc_info=True
)
Note that the above example is very basic. The real power of this module comes from being able to use the more advanced operations that the brace-style formatting can provide. See the docs on the format string syntax for more details.
Key-based formatting
The logger from the standard library has a special case (introduced in Python 2.4) where key-based formatting can be used when a single non-empty dictionary is passed into the log message like so:
__log__.info("a:%(a)s, b:%(b)s", {"a": 1, "b": 2})
This is also supported when using brace-style formatting:
# These both produce the same log message:
__log__.info("a:{a}, b:{b}", {"a": 1, "b": 2})
__log__.info("a:{0[a]}, b:{0[b]}", {"a": 1, "b": 2})
Compatibility with existing code and loggers
This library will only enable brace-style formatting for loggers created by this module's
get_logger() function. Loggers created via the stdlib logging.getLogger() function will still
use the normal %-based formatting.
This opt-in style means that codebases can gradually transition to brace-style logging without having to convert everything over all at once. It also means that the logs from any third-party code like libraries will continue to work as normal.
In addition to being compatible with existing code, it should also be compatible with most other stdlib-compatible logging packages and modifications. As when using the stdlib logger, the message arguments are still stored on the log record and the message is only formatted when it is handled (and not at all if the message is filtered).
Converting existing code
Because there is no special syntax required, migrating existing logs to brace-style formatting is easy:
- Change loggers created with
logging.getLogger(name)to usebracelogger.get_logger(name) - Change the log messages for the loggers to use brace-style formatting (ex: change
%sto{})
Tests
This package contains basic tests. To run them, install pytest (pip install pytest) and run
py.test in the project directory.
License
Licensed under the GNU LGPLv3.
Metadata
Release files for bracelogger 1.1.3
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 |
|---|---|---|---|---|
| bracelogger-1.1.3.tar.gz | 3.3 kB | Without provenance | Details for bracelogger-1.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bracelogger-1.1.3-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for bracelogger-1.1.3-py2.py3-none-any.whl |
Total release size: 6.9 kB
Release files / bracelogger-1.1.3.tar.gz
| Download URL | bracelogger-1.1.3.tar.gz |
|---|---|
| Size | 3.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a84afffebc93868e332c5bcdf69ccf14739af1bbf798d17b4d30fe055d2d6cd4
|
|
BLAKE2b-256 checksum How to use checksums |
0215a5a9f2c99d7e754dad65d83e5d95ef092b2a3867724aaf9c07ee403ec051
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Release files / bracelogger-1.1.3-py2.py3-none-any.whl
| Download URL | bracelogger-1.1.3-py2.py3-none-any.whl |
|---|---|
| Size | 3.6 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
c7b444560e0d9291911bde9fc6769dd3a65e3f5c31b0652127368b79e33c7519
|
|
BLAKE2b-256 checksum How to use checksums |
06866c668d5a330744b60eb2abb9daf96bda3ba41c57355c02a018f1be47d0f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Security for bracelogger 1.1.3
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.