EzLogger¶
Loguru-based file logging handler.
Overview¶
The EzLogger class (alias: Logger) provides file logging with rotation, retention, and compression using loguru. It offers structured log formatting with timestamps, levels, and source location.
Class Reference¶
EzLogger
¶
EzLogger(log_file: Path | str, level: str = 'INFO', rotation: str | None = None, retention: str | None = None, compression: str | None = None)
Bases: LoggingHandler
File logger handler with advanced formatting and session management.
This handler provides file-based logging with: - Structured log format - Session separators - HTML tag sanitization - Automatic file creation
Initialize the file logger handler.
| PARAMETER | DESCRIPTION |
|---|---|
log_file
|
Path to the log file
TYPE:
|
level
|
The desired logging level
TYPE:
|
rotation
|
Rotation size (e.g., "10 MB") or time (e.g., "1 day")
TYPE:
|
retention
|
Retention period (e.g., "7 days")
TYPE:
|
compression
|
Compression format (e.g., "zip", "gz")
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValidationError
|
If the provided level is invalid |
FileOperationError
|
If file operations fail |
Source code in src/ezpl/handlers/file.py
Attributes¶
level_manually_set
property
¶
Return whether level was set manually at runtime.
Functions¶
mark_level_as_configured
¶
set_level
¶
Set the logging level.
| PARAMETER | DESCRIPTION |
|---|---|
level
|
The desired logging level
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValidationError
|
If the provided level is invalid |
LoggingError
|
If level update fails |
Source code in src/ezpl/handlers/file.py
log
¶
Log a message with the specified level.
| PARAMETER | DESCRIPTION |
|---|---|
level
|
The log level
TYPE:
|
message
|
The message to log (any type, will be converted to string)
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValidationError
|
If the level is invalid |
LoggingError
|
If logging fails |
Source code in src/ezpl/handlers/file.py
trace
¶
debug
¶
info
¶
success
¶
warning
¶
warn
¶
error
¶
critical
¶
exception
¶
bind
¶
opt
¶
patch
¶
get_loguru
¶
Get the underlying Loguru logger instance for advanced usage.
Returns:
* loguru.Logger: The loguru logger instance
Raises:
* LoggingError: If the logger is not initialized
Source code in src/ezpl/handlers/file.py
get_log_file
¶
get_file_size
¶
Get the current log file size in bytes.
| RETURNS | DESCRIPTION |
|---|---|
int
|
File size in bytes, or 0 if file doesn't exist or error occurs |
Source code in src/ezpl/handlers/file.py
close
¶
Close the logger handler and release file handles.
This method removes the loguru handler to release file handles, which is especially important on Windows where files can remain locked.
Source code in src/ezpl/handlers/file.py
add_separator
¶
Add a separator line to the log file for session distinction.
| RAISES | DESCRIPTION |
|---|---|
FileOperationError
|
If writing to the log file fails |