Logger Reference
kevinbotlib.logger
kevinbotlib.logger.FileLoggerConfig
dataclass
Configuration for file-based logging
directory = field(default_factory=LoggerDirectories.get_logger_directory)
class-attribute
instance-attribute
Directory to store log files. Defaults to the user's data directory.
rotation_size = '150MB'
class-attribute
instance-attribute
Rotation size for the log file. Defaults to 150MB.
level = None
class-attribute
instance-attribute
Logging level for the log file. Defaults to the global logging level.
__init__(directory=LoggerDirectories.get_logger_directory(), rotation_size='150MB', level=None)
kevinbotlib.logger.Level
Bases: Enum
Logging levels
TRACE = _internal_logger.level('TRACE')
class-attribute
instance-attribute
Trace level logging - used for more detailed info than DEBUG - level no. 5
DEBUG = _internal_logger.level('DEBUG')
class-attribute
instance-attribute
Debug level logging - used for debugging info - level no. 10
INFO = _internal_logger.level('INFO')
class-attribute
instance-attribute
Debug level logging - used for regular info - level no. 20
WARNING = _internal_logger.level('WARNING')
class-attribute
instance-attribute
Warnng level logging - used for warnings and recommended fixes - level no. 30
ERROR = _internal_logger.level('ERROR')
class-attribute
instance-attribute
Error level logging - used for non-critical and recoverable errors - level no. 40
SECURITY = _internal_logger.level('SECURITY', 45, '<bg 202><bold>')
class-attribute
instance-attribute
Security level logging - used for non-application-breaking secutiry issues/threats - level no. 45
CRITICAL = _internal_logger.level('CRITICAL')
class-attribute
instance-attribute
Error level logging - used for critical and non-recoverable errors - level no. 50
kevinbotlib.logger.Logger
is_configured = False
class-attribute
instance-attribute
config
property
Get the current logger configuration.
Returns:
Type | Description |
---|---|
LoggerConfiguration | None
|
Current global logger configuration. |
loguru_logger
property
Get the internal loguru logger instance.
Returns:
Type | Description |
---|---|
Logger
|
Loguru logger. |
__init__()
Create a logger instance
suppress()
classmethod
Content manager to suppress all logging.
configure(config)
Configures file-based logging with rotation and cleanup.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
LoggerConfiguration
|
Logger configuration. |
required |
add_hook(hook)
Add a new serialized logger write hook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hook
|
Callable[[Message], None]
|
Logger write hook. |
required |
add_hook_ansi(hook)
Add a new ANSI logger write hook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hook
|
Callable[[str], None]
|
Logger write hook. |
required |
log(level, message, opts=None)
Log a message with the specified level and options.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
level
|
Level
|
Logger level |
required |
message
|
str | BaseException
|
Message to log. Can be a string or an exception. |
required |
opts
|
LoggerWriteOpts | None
|
Logger options. |
None
|
trace(message)
Log a trace message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
Message |
required |
debug(message)
Log a debug message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
Message |
required |
info(message)
Log an info message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
Message |
required |
warning(message)
Log a warning message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
Message |
required |
warn(message)
Log a warning message. Deprecated. Use Logger.warning() instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
Message |
required |
error(message)
Log an error message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
Message |
required |
security(message)
Log a security message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
Message |
required |
critical(message)
kevinbotlib.logger.LoggerConfiguration
dataclass
Configuration for the logger
level = Level.INFO
class-attribute
instance-attribute
Global logging level. Defaults to INFO.
enable_stderr_logger = True
class-attribute
instance-attribute
Enable logging to stderr. Defaults to True.
file_logger = None
class-attribute
instance-attribute
File-based logging configuration. Defaults to None.
__init__(level=Level.INFO, enable_stderr_logger=True, file_logger=None)
kevinbotlib.logger.LoggerDirectories
get_logger_directory(*, ensure_exists=True)
staticmethod
Returns the log directory path and ensures its existence if needed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ensure_exists
|
bool
|
Create the directory if it doesn't exist. |
True
|
Returns:
Type | Description |
---|---|
str
|
The log directory path. |
cleanup_logs(directory, max_size_mb=500)
staticmethod
Deletes oldest log files if the total log directory exceeds max_size_mb.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory
|
str
|
Log directory path. |
required |
max_size_mb
|
int
|
Maximum size of the log directory in MB. |
500
|
get_directory_size(directory)
staticmethod
Returns the size of the directory in MB.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory
|
str
|
Directory to measure size. |
required |
Returns:
Type | Description |
---|---|
float
|
Directory size in MB. |
kevinbotlib.logger.LoggerWriteOpts
dataclass
Options for writing to the logger
depth = 1
class-attribute
instance-attribute
Logger depth. Used to determine the statement that triggered the log. Defaults to 1.
colors = True
class-attribute
instance-attribute
Enable colorized output. Defaults to True.
ansi = True
class-attribute
instance-attribute
Enable ANSI escape codes. Defaults to True.
exception = False
class-attribute
instance-attribute
Exception to send to the logger. Defaults to False.
__init__(depth=1, colors=True, ansi=True, exception=False)
kevinbotlib.logger.StreamRedirector
Bases: IO
Redirect a stream to logging
__init__(logger, level=Level.INFO)
write(buffer)
flush()
close()
Close the stream.
readable()
Return False as this stream is not readable.
writable()
Return True as this stream is writable.
seekable()
Return False as this stream is not seekable.
fileno()
Raise OSError as this stream has no underlying file descriptor.
isatty()
Return False as this is not a terminal.
kevinbotlib.remotelog
kevinbotlib.remotelog.ANSILogSender
Class to send ANSI-formatted log entries over Redis Pub/Sub
logger = logger
instance-attribute
client = client
instance-attribute
key = key
instance-attribute
__init__(logger, client, key)
start()
hook(message)
kevinbotlib.remotelog.ANSILogReceiver
Class to receive ANSI-formatted log entries over Redis Pub/Sub
callback = callback
instance-attribute
client = client
instance-attribute
key = key
instance-attribute
__init__(callback, client, key)
start()
kevinbotlib.logger.downloader
kevinbotlib.logger.downloader.RemoteLogDownloader
Tool for downloading logs from a remote host using SFTP.
default_missing_host_key_policy = paramiko.WarningPolicy()
class-attribute
instance-attribute
ssh_connection = None
instance-attribute
sftp_client = None
instance-attribute
log_dir
property
writable
__init__(log_dir='~/.local/share/kevinbotlib/logging/')
connect_with_password(host, username, password, port=22, missing_host_key_policy=default_missing_host_key_policy)
Connect to a remote host using a password.
connect_with_key(host, username, key, port=22, missing_host_key_policy=default_missing_host_key_policy)
Connect to a remote host using a Paramiko RSA key.
disconnect()
get_logfiles()
get_raw_log(logfile, progress_callback=None)
delete_log(logfile)
get_log(logfile, progress_callback=None)
get_file_modification_time(logfile)
get_file_size(logfile)
kevinbotlib.logger.parser
kevinbotlib.logger.parser.LogEntry
dataclass
Class representing a single log entry.
timestamp
instance-attribute
Timestamp of the log entry.
modname
instance-attribute
Module name of the log entry.
function
instance-attribute
Function name of the log entry.
line
instance-attribute
Line of code where the log entry was generated.
level_no
instance-attribute
Log level number.
level_name
instance-attribute
Log level name.
level_icon
instance-attribute
Log level icon.
message
instance-attribute
Messages logged.
__init__(timestamp, modname, function, line, level_no, level_name, level_icon, message)
kevinbotlib.logger.parser.Log
Bases: list
Class representing a list of LogEntry instances.
__init__(entries=None)
Create a new log object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entries
|
list[LogEntry] | None
|
List of LogEntry instances. Defaults to None. |
None
|
append(item)
Append a new LogEntry to the log.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item
|
LogEntry
|
New LogEntry to append. |
required |
extend(items)
insert(index, item)
Insert a new LogEntry into the log at the specified index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index
|
int
|
Index to insert the LogEntry at. |
required |
item
|
LogEntry
|
LogEntry to insert. |
required |
convert()
kevinbotlib.logger.parser.LogParser
Class for parsing log data.
parse(data)
staticmethod
Parse raw log file data into a Log object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
str
|
Log file data. |
required |
Returns: Log object.