A simple library for working with the context of logs.
Project description
unilogging
A simple library for working with the context of logs.
Quickstart
pip install unilogging
Features
Logging Contexts and Integration with Dishka
One of the main features of Unilogging is the ability to conveniently pass values into a context, the data from which can later be used by your formatter. This is similar to the extra argument in Python's standard logging.
Unilogging offers new possibilities with a more convenient API. You can populate the context with data at various stages of your application's execution, and logger classes below will pick up this context at any level of the application. This works within the REQUEST-scope.
Here’s an example to illustrate – a middleware in a FastAPI application that generates a request_id and adds it to the context.
@app.middleware("http")
async def request_id_middleware(request, call_next):
logger = await request.state.dishka_container.get(Logger)
with logger.begin_scope(request_id=uuid.uuid4()):
response = await call_next(request)
return response
Generic logger name or your own factory (Integration with Dishka)
You can retrieve a logger from the DI container as follows:
class SomeClass:
def __init__(self, logger: Logger['SomeClass']):
...
In this case, when using the standard integration with Dishka, a new logger will be created with the name your_module.path_to_class.SomeClass. If you don’t need this, you can avoid using a generic logger – in that case, the logger name will be unilogging.Logger, or you can pass your own factory into the integration.
The default logger factory in the provider is used so that you can supply your own factory with custom logic for creating standard loggers – for example, if you want logger names to be generated based on different criteria. However, your factory must conform to the StdLoggerFactory protocol.
Your factory should follow the protocol below:
class StdLoggerFactory(Protocol):
def __call__(self, generic_type: type, default_name: str = ...) -> logging.Logger:
...
Then you can pass it like this:
UniloggingProvider(std_logger_factory=your_factory)
Templating – Injecting values from the context
You can use the built-in log record formatting provided by the library. At the stage of passing the record to the standard logger, it formats the message using format_map, injecting the entire current context. This feature is typically used when your logs are output in JSON format.
with logger.begin_context(user_id=user.id):
logger.info("User {user_id} logged in using {auth_method} auth method", auth_method="telegram")
INFO:unilogging.Logger:User 15c71f84-d0ed-49a6-a36e-ea179f0f62ef logged in using telegram auth method
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 unilogging-0.1.0.tar.gz.
File metadata
- Download URL: unilogging-0.1.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95cf9624f4d813d95cd4bb664d08c2795d41b6f51044629680d0fc731524ebe9
|
|
| MD5 |
73513d0a739af819dc3370b8ce62a1fe
|
|
| BLAKE2b-256 |
43147ae6b7bb884fb6d111f0bd4db89098c615b0f18113186eed47e93c2aad6b
|
File details
Details for the file unilogging-0.1.0-py3-none-any.whl.
File metadata
- Download URL: unilogging-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
515fc111e5b4de76e18148aa6fefd71fbd85e4a24737f97c5f7aba9e7b4efc1e
|
|
| MD5 |
b2f4367655eac5e1c254b3eb35da7b0a
|
|
| BLAKE2b-256 |
18b74e378f2d7d1aafbe9cc8086235f81ddaeb3fed917ab68715e43638712ffc
|