Customised Logger#
Triumvirate provides a customised logging.Logger
, which can
be accessed from the logger
module.
from triumvirate.logger import setup_logger
Verbosity#
When setting up the logger, one could choose the verbosity level by
passing an integer or logging level variable. By default, the logging level is
set at logging.INFO
(see
Logging Levels).
trv_logger = setup_logger(log_level=20) # HINT: logging.INFO = 20
Log format#
This logger behaves like a typical Python logger but with customised log
message format [{local-timestamp}<YYYY-MM-DD HH:MM:SS> ({elapsed-time}<+HH:MM:SS>) {level}] {message}
.
It also includes a C++ state indicator which is used to inform the user that the Cython-wrapped C++ backend is running.
trv_logger.info("This is a logged INFO-level message.")
trv_logger.debug("This is an unlogged DEBUG-level message.")
trv_logger.info(
"This log entry indicates C++ backend is running.",
cpp_state=True
)
[2023-10-03 16:13:22 (+00:00:00) INFO] This is a logged INFO-level message.
[2023-10-03 16:13:22 (+00:00:00) INFO] This log entry indicates C++ backend is running. (in C++)
Warning capture#
The logger also captures warning messages and records them in a customised format.
# DEMO: capture warning with the logger.
import warnings
warnings.warn("This is a warning message captured as a log entry.")
[2023-10-03 16:13:22 (+00:00:00) WARN] This is a warning message captured as a log entry. (/var/folders/1_/q2lsr5v97z785g1fy4fh40sm0000gn/T/ipykernel_18426/2246887521.py:3:UserWarning --> "warnings.warn("This is a warning message captured as a log entry.")")
C++ backend logger#
Some callables in Triumvirate calls C++ routines in the backend, which
has its own logger. Any log-like entry in stdout
with the format
[{local-timestamp}<YYYY-MM-DD HH:MM:SS> ({elapsed-time}<+HH:MM:SS>) {level} C++] {message}
(note the inclusion of ‘C++’) is a log message recorded by that logger.
By default this logger is alway present, but one can control its
verbosity/logging level with the verbose
parameter/attribute in a
ParameterSet
object passed to the
callable.
Passing as an argument#
Now, whenever a callable in Triumvirate accepts a logger
argument, you
can pass trv_logger
above to it. You can modify it further to suit your needs
(see logging
).