Package: mloggers
MLoggersThis package offers a collection of loggers well-suited for machine learning experiments.Getting startedYou can download the package viapip install mloggers. Python version $\geq$ 3.10 is required. Dependencies include:aenumnumpytermcolorwandb(for integration with Weights & Biases)omegaconf(for integration with Hydra via Weights & Biases)UsageExample usage (with Hydra integration):importtimeimporthydrafromomegaconfimportDictConfigfrommloggersimportConsoleLogger,MultiLogger,WandbLogger@hydra.main(version_base=None,config_path="configs",config_name="train")defmain(config:DictConfig):run_id=str(int(time.time()))# Create a multi-loggerlogger=MultiLogger([ConsoleLogger(),WandbLogger(config.project_name,config.group_name,config.experiment_name+"_"+run_id,config,),],default_mask=[WandbLogger],)# Run an experimentlogger.info("Starting the experiment")try:# `run_experiment` returns a dictionary of resultsresults=run_experiment(config,logger)exceptExceptionase:logger.error({"Exception occurred during training":e})results={}# Log the experiment resultslogger(results,mask=[ConsoleLogger])Built-in loggersAt this moment, the built-in loggers are:Filelogger: records logs to a file.ConsoleLogger: records logs to the console.WandbLogger: sends logs to a Weights & Biases project; requires an API key.MultiLogger: aggregates any/all of the above loggers to record the same messages through multiple channels in a singlelog()call.The available methods to log messages are:log(message, level): logs a message of a givenLogLevel(INFO,WARN,ERROR,DEBUGor a custom level).info(message): wrapper to calllog(message, LogLevel.INFO).warn(message): wrapper to calllog(message, LogLevel.WARN).error(message): wrapper to calllog(message, LogLevel.ERROR).debug(message): wrapper to calllog(message, LogLevel.DEBUG).In the case of theMultiLogger, the methods above have the additional optional argumentmask, which can be used to prevent the given message from being propagated through the masked loggers.MasksMasks are used by theMultiLoggerto filter loggers which are not supposed to record a given message. At the time of initialization, you can define a default mask to use for all messages for which a mask is not specified when callingMultiLogger.log(message, level, mask)or the level-specific variants. To create a mask, simply pass as argument a list of the class references for the loggers you would like to mask out.Progress barsYou can make use of a pre-configured wrapper of the progress bars provided by the packagerich.progress. The wrapper is provided via the functionmloggers.progress.log_progress. Example usage:importtimefrommloggers.progressimportlog_progressfor_inlog_progress(range(100)):time.sleep(0.1)Customized loggersYou can extend the base classLoggerin order to create a custom logger to suit your own needs. Make sure to implement all abstract methods.Customized log levelsYou can register new log levels by usingregister_level(level, color). Once you register a level"MyLevel", you can use it aslogger.log(message, LogLevel.MYLEVEL). The methodlogalso supports a string as a level, which will be upper-cased and given a default color; the level can also beNone, which will simply log the message as a stand-alone.