EzLogger¶
Loguru-backed file logger handler.
🔍 API¶
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_file
|
Path | str
|
Path to the log file |
required |
level
|
str
|
The desired logging level |
'INFO'
|
rotation
|
str | None
|
Rotation size (e.g., "10 MB") or time (e.g., "1 day") |
None
|
retention
|
str | None
|
Retention period (e.g., "7 days") |
None
|
compression
|
str | None
|
Compression format (e.g., "zip", "gz") |
None
|
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.
Functions¶
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 |