简体   繁体   English

带有dictConfig的Python TimedRotatingFileHandler

[英]Python TimedRotatingFileHandler with dictConfig

I am trying to implement TimedRotatingFileHandler with dictConfig 我想实现TimedRotatingFileHandlerdictConfig

Here is the code: 这是代码:

LOG_SETTINGS = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'default': {
            'format': '%(asctime)s: %(name)s: #%(lineno)d: %(levelname)s - %(message)s'
        }
    },
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'formatter': 'default',
            'filename': logfile_name,
            'when': 'midnight',
            'interval': 1,
            'backupCount': 5
        }
    },
    'loggers': {
        ' ': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True
        },
    }
}

logfile_name = $HOME/.local/share/app/file.log logfile_name = $HOME/.local/share/app/file.log

Loading dictionary with logging.config.dictConfig(LOG_SETTINGS) 使用logging.config.dictConfig(LOG_SETTINGS)加载字典

Writing to file: logging.debug("Some text") 写入文件: logging.debug("Some text")

This creates a file named file.log , but the file is completely empty. 这将创建一个名为file.log的文件,但该文件完全为空。 I got no errors/warnings. 我没有错误/警告。

I visited Basic logging dictConfig in Python . 我访问了Python中的Basic logging dictConfig File seems quite similar. 文件似乎很相似。

Where did I go wrong? 我哪里做错了? Using Python 3.6 使用Python 3.6

Try that: 试试看:

import logging  # there we import logging module
logger = logging.getLogger(' ')  # there we get logger by name from settings
logger.debug('yolo')  # there we test logging with custom message

UPDATE: In my company we use that always: 更新:在我的公司中,我们始终使用以下方法:

logger = logging.getLogger(__name__)

with define logger: 使用定义记录器:

'loggers': {
    'app.views': {
        'handlers': ['handler_name'],
        'level': os.getenv('ERROR', 'INFO'), # needed level
        'propagate': True,
    },

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

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