Logging#
Loguru is a library that simplifies logging and has useful features.
Log levels:
debug: Detailed information useful during developmentinfo: High level operation of the codewarning: Might be an issueerror: Problem that needs attentioncritical: Fatal error
loguru#
from loguru import logger
def fit_model(model, params):
logger.debug(f"Using params: {params}")
logger.info(f'Starting training.')
if params['learning_rate'] is None:
logger.warning("learning_rate param not specified, using 3e-4.")
if hasattr(model, 'fit'):
model.fit()
else:
logger.error("The model provided does not have a fit() method.")
Logging to a File#
By default, Loguru will output to the console (stdout). The logger.add() method is use to save logs to a file.
logger.add("file.log"): Save log data to “file.log” path. Parameters of note:
retentionpamameter controls how long we keep logs. E.g.retention="1 HOUR"rotation: Rotates log file when it exceeds a certain size. E.g.rotation="10 MB"
Function Decoratoring:#
@logger.catch allows us to automatically log the function’s entry, exit, and exceptions. Parameters:
reraise: If set to True, the decorator will re-raise the exception after logging it. This allows the program to continue handling the error as it normally would.level: Specify the log level (e.g., “DEBUG”, “ERROR”, etc.) at which the exception should be logged.onerror: Provide a fallback function to execute when an exception occurs.
from loguru import logger
@logger.catch(onerror=lambda _: print("An error occurred, but we are moving on."))
def run_error():
return 1 / 0
# run_error() # This will raise ZeroDivisionError and log it