模块化日志记录不会写入文件-Python

时间:2020-06-03 16:41:40

标签: python logging

我有写入日志配置的文件“ a.py”。我刚刚导入日志记录并写入日志的另一个文件“ b.py”,它会创建一个空文件,但无法在其中写入内容。 这是来自两个文件的代码。 请告诉我我在哪里缺少什么。谢谢

“ a.py”

import logging.config
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'default': {
            'format': '%(asctime)s %(pathname)s:%(lineno)d %(message)s',
        },
    },
    'handlers': {
        'default': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'rror.log',
            'backupCount': 2,
            'formatter': 'default',
        },
        'information': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'information.log',
            'backupCount': 2,
            'formatter': 'default',
        },
    },
    'loggers': {
        'logger1': {
            'handlers': ['default'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'logger2': {
            'handlers': ['information'],
            'level': 'INFO',
            'propagate': False,
        },
    },
}

logging.config.dictConfig(LOGGING)

“ b.py”

import logging

logging.getLogger('logger1').info("hey there!")
logging.getLogger('logger2').debug("hey logger2")

1 个答案:

答案 0 :(得分:1)

将“ b.py”更改为此:

from a import logging

logging.getLogger('logger1').debug("hey there!")
logging.getLogger('logger2').info("hey logger2")