Library Reference
Setup functions
This group of functions helps you setup the logging in the base level. All are directed to work with scripting except for get_stream_logger, which is meant to be used in notebooks.
- log4data.setup_log_args(parser: ArgumentParser | None = None, return_args: bool = False) Namespace | None[source]
Adds logging related arguments to an argparse.ArgumentParser.
This function will add three arguments (
log-level,log-file-name, andlog-format) to the parser provided. If no parser is given, a new one is created. Ifreturn_argsis True, parse and return the arguments.- Parameters
- parserOptional[argparse.ArgumentParser] default None
The parser to which the arguments are added. If None, a new parser will be created.
- return_args(bool) default False
If True, parse the arguments and return the Namespace containing them.
- Returns
- Optional[argparse.Namespace]
The Namespace containing parsed arguments if return_args is True otherwise, None.
Note
The arguments added are:
--log-level(-lglv) [str]: Level at which logs will be shown.--log-file-name(-lgfn) [str]: File where logs will be written.--log-format(-lgfmt) [str]: Logging format. Default is:%(levelname)s - %(asctime)s - %(name)s - %(message)s
- log4data.setup_default_logger()[source]
Quick and easy way to setup the logging.basicConfig
level:
lg.INFOfilename:
exit_<YYYYMMDD>.logformat:
%(asctime)s - %(name)s - %(levelname)s - %(message)s
- log4data.setup_logger(level: int = 20, log_file_name: str = 'exit.log', log_format: str = '%(asctime)s - %(name)s - %(levelname)s - %(message)s', dynamic_date: bool = True)[source]
Configures the logging.basicConfig() taking into account the log_file_name. Level is set to INFO and format is set to the default:
%(asctime)s - %(name)s - %(levelname)s - %(message)s- Parameters
- log_file_name(str)
Sets the file where logs will be written to.
- dynamic_date(bool)
If True the name will be altered to add the date and result in a name like this:
<log_file_name>_<YYYYMMDD>.log
- log4data.setup_logger_from_args(args: Namespace)[source]
Configures the logging.basicConfig() taking into account the arguments passed in args.
args.log_levelsets the level of the loggerargs.log_file_namesets the file where logs will be written to. This name is taken, and the date is added, resulting in:<args.log_file_name>_<YYYYMMDD>.logargs.log_formatsets the format string for the handlerargs.add_dynamic_date
- Parameters
args : (argparse.Namespace)
- log4data.get_stream_logger(level: int = 20, log_format: str = '%(asctime)s - %(name)s - %(levelname)s - %(message)s') Logger[source]
Setup logging to make beautiful logging in notebooks or in the terminal.
This function returns a logger object that will print everything to the stream output. This is meant to be used in notebooks or in scripts ran from the terminal.
- Parameters
- level(int)
The logging level, default lg.INFO
- log_format(str)
The format of the messages. Set by default to library standard.
Injectors
This functions are decorators to add a specific logger to functions that take logger as an argument.
- log4data.inject_logger(func: Callable[[...], Any]) Callable[[...], Any][source]
A decorator that injects a logger into the decorated function.
This decorator modifies the function by adding a
loggerparameter automatically before calling the function. It retrieves a logger instance using the function’s module and name, which helps in tracking which function logged the messages.Note
The decorated function must be designed to accept a
loggerkeyword argument. This implementation does not handle the case where the function already has aloggerkeyword argument or uses *args and **kwargs in a way that conflicts with the automatic injection of the logger.- Parameters
- func(Callable[…, Any])
The function to decorate.
- Returns
- Callable
A wrapper function that adds the logger to
func‘s arguments.
- Example
@inject_logger() def process_data(data, logger=None): logger.info("Processing data") pass # call the function without passing a logger process_data(data)
- log4data.inject_named_logger(logger_name: str | None = None)[source]
A decorator that injects a logger into the decorated function, with a given name.
This decorator modifies the function by adding a
loggerparameter automatically before calling the function. It retrieves a logger instance using the passed argument logger_name, which helps in tracking.Note
The decorated function must be designed to accept a
loggerkeyword argument. This implementation does not handle the case where the function already has aloggerkeyword argument or uses *args and **kwargs in a way that conflicts with the automatic injection of the logger.- Parameters
- logger_name(Optional[str])
If logger_name is not None, the logger will have this name. Else the name will be root.
- Returns
- Callable
A wrapper function that adds the logger to
func‘s arguments.
- Example
@inject_named_logger("my_logger") def process_data(data, logger=None): logger.info("Processing data") pass # call the function without passing a logger process_data(data)
Monitoring
- log4data.setup_monitoring_args(parser: ArgumentParser | None = None, return_args: bool = False) Namespace | None[source]
Adds monitor related arguments to an argparse.ArgumentParser.
This function will add two arguments (
monitor-nameandmonitor-file-name) to the parser provided. If no parser is given, a new one is created. Ifreturn_argsis True, parse and return the arguments.- Parameters
- parser(argparse.ArgumentParser, None)
The parser to which the arguments are added. If None, a new parser will be created.
- return_args(bool)
If True, parse the arguments and return the Namespace containing them.
- Returns
- argparse.Namespace or None
The Namespace containing parsed arguments if return_args is True otherwise, None.
Note
The arguments added are:
--monitor-name(-mtn) [str]: The name of the monitor.--monitor-file-name(-mtfn) [str]: File where monitor logs will be written.
- log4data.setup_default_monitor(return_monitor: bool = False) Logger | None[source]
This function initializes a MonitorLogger with a default configuration.
Optionally, the function returns the monitor object by setting return_monitor to True. You can also get the monitor in other scopes by calling
logging.getLogger("monitor")Default configurations are:name: “monitor”
file:
logs/monitoring_exit_<YYYYMMDD>.loglog_level:
logging.INFO
- log4data.setup_monitor(monitor_name: str = 'monitor', level: int = 20, monitor_file: str = 'logs/monitoring_exit.log', dynamic_date: bool = True, return_monitor: bool = False) Logger | None[source]
This function initializes a monitor logger with the given configuration.
Optionally, the function returns the monitor object by setting return_monitor to True. You can also get the monitor in other scopes by calling
logging.getLogger(monitor_name). All parameters have default values- Parameters
- logger_name: (str)
The name of the MonitorLogger (subclass of Logger) object
- level: (int)
Level at wich monitor logs will be shown
- monitor_file: (str)
The file where logs will be stored
- dynamic_date: (bool)
Whether to add the date suffix after the monitor_file, obtaining a file like this:
<monitor_file>_<YYYYMMDD>.log
- log4data.setup_monitor_from_args(args: Namespace, return_monitor: bool = False) Logger | None[source]
Same as setup_monitor but uses an argparse.Namespace to define the configuration.
- log4data.inject_default_monitor(func: Callable[[...], Any]) Callable[[...], Any][source]
A decorator that injects a monitor logger into the decorated function.
This decorator modifies the function by adding a
monitorparameter automatically before calling the function. It retrieves the default monitor logger (with name “monitor”).Note
The decorated function must be designed to accept a ‘monitor’ keyword argument. This implementation does not handle the case where the function already has a ‘monitor’ keyword argument or uses *args and **kwargs in a way that conflicts with the automatic injection of the monitor.
- Parameters
- funcCallable
The function to decorate.
- Returns
- Callable
A wrapper function that adds the monitor to
func’s arguments.
- Example
@inject_default_monitor def process_data(data, monitor=None): monitor.info( "Processing data", key="data_value", value=data ) pass # call the function without passing a monitor process_data(data)
- log4data.inject_named_monitor(monitor_name: str)[source]
A decorator that injects a monitor logger into the decorated function, with a given name.
This decorator modifies the function by adding a
monitorparameter automatically before calling the function. It retrieves a monitor instance using the passed argument monitor_name, which helps in tracking.Note
The decorated function must be designed to accept a ‘monitor’ keyword argument. This implementation does not handle the case where the function already has a ‘monitor’ keyword argument or uses *args and *kwargs in a way that conflicts with the automatic injection of the logger.
The monitor must be initialized before injecting it. Else, the logger returned will be a base lg.Logger object with the default configuration.
- Parameters
- monitor_namestr
The name of the monitor to be used. Cant’t be an empty string.
- Returns
- Callable
A wrapper function that adds the logger to
func’s arguments.
- Examples
@inject_named_monitor("my_monitor") def process_data(data, monitor=None): monitor.info( "Processing data", key="data_value", value=data ) pass # call the function without passing a monitor logger process_data(data)