Logging#
Logging is done all trough the package execution. Two loggers are defined in the classes :
DensitySplit
in thecompute_Densitysplit
method (onDEBUG
level)CorrHOD
trough the classes.
The logging is done using the standard python logging module, usually on the INFO
level
(except for some specific messages not really useful)
In the run_all()
method, the logging is used to print the progress of the computation and
some useful informations.
Tip
This is done to avoid the use of the print
function, which has a buffering system that
will not print the messages in the correct order or in real time.
To follow the progress of the correlation functions, the setup_logging()
from the mockfactory
package can be used. However, this method will format every log message, and this might not be
the desired behavior.
Therefore, the create_logger()
method from the logging
module can be used to initialize a
logger that will not be formatted by the setup_logging()
method.
Tip
The create_logger()
method is a wrapper around the logging.getLogger()
method, and
will return the same logger if it has already been initialized.
It is also possible to get the same formatting as the setup_logging()
method by setting the
propagate
attribute of the logger to True
.
Recommended logger initialization#
from CorrHOD import create_logger
from mockfactory import setup_logging
# Get a logger object
setup_logging() # Initialize the logging for all the loggers that will not have a handler
logger = create_logger('CorrHOD', level='debug', stream=sys.stdout)
API#
- CorrHOD.logging.log_newline(self, how_many_lines=1)[source][source]#
Add a blank to the logger. Accessed as a method of the logger object with types.MethodType(log_newline, logger)
- CorrHOD.logging.create_logger(name: str, level=logging.INFO, stream=sys.stdout, filename: Optional[str] = None, filemode: str = 'w', propagate=False)[source][source]#
Will get or create the loger with the given name, and add a method to the logger object to output a blank line. A handler is created, with the given level and stream (or file output).
- Parameters:
name (str) – Name of the logger
level (optional) – Level of the logger and handler. Can be a string (‘info’, ‘debug’, ‘warning’) or a logging level (logging.INFO, logging.DEBUG, logging.WARNING). Defaults to logging.INFO
stream (optional) – Stream to which the logger will output. Defaults to sys.stdout
filename (str, optional) – Path to the file to which the logger will output. Defaults to None
filemode (str, optional) – Mode to open the file. Defaults to ‘w’
propagate (bool, optional) – Whether to propagate the logs to the root logger. Defaults to False Warning : If sets to False, the logs will not be propagated to the root logger, and will not be output by the root logger. If the root logger outputs to a file, the logs will not be saved in the file, unless the logger has the same output file. However, if propagate is True, the logs will be output twice if the root has a different handler (once by the logger, once by the root logger)
- Returns:
logger – Logger object
- Return type:
logging.Logger