def add_logger_handlers(debug=False, logfile=None): """ Create the logger and attach the handlers. :param debug: the level of the messages that we should log :type debug: bool :param logfile: the file name of where we should to save the logs :type logfile: str :return: the new logger with the attached handlers. :rtype: logging.Logger """ # TODO: get severity from command line args if debug: level = logging.DEBUG else: level = logging.WARNING # Create logger and formatter logger = logging.getLogger(name='leap') logger.setLevel(level) log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' formatter = logging.Formatter(log_format) # Console handler console = logging.StreamHandler() console.setLevel(level) console.setFormatter(formatter) silencer = log_silencer.SelectiveSilencerFilter() console.addFilter(silencer) logger.addHandler(console) logger.debug('Console handler plugged!') # LEAP custom handler leap_handler = LeapLogHandler() leap_handler.setLevel(level) leap_handler.addFilter(silencer) logger.addHandler(leap_handler) logger.debug('Leap handler plugged!') # File handler if logfile is not None: logger.debug('Setting logfile to %s ', logfile) fileh = logging.FileHandler(logfile) fileh.setLevel(logging.DEBUG) fileh.setFormatter(formatter) fileh.addFilter(silencer) logger.addHandler(fileh) logger.debug('File handler plugged!') return logger
def add_logger_handlers(debug=False, logfile=None): """ Create the logger and attach the handlers. :param debug: the level of the messages that we should log :type debug: bool :param logfile: the file name of where we should to save the logs :type logfile: str :return: the new logger with the attached handlers. :rtype: logging.Logger """ # TODO: get severity from command line args if debug: level = logging.DEBUG else: level = logging.WARNING # Create logger and formatter logger = logging.getLogger(name='leap') logger.setLevel(level) log_format = ('%(asctime)s - %(name)s:%(funcName)s:L#%(lineno)s ' '- %(levelname)s - %(message)s') formatter = logging.Formatter(log_format) # Console handler try: import coloredlogs console = coloredlogs.ColoredStreamHandler(level=level) except ImportError: console = logging.StreamHandler() console.setLevel(level) console.setFormatter(formatter) using_coloredlog = False else: using_coloredlog = True silencer = log_silencer.SelectiveSilencerFilter() console.addFilter(silencer) logger.addHandler(console) logger.debug('Console handler plugged!') # LEAP custom handler leap_handler = LeapLogHandler() leap_handler.setLevel(level) leap_handler.addFilter(silencer) logger.addHandler(leap_handler) logger.debug('Leap handler plugged!') # File handler if logfile is not None: logger.debug('Setting logfile to %s ', logfile) fileh = logging.FileHandler(logfile) fileh.setLevel(logging.DEBUG) fileh.setFormatter(formatter) fileh.addFilter(silencer) logger.addHandler(fileh) logger.debug('File handler plugged!') if not using_coloredlog: replace_stdout_stderr_with_logging(logger) return logger