Logger¶
ColoredFormatter
¶
Bases: Formatter
Logger class formatting messages in a specific way with colors interpretable by the terminal/console according to the log level:
DEBUG
andINFO
: greyWARNING
: yellowERROR
: redCRITICAL
: bold red
Message is formatted like this:
timestamp [log_level] current_flow_name - filename:LOC - actual_message
format(record)
¶
Format the given record
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record |
LogRecord
|
Record passing through the formatter. See Python logging for more info. |
required |
Returns:
Type | Description |
---|---|
str
|
The formatted text. |
Logger
¶
Single Logger object to handle Python logging within OneCode projects.
The default logging level is INFO. See set_level()
to change it.
Use the static methods debug()
, info()
,
warning()
, error()
, and
critical()
to conveniently log your messages with the
corresponding logging level.
By default, the ColoredFormatter is used. You may add other logging handlers using
add_handler()
, for instance to redirect logs to a file.
Example
import logging
from onecode import Logger
Logger().set_level(logging.DEBUG)
Logger.debug("debug")
Logger.info("info")
Logger.warning("warning")
Logger.error("error")
Logger.critical("critical")
_flush()
staticmethod
¶
Force flush to stdout if ConfigOption.FLUSH_STDOUT
is True. See
Project.config for more information.
add_handler(handler=None, root_logger=True)
¶
Add an extra handler in addition to the default console stream one. Nothing is done if handler is None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
Optional[Handler]
|
New handler to add. |
None
|
root_logger |
bool
|
If True, add the handler at the root logging, otherwise
as a child of the |
True
|
Example
import logging
from onecode import Logger
handler = logging.FileHandler("debug.log")
Logger().add_handler(handler)
Logger.error('oops!') # will print to console in red and to file "debug.log"
critical(msg)
staticmethod
¶
Convenience function to log a critical message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
Any
|
Message to log. |
required |
debug(msg)
staticmethod
¶
Convenience function to log a debug message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
Any
|
Message to log. |
required |
error(msg)
staticmethod
¶
Convenience function to log an error message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
Any
|
Message to log. |
required |
info(msg)
staticmethod
¶
Convenience function to log an info message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
Any
|
Message to log. |
required |
logger(stacklevel=1)
¶
Get the Python Logger object corresponding to the given stack level. Preferentially use
convenience methods debug()
, info()
,
warning()
, error()
, and
critical()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stacklevel |
int
|
Number of hops back in the function call stack to tie the logger to. By default, it is the last function calling this logger() method. |
1
|
Returns:
Type | Description |
---|---|
Logger
|
Python Logger object. |
reset(root_logger=True)
¶
Remove all added handlers attached to the OneCode logger and optionally
the root logger if specified (see logging.removeHandler()
for more info).
OneCode logger is then reset to the default console stream handler with the
ColoredFormatter with INFO
level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root_logger |
bool
|
If True, remove the handlers from the root logger too, in addition to removing the handlers from OneCode logger. |
True
|
set_level(level)
¶
Set the OneCode logger level. Default logging is INFO.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
level |
int
|
Numerical value to set the logging level to. See logging levels for more information. |
required |
warning(msg)
staticmethod
¶
Convenience function to log a warning message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
Any
|
Message to log. |
required |