Logger¶
ColoredFormatter
¶
Bases: logging.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 |
logging.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[logging.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 |
str
|
Message to log. |
required |
debug(msg)
staticmethod
¶
Convenience function to log a debug message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
str
|
Message to log. |
required |
error(msg)
staticmethod
¶
Convenience function to log an error message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
str
|
Message to log. |
required |
info(msg)
staticmethod
¶
Convenience function to log an info message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
str
|
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 |
---|---|
logging.Logger
|
Python Logger object. |
reset()
¶
Remove all added handlers attached to the OneCode logger (see logging.removeHandler()
for more info) and reset to the default console stream handler with the
ColoredFormatter with INFO
level.
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 |
str
|
Message to log. |
required |