Source code for autoflow.utils.logging_

# -*- encoding: utf-8 -*-
import inspect
import logging
import logging.config
import os

import yaml


[docs]def setup_logger(output_file=None, logging_config=None): # logging_config must be a dictionary object specifying the configuration # for the loggers to be used in auto-sklearn. if logging_config is not None: if output_file is not None: logging_config['handlers']['file_handler']['filename'] = output_file logging.config.dictConfig(logging_config) else: with open(os.path.join(os.path.dirname(__file__), 'logging.yaml'), 'r') as fh: logging_config = yaml.safe_load(fh) if output_file is not None: logging_config['handlers']['file_handler']['filename'] = output_file logging.config.dictConfig(logging_config)
def _create_logger(name): return logging.getLogger(name)
[docs]def get_logger(name): # todo: 判断是类还是实例 if isinstance(name, str): pass elif inspect.isclass(name): name = name.__module__ + "." + name.__name__ else: name = name.__module__ + "." + name.__class__.__name__ logger = PickableLoggerAdapter(name) return logger
[docs]class PickableLoggerAdapter(object): def __init__(self, name): self.name = name self.logger = _create_logger(name) def __getstate__(self): """ Method is called when pickle dumps an object. Returns ------- Dictionary, representing the object state to be pickled. Ignores the self.logger field and only returns the logger name. """ return {'name': self.name} def __setstate__(self, state): """ Method is called when pickle loads an object. Retrieves the name and creates a logger. Parameters ---------- state - dictionary, containing the logger name. """ self.name = state['name'] self.logger = _create_logger(self.name)
[docs] def debug(self, msg, *args, **kwargs): self.logger.debug(msg, *args, **kwargs)
[docs] def info(self, msg, *args, **kwargs): self.logger.info(msg, *args, **kwargs)
[docs] def warning(self, msg, *args, **kwargs): self.logger.warning(msg, *args, **kwargs)
[docs] def error(self, msg, *args, **kwargs): self.logger.error(msg, *args, **kwargs)
[docs] def exception(self, msg, *args, **kwargs): self.logger.exception(msg, *args, **kwargs)
[docs] def critical(self, msg, *args, **kwargs): self.logger.critical(msg, *args, **kwargs)
[docs] def log(self, level, msg, *args, **kwargs): self.logger.log(level, msg, *args, **kwargs)
[docs] def isEnabledFor(self, level): return self.logger.isEnabledFor(level)