API Reference¶
This section provides detailed API documentation for ctxlog.
Core API¶
ctxlog - A structured logging library for Python.
This library provides a structured logging system with context-rich logs, multiple output handlers, and support for traditional log levels.
- class ctxlog.LogLevel(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Bases:
EnumLog levels for ctxlog.
- DEBUG = 10¶
- INFO = 20¶
- WARNING = 30¶
- ERROR = 40¶
- CRITICAL = 50¶
- classmethod from_string(level_str)[source]¶
Convert a string to a LogLevel.
- Parameters:
level_str (
str) – The string representation of the log level.- Return type:
- Returns:
The corresponding LogLevel enum value.
- Raises:
ValueError – If the string is not a valid log level.
- class ctxlog.Handler(level=None, serialize=False)[source]¶
Bases:
ABCBase class for log handlers.
- class ctxlog.ConsoleHandler(level=None, serialize=False, color=True, use_stderr=False)[source]¶
Bases:
HandlerHandler that outputs logs to the console.
- class ctxlog.FileHandler(file_path, level=None, serialize=True, rotation=None)[source]¶
Bases:
HandlerHandler that outputs logs to a file.
- Parameters:
file_path (str)
level (LogLevel | None)
serialize (bool)
rotation (FileRotation | None)
- class ctxlog.FileRotation(size=None, time=None, keep=5, compression=None)[source]¶
Bases:
objectConfiguration for log file rotation.
- Parameters:
- __init__(size=None, time=None, keep=5, compression=None)[source]¶
Initialize a FileRotation configuration.
- Parameters:
size (
Optional[str]) – Size threshold for rotation (e.g., “20MB”). Mutually exclusive with time.time (
Optional[str]) – Time of day for rotation (e.g., “00.00”). Mutually exclusive with size.keep (
int) – Number of rotated files to keep.compression (
Optional[Literal['gzip','zip']]) – Compression method for old files (e.g., “gzip”, “zip”).
- Raises:
ValueError – If both size and time are specified.
- Return type:
None
- class ctxlog.Log(event=None, has_parent=False)[source]¶
Bases:
objectA log context with methods for adding structured fields and emitting logs.
- class ctxlog.Logger(name)[source]¶
Bases:
objectMain entry point for ctxlog.
This class provides methods for creating log contexts and emitting logs.
- Parameters:
name (str)
- __init__(name)[source]¶
Initialize a Logger.
- Parameters:
name (
str) – The name of the logger, typically the module name.- Return type:
None
- ctx(**kwargs)[source]¶
Create a new log context with additional fields. Use .new().ctx() instead if you want to set an event name.
- ctxlog.configure(level=LogLevel.INFO, timefmt='iso', utc=False, handlers=None)[source]¶
Configure the global settings for ctxlog.
This function should be called once, typically at application startup.
- Parameters:
level (
Union[LogLevel,Literal['debug','info','warning','error','critical'],int]) – The global log level. Can be a LogLevel enum value, a string, or an int.timefmt (
str) – Timestamp format for log entries. Use ‘iso’ for ISO8601, or provide a custom strftime format string.utc (
bool) – If True, use UTC for timestamps. Default is False (local time).handlers (
Optional[List[Handler]]) – List of output handlers. If None, a default ConsoleHandler will be used.
- Return type:
Example
Configure ctxlog at startup:
ctxlog.configure( level=ctxlog.LogLevel.INFO, timefmt="iso", utc=True, handlers=[ ctxlog.ConsoleHandler(serialize=False, color=True, use_stderr=False), ctxlog.FileHandler( level=ctxlog.LogLevel.DEBUG, serialize=True, file_path="./app.log", rotation=ctxlog.FileRotation( size="20MB", # mutually exclusive with time time="00.00", # mutually exclusive with size keep=12, compress_old=True ), ), ] )