简体   繁体   English

Django应用日志级别信息未写入文件

[英]Django App Log level info not getting write into file

I have added below setting in Django for Info level log. 我在Django的Info级日志中添加了以下设置。

logger = logging.getLogger(__name__)
logging.basicConfig(LOGGING=settings.LOGGING)

LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
    'verbose': {
        'format': '%(asctime)s %(levelname)s %(message)s',
    },
},
'handlers': {
    'file': {
        'class': 'logging.handlers.TimedRotatingFileHandler',
        'formatter': 'verbose',
        'level': logging.INFO,
        'filename': '/logs/django/api.log',
        'interval': 1,
        'when': 'midnight',
        'encoding': 'utf8'
    },
},
'loggers': {
    'django': {
        'handlers': ['file'],
        'level': logging.INFO,
        'propagate': True,
    },
},
}

If I change this level to Debug then it works properly but when I change it to INFO Level then the log will not be written into the file. 如果我将此级别更改为“调试”,则它可以正常工作,但是当我将其更改为“信息”级别时,该日志将不会写入文件中。 Can somebody help? 有人可以帮忙吗?

Try the following settings: 尝试以下设置:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
        },
    },
    'handlers': {
        'default': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': 'test.log',
            'maxBytes': 1024*1024*5, # 5 MB
            'backupCount': 5,
            'formatter':'standard',
        },
        'request_handler': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': 'django_request.log',
            'maxBytes': 1024*1024*5, # 5 MB
            'backupCount': 5,
            'formatter':'standard',
        },
    },
    'loggers': {
        '': {
            'handlers': ['default'],
            'level': 'DEBUG',
            'propagate': True
        },
        'django.request': {
            'handlers': ['request_handler'],
            'level': 'DEBUG',
            'propagate': False
        },
    }
}

In your code: 在您的代码中:

import logging

logger = logging.getLogger(__name__)

logger.info("My name is Info")

Sample output: 样本输出:

2017-05-25 04:56:38,912 [INFO] api.views: API - initiate_scan view, new object created. Uid: cf3822b261790186297d30e4f5b448b2

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

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