def configure_stream_logger(logger, level=None): """ Configure the default stream handler for logging messages to the console. This also configures the basic logging environment for the application. :param str logger: The logger to add the stream handler for. :param level: The level to set the logger to, will default to WARNING if no level is specified. :type level: None, int, str :return: The new configured stream handler. :rtype: :py:class:`logging.StreamHandler` """ if level is None: level = constants.DEFAULT_LOG_LEVEL if isinstance(level, str): level = getattr(logging, level) root_logger = logging.getLogger('') for handler in root_logger.handlers: root_logger.removeHandler(handler) logging.getLogger(logger).setLevel(logging.DEBUG) console_log_handler = logging.StreamHandler() console_log_handler.setLevel(level) if its.on_linux: console_log_handler.setFormatter(color.ColoredLogFormatter('%(levelname)s %(message)s')) else: console_log_handler.setFormatter(logging.Formatter('%(levelname)-8s %(message)s')) logging.getLogger(logger).addHandler(console_log_handler) logging.captureWarnings(True) return console_log_handler
def _ex_config_logging(arguments, config, console_handler): """ If a setting is configured improperly, this will terminate execution via :py:func:`sys.exit`. :return: The path to a log file if one is in use. :rtype: str """ default_log_level = min( getattr(logging, (arguments.loglvl or constants.DEFAULT_LOG_LEVEL)), getattr(logging, config.get_if_exists('logging.level', 'critical').upper()) ) log_levels = ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'FATAL') file_path = None if config.has_option('logging.file'): options = config.get('logging.file') for _ in range(1): default_format = '%(asctime)s %(name)-50s %(levelname)-8s %(message)s' if isinstance(options, dict): # new style if not options.get('enabled', True): break if 'path' not in options: color.print_error('logging.file is missing required key \'path\'') sys.exit(os.EX_CONFIG) if 'level' not in options: color.print_error('logging.file is missing required key \'level\'') sys.exit(os.EX_CONFIG) file_path = options['path'] formatter = logging.Formatter(options.get('format', default_format)) if not options['level'].upper() in log_levels: color.print_error('logging.file.level is invalid, must be one of: ' + ', '.join(log_levels)) sys.exit(os.EX_CONFIG) log_level = getattr(logging, options['level'].upper()) root = options.get('root', '') elif isinstance(options, str): # old style file_path = options formatter = logging.Formatter(default_format) log_level = default_log_level root = '' else: break file_handler = logging.FileHandler(file_path) file_handler.setFormatter(formatter) logging.getLogger(root).addHandler(file_handler) file_handler.setLevel(log_level) if config.has_option('logging.console'): options = config.get('logging.console') for _ in range(1): if isinstance(options, dict): # new style if not options.get('enabled', True): break if 'format' in options: console_handler.setFormatter(color.ColoredLogFormatter(options['format'])) if arguments.loglvl is None and 'level' in options: log_level = str(options.get('level', '')).upper() if log_level not in log_levels: color.print_error('logging.console.level is invalid, must be one of: ' + ', '.join(log_levels)) sys.exit(os.EX_CONFIG) console_handler.setLevel(getattr(logging, log_level)) elif isinstance(options, str): # old style console_handler.setLevel(default_log_level) return file_path