Documentation for ToMeDa
tomeda.tomeda_logging
This module provides extended logging functionality, including the
TraceLogger
class and the TomedaLogging
class for flexible logging
configuration.
TraceLogger
enhances Python's standard logging.Logger
class by
introducing a new logging level, TRACE
, which is lower than DEBUG
.
It includes a new trace
method for logging at this level.
TomedaLogging
, on the other hand, provides a convenient interface for
configuring both the root logger and any custom loggers needed by
individual modules. It manages the format of log messages, the default
log level, and the routing of logs to various outputs, such as the
console or a file. For a logger that does not require a unique
configuration, simply invoking logging.getLogger(__name__)
will apply
the default settings as set by root logger.
In essence, this module extends Python's logging capabilities, making it easier to manage and adapt logs according to specific needs.
logger
module-attribute
logger = getLogger(__name__)
TomedaLogging
TomedaLogging(
name: str | None = None,
console_level: int | None = TraceLogger.TRACE,
file_level: int | None = logging.DEBUG,
file_name: str | None = "tomeda.log",
console_formatter: Formatter
| str
| None = _CONSOLE_HANDLER_FORMATTER,
file_formatter: Formatter
| str
| None = _FILE_HANDLER_FORMATTER,
)
This class configures logging for the application, primarily focusing on configuring the root logger, but also supports configuring module-specific loggers.
The TomedaLogging class supports configuring different loggers with different levels, formatting styles, and handlers for console and file output. If a logger does not need to be specifically configured, it will inherit the configuration from the root logger, which can be simply retrieved using 'logging.getLogger(name)'.
The class doesn't require the logger configuration to be present in the same module where the logger is used. This allows for a centralized logger configuration.
Example
To configure the root logger and then retrieve a module-specific logger:
TomedaLogging()
logger = logging.getLogger(__name__)
To configure a logger with a custom name, specify a filename for the log file, set the log level for file logging to WARNING, and disable console logging:
TomedaLogging(
"my_special_logger",
file_name="my_special_logger.log",
file_level=logging.WARNING,
console_level=None
)
logger = logging.getLogger("my_special_logger")
Source code in tomeda/tomeda_logging.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
set_console_level
set_console_level(level: int | None) -> None
Set the log level for the console handler.
If the level is set to None, the console handler will be removed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
level |
int | None
|
The log level. If it's None, the console handler will be removed. |
required |
Source code in tomeda/tomeda_logging.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
set_file_level
set_file_level(level: int | None) -> None
Set the log level for the file handler.
If the level is set to None, the file handler will be removed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
level |
int | None
|
The log level. If it's None, the file handler will be removed. |
required |
Source code in tomeda/tomeda_logging.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
TraceLogger
Bases: Logger
Extended Logger class that introduces a new logging level TRACE
.
The TRACE
level is situated below the DEBUG
level and is
represented by the numeric value 5. A corresponding method trace
is provided to log messages at this level.
TRACE
class-attribute
instance-attribute
TRACE = 5
trace
trace(msg: Any, *args: Any, **kwargs: Any) -> None
Log a message with a severity TRACE
on this logger.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
Any
|
The message to be logged. It can be any Python structure
that has a |
required |
args |
Any
|
Variable argument list that gets formatted with |
()
|
kwargs |
Any
|
Arbitrary keyword arguments that are interpreted according e.g.
to the |
{}
|
Source code in tomeda/tomeda_logging.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|