예제 #1
0
def setup_logging(options, conf):
    """
    Sets up the logging options for a log with supplied name

    :param options: Mapping of typed option key/values
    :param conf: Mapping of untyped key/values from config file
    """
    if options.get('log_config', None):
        # Use a logging configuration file for all settings...
        if os.path.exists(options['log_config']):
            logging.config.fileConfig(options['log_config'])
            return
        else:
            raise RuntimeError("Unable to locate specified logging "\
                               "config file: %s" % options['log_config'])

    # If either the CLI option or the conf value
    # is True, we set to True
    debug = options.get('debug') or conf.get('debug', False)
    debug = debug in [True, "True", "1"]
    verbose = options.get('verbose') or conf.get('verbose', False)
    verbose = verbose in [True, "True", "1"]
    root_logger = logging.root
    if debug:
        level = logging.DEBUG
    elif verbose:
        level = logging.INFO
    else:
        level = logging.WARNING
    root_logger.setLevel(level)

    # Set log configuration from options...
    # Note that we use a hard-coded log format in the options
    # because of Paste.Deploy bug #379
    # http://trac.pythonpaste.org/pythonpaste/ticket/379
    log_format = options.get('log_format', DEFAULT_LOG_FORMAT)
    log_date_format = options.get('log_date_format', DEFAULT_LOG_DATE_FORMAT)
    formatter = logging.Formatter(log_format, log_date_format)

    logfile = options.get('log_file')
    if not logfile:
        logfile = conf.get('log_file')

    if logfile:
        logdir = options.get('log_dir')
        if not logdir:
            logdir = conf.get('log_dir')
        if logdir:
            logfile = os.path.join(logdir, logfile)
        logfile = logging.FileHandler(logfile)
        logfile.setFormatter(formatter)
        root_logger.addHandler(logfile)
        # Mirror to console if verbose or debug
        if debug or verbose:
            add_console_handler(root_logger, logging.INFO)
    else:
        handler = logging.StreamHandler(sys.stdout)
        handler.setFormatter(formatter)
        root_logger.addHandler(handler)
예제 #2
0
def setup_logging(options, conf):
    """
    Sets up the logging options for a log with supplied name

    :param options: Mapping of typed option key/values
    :param conf: Mapping of untyped key/values from config file
    """
    if options.get('log_config', None):
        # Use a logging configuration file for all settings...
        if os.path.exists(options['log_config']):
            logging.config.fileConfig(options['log_config'])
            return
        else:
            raise RuntimeError("Unable to locate specified logging "\
                               "config file: %s" % options['log_config'])

    # If either the CLI option or the conf value
    # is True, we set to True
    debug = options.get('debug') or conf.get('debug', False)
    debug = debug in [True, "True", "1"]
    verbose = options.get('verbose') or conf.get('verbose', False)
    verbose = verbose in [True, "True", "1"]
    root_logger = logging.root
    root_logger.setLevel(
        logging.DEBUG if debug else
        logging.INFO if verbose else
        logging.WARNING)

    # Set log configuration from options...
    # Note that we use a hard-coded log format in the options
    # because of Paste.Deploy bug #379
    # http://trac.pythonpaste.org/pythonpaste/ticket/379
    log_format = options.get('log_format', DEFAULT_LOG_FORMAT)
    log_date_format = options.get('log_date_format', DEFAULT_LOG_DATE_FORMAT)
    formatter = logging.Formatter(log_format, log_date_format)

    # grab log_file and log_dir from config; set to defaults of not already
    # defined
    logfile = options.get('log_file') or conf.get('log_file', DEFAULT_LOG_FILE)
    logdir = options.get('log_dir') or conf.get('log_dir', DEFAULT_LOG_DIR)

    # Add FileHandler if it doesn't exist
    logfile = os.path.abspath(os.path.join(logdir, logfile))
    handlers = [handler for handler in root_logger.handlers
                if isinstance(handler, FileHandler)
                and handler.baseFilename == logfile]

    if not handlers:
        logfile = logging.FileHandler(logfile)
        logfile.setFormatter(formatter)
        root_logger.addHandler(logfile)

    # Mirror to console if verbose or debug
    if debug or verbose:
        add_console_handler(root_logger, logging.DEBUG)
    else:
        add_console_handler(root_logger, logging.INFO)
예제 #3
0
def setup_logging(options, conf):
    """
    Sets up the logging options for a log with supplied name

    :param options: Mapping of typed option key/values
    :param conf: Mapping of untyped key/values from config file
    """
    if options.get('log_config', None):
        # Use a logging configuration file for all settings...
        if os.path.exists(options['log_config']):
            logging.config.fileConfig(options['log_config'])
            return
        else:
            raise RuntimeError("Unable to locate specified logging "\
                               "config file: %s" % options['log_config'])

    # If either the CLI option or the conf value
    # is True, we set to True
    debug = options.get('debug') or conf.get('debug', False)
    debug = debug in [True, "True", "1"]
    verbose = options.get('verbose') or conf.get('verbose', False)
    verbose = verbose in [True, "True", "1"]
    root_logger = logging.root
    root_logger.setLevel(logging.DEBUG if debug else logging.
                         INFO if verbose else logging.WARNING)

    # Set log configuration from options...
    # Note that we use a hard-coded log format in the options
    # because of Paste.Deploy bug #379
    # http://trac.pythonpaste.org/pythonpaste/ticket/379
    log_format = options.get('log_format', DEFAULT_LOG_FORMAT)
    log_date_format = options.get('log_date_format', DEFAULT_LOG_DATE_FORMAT)
    formatter = logging.Formatter(log_format, log_date_format)

    # grab log_file and log_dir from config; set to defaults of not already
    # defined
    logfile = options.get('log_file') or conf.get('log_file', DEFAULT_LOG_FILE)
    logdir = options.get('log_dir') or conf.get('log_dir', DEFAULT_LOG_DIR)

    # Add FileHandler if it doesn't exist
    logfile = os.path.abspath(os.path.join(logdir, logfile))
    handlers = [
        handler for handler in root_logger.handlers
        if isinstance(handler, FileHandler) and handler.baseFilename == logfile
    ]

    if not handlers:
        logfile = logging.FileHandler(logfile)
        logfile.setFormatter(formatter)
        root_logger.addHandler(logfile)

    # Mirror to console if verbose or debug
    if debug or verbose:
        add_console_handler(root_logger, logging.DEBUG)
    else:
        add_console_handler(root_logger, logging.INFO)