Logger
Logger
An asynchronous logger supporting contextual logging with configurable output.
Provides a queue-based logging system that can write to either a file or stdout. Supports contextual logging where entries can be grouped under specific contexts, and includes support for both message and error logging.
Can be used either as an async context manager or directly:
-
As a context manager:
-
Direct usage:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str | Path | None
|
Path to the log file. If None, logs will be written to stdout |
None
|
Initialize a new Logger instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str | Path | None
|
Path to the log file. If None, logs will be written to stdout. |
None
|
Source code in freeact/logger.py
aclose
async
Close the logger and process remaining entries.
This method must be called explicitly when using the Logger
directly (not as
a context manager) to ensure all queued log entries are processed before
shutting down.
When using the Logger
as a context manager, this method is called automatically.
Source code in freeact/logger.py
context
async
context(frame: str)
A context manager that adds an additional context frame to the current context.
Example
async with logger.context("User Login"):
await logger.log("Attempting login") # Logs with "User Login" context
await db.query(...)
await logger.log("Login successful") # Logs with "User Login" context
# Contexts can be nested
async with logger.context("Profile"):
# Logs with "User Login / Profile" context
await logger.log("Loading user profile")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
frame
|
str
|
The context frame name to add |
required |
Yields:
Type | Description |
---|---|
The Logger instance. |
Raises:
Type | Description |
---|---|
Exception
|
Re-raises any exception that occurs within the context after logging it. |
Source code in freeact/logger.py
log
async
Log a message with optional metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log |
required |
metadata
|
dict[str, Any] | None
|
Optional dictionary of additional data to include |
None
|