EzLogger¶
Loguru-backed file logger handler.
🔍 API¶
EzLogger
¶
EzLogger(
log_file: Path | str,
level: str = "INFO",
rotation: RotationSpec = None,
retention: RetentionSpec = None,
compression: CompressionSpec = None,
*,
backtrace: bool = True,
diagnose: bool = False,
)
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_file
|
Path | str
|
Path to the log file |
required |
level
|
str
|
The desired logging level |
'INFO'
|
rotation
|
RotationSpec
|
Rotation trigger — size string ("10 MB"), byte count, datetime.time, datetime.timedelta, or a predicate callable |
None
|
retention
|
RetentionSpec
|
Retention policy — duration string ("7 days"), number of files to keep (int), timedelta, or a cleanup callable |
None
|
compression
|
CompressionSpec
|
Compression format name (see SUPPORTED_COMPRESSIONS) or a callable applied to the rotated file path |
None
|
backtrace
|
bool
|
If True, extend tracebacks beyond the catching point |
True
|
diagnose
|
bool
|
If True, include local variable values in tracebacks. Defaults to False, unlike loguru: locals routinely hold secrets, and log files are archived and shipped elsewhere. |
False
|
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the provided level is invalid |
FileOperationError
|
If file operations fail |
Attributes¶
level_manually_set
property
¶
Return whether level was set manually at runtime.
Methods:¶
mark_level_as_configured
¶
Mark the current level as coming from configuration (not manual set).
set_level
¶
Set the logging level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
The desired logging level |
required |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the provided level is invalid |
LoggingError
|
If level update fails |
log
¶
Log a message with the specified level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
The log level |
required |
message
|
Any
|
The message to log (any type, will be converted to string) |
required |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the level is invalid |
LoggingError
|
If logging fails |
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
get_log_file
¶
Get the current log file path.
Returns:
| Type | Description |
|---|---|
Path
|
Path to the log file |
get_file_size
¶
Get the current log file size in bytes.
Returns:
| Type | Description |
|---|---|
int
|
File size in bytes, or 0 if file doesn't exist or error occurs |
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.
add_separator
¶
Add a separator line to the log file for session distinction.
Raises:
| Type | Description |
|---|---|
FileOperationError
|
If writing to the log file fails |