climate_ref_core.logging
#
Logging utilities
The REF uses loguru, a simple logging framework. The log level and format are configured via the REF configuration file.
DEFAULT_LOG_FORMAT = '<green>{time:YYYY-MM-DD HH:mm:ss.SSS Z}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan> - {extra[execution_prefix]}<level>{message}</level>'
module-attribute
#
Default log format used by the REF
{extra[execution_prefix]} is [<execution slug>] for records logged during an execution,
and empty otherwise.
EXECUTION_LOG_FILENAME = 'out.log'
module-attribute
#
Filename for the execution log.
This file is written via climate_ref_core.logging.redirect_logs.
VERBOSE_LOG_FORMAT = '<green>{time:YYYY-MM-DD HH:mm:ss.SSS Z} e{elapsed}s</green> | <level>{level: <8}</level> | {process.name}:{process.id} | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - {extra[execution_prefix]}<level>{message}</level>'
module-attribute
#
The verbose log format is used for debugging and development.
This is the format that is used when writing the log messages to file for later debugging. It contains information about the process and function that the log message was generated in.
add_log_handler(**kwargs)
#
Add a log sink to the logger to capture logs.
This is useful for testing purposes, to ensure that logs are captured correctly.
Source code in packages/climate-ref-core/src/climate_ref_core/logging.py
capture_logging()
#
Capture logging from the standard library and redirect it to Loguru
This also sets the execution_prefix extra field used by the REF log formats.
Note that this replaces the root logger, so any other handlers attached to it will be removed.
Source code in packages/climate-ref-core/src/climate_ref_core/logging.py
execution_log_only()
#
Get a logger whose records go to the execution log but not the console
Use this for detail that is too bulky for collected logs, such as a failed command's full output. Outside redirect_logs the records are logged as usual.
Source code in packages/climate-ref-core/src/climate_ref_core/logging.py
initialise_logging(level, format, log_directory, serialize=False)
#
Initialise the logging for the REF
This sets up the default log handler and configures the REF logger.
When serialize is True the console handler writes one JSON object per record,
so a multi-line traceback stays a single entry for log aggregators.
Source code in packages/climate-ref-core/src/climate_ref_core/logging.py
redirect_logs(definition, log_level)
#
Temporarily redirect log output to a file.
The console handler, if configured, only shows warnings and errors while redirected, so failures still reach stdout-based log collection without the diagnostic's full output.
This also writes some common log messages
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
definition
|
ExecutionDefinition
|
Diagnostic definition to capture logging for |
required |
log_level
|
str
|
Log level as a string e.g. INFO, WARNING, DEBUG. This log level will dictate what logs will be sent to disk The logger will also be reset to this level after leaving the context manager. |
required |
Source code in packages/climate-ref-core/src/climate_ref_core/logging.py
remove_log_handler()
#
Remove the default log handler from the logger.
This is useful for cleaning up after tests or when changing logging configurations.
The previously used logger kwargs are kept in logger.default_handler_kwargs if the
logger should be readded later