简体   繁体   English

Python记录:dictConfig

[英]Python Logging: dictConfig

I am trying to use a config file for configure Python Logging, but also adding handlers after the dict config has been loaded. 我正在尝试使用配置文件来配置Python日志记录,但也在加载dict配置后添加处理程序。 SO my config file is like 所以我的配置文件就像

version: 1
formatters:
  default_formatter:
    format: '%(asctime)s : %(levelname)s : %(message)s'
    datefmt: '%d-%b-%Y %H:%M:%S'
  plain_formatter:
    format: '%(message)s'
handlers:  
  console_default_handler:
    class: logging.StreamHandler
    level: INFO
    formatter: default_formatter
    stream: ext://sys.stdout  
root:
  level: INFO
  handlers: [console_default_handler]

Then in the code - I do 然后在代码中 - 我做

log_config_dict=yaml.load(open(log_config_file, 'r'))
logging.config.dictConfig(log_config_dict)

And I want to add loggers in this way - 我希望以这种方式添加记录器 -

fhandler1=logging.FileHandler(log_file_name,mode="w")
fhandler1.setFormatter(log_config_dict['formatters']['plain_formatter'])
fhandler1.setLevel(logging.DEBUG)

This is not working. 这不起作用。 Is there any way I catch fetch values defined in the dictConfig for using them in my manual log configuration please? 有没有什么方法可以捕获dictConfig中定义的提取值,以便在我的手动日志配置中使用它们?

Thanks 谢谢

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import logging.handlers
from logging.config import dictConfig

logger = logging.getLogger(__name__)

DEFAULT_LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
}
def configure_logging(logfile_path):
    """
    Initialize logging defaults for Project.

    :param logfile_path: logfile used to the logfile
    :type logfile_path: string

    This function does:

    - Assign INFO and DEBUG level to logger file handler and console handler

    """
    dictConfig(DEFAULT_LOGGING)

    default_formatter = logging.Formatter(
        "[%(asctime)s] [%(levelname)s] [%(name)s] [%(funcName)s():%(lineno)s] [PID:%(process)d TID:%(thread)d] %(message)s",
        "%d/%m/%Y %H:%M:%S")

    file_handler = logging.handlers.RotatingFileHandler(logfile_path, maxBytes=10485760,backupCount=300, encoding='utf-8')
    file_handler.setLevel(logging.INFO)

    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.DEBUG)

    file_handler.setFormatter(default_formatter)
    console_handler.setFormatter(default_formatter)

    logging.root.setLevel(logging.DEBUG)
    logging.root.addHandler(file_handler)
    logging.root.addHandler(console_handler)



[31/10/2015 22:00:33] [DEBUG] [yourmodulename] [yourfunction_name():9] [PID:61314 TID:140735248744448] this is logger infomation from hello module

I think config log with function is more convenient. 我认为配置日志功能更方便。

Ohh I figure it out. 哦,我明白了。 What I need to do is 我需要做的是

formatter =logging.Formatter(log_config_dict['formatters']['plain_formatter']['format'])             
fhandler1.setFormatter(formatter)

So, I need to create a Formatter object. 所以,我需要创建一个Formatter对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM