2

以下是我在 django settings.py文件中使用的日志记录片段。所有的 GET、POST 请求都被写入日志,但是当我写logger.info("print something") 时,它没有在控制台和日志文件中打印/捕获

请建议一种解决方法来捕获logger.info()日志

视图.py

import logging

logger = logging.getLogger(__name__)

def custom_data_generator(request):
    logger.info("print somethig") # NOT GETTING CAPTURED IN LOG FILE
    return somethig

settings.py(在 settings.py 文件中的DEBUG = TrueDEBUG_MODE = False )

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
    'simple': {
        'format': '[%(asctime)s] %(levelname)s|%(name)s|%(message)s',
        'datefmt': '%Y-%m-%d %H:%M:%S',
    },
},
'handlers': {
    'applogfile': {
        'level': 'DEBUG',
        'class': 'logging.handlers.RotatingFileHandler',
        'filename': '/home/mahesh/Documents/refactor/unityapp/unity/media/myproject.log',
        'backupCount': 10,
        'formatter': 'simple',
    },
    'console': {
        'level': 'DEBUG',
        'class': 'logging.StreamHandler',
        'formatter': 'simple'
    }
},
'loggers': {
    'django': {
        'handlers': ['applogfile', 'console'],
        'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
    }
}
}

生成的日志数据如下

[2020-07-07 11:43:25] ERROR|django.server|"GET /a11y/project-dashboard/?

refnum=ACGLOBAL&env_id=4 HTTP/1.1" 500 92016
[2020-07-07 12:05:21] INFO|django.server|"GET /admin/ HTTP/1.1" 200 59501
[2020-07-07 12:05:21] INFO|django.server|"GET /admin/ HTTP/1.1" 200 59501
[2020-07-07 12:05:21] INFO|django.server|"GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
[2020-07-07 12:05:21] INFO|django.server|"GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[2020-07-07 12:05:21] INFO|django.server|"GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
[2020-07-07 12:05:26] INFO|django.server|"GET /admin/accessibility/axe_json/ HTTP/1.1" 200 1886434
[2020-07-07 12:05:27] INFO|django.server|"GET /admin/jsi18n/ HTTP/1.1" 200 3223
[2020-07-07 12:05:27] INFO|django.server|"GET /static/admin/js/vendor/jquery/jquery.js HTTP/1.1" 200 280364
[2020-07-07 12:05:27] INFO|django.server|"GET /static/admin/js/vendor/xregexp/xregexp.js HTTP/1.1" 200 128820
[2020-07-07 12:05:34] INFO|django.server|"GET /admin/accessibility/axe_json/?page_id=https%3A%2F%2Fjobs.chegg.com%2Fapplythankyou HTTP/1.1" 200 1868950
[2020-07-07 12:05:35] INFO|django.server|"GET /admin/jsi18n/ HTTP/1.1" 200 3223
4

2 回答 2

2

这可能是因为您的视图模块没有设置日志记录级别,因此它将继承根记录器的默认级别WARNING. 如果您添加一个root级别为 的条目INFO,类似于文档中的示例,您应该会看到来自其他模块的消息。或者,您可以在特定模块层次结构的键下指定记录器名称loggers,无论是什么。(您的示例仅覆盖层次结构WARNING中模块的django级别,即 Django 本身中的代码。)

于 2020-07-07T22:41:56.140 回答
0

In the example you provided, you are only setting the log level for the 'django' logger. That logger controls the log level for django.request, django.server, django.templateetc. If you are looking to set the log level for your own apps, you need to set the log level for that particular app's logger, or set it for all apps using a configuration like below

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console_handler': {
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        # More info on '' (unnamed) loggers at the end of this comment
        '': {
            'level': 'INFO',
            'handlers': ['console_handler'],
        },
    },

}

The '' (unnamed) logger will process records from all loggers. More info here: https://docs.djangoproject.com/en/dev/howto/logging/#configure-a-logger-mapping

于 2021-12-14T03:39:43.500 回答