我在Django中为Info level log添加了以下设置。
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,
},
},
}
如果我将此级别更改为Debug,那么它可以正常工作,但是当我将其更改为INFO Level时,日志将不会写入文件。有人可以帮忙吗?
答案 0 :(得分:0)
尝试以下设置:
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
},
}
}
在您的代码中:
import logging
logger = logging.getLogger(__name__)
logger.info("My name is Info")
示例输出:
2017-05-25 04:56:38,912 [INFO] api.views: API - initiate_scan view, new object created. Uid: cf3822b261790186297d30e4f5b448b2