简体   繁体   English

记录不将记录消息写入日志文件但在控制台上打印

[英]Logging not writing logging messages to log file but printing on console

I am trying to learn logging module in Django.我正在尝试学习 Django 中的日志记录模块。 I want to write exceptions to the log file so that I can see them later.我想将异常写入日志文件,以便稍后查看。 Printing log messages on console is working fine but I am unable to write the same log messages to the log file.在控制台上打印日志消息工作正常,但我无法将相同的日志消息写入日志文件。 Below is my code.下面是我的代码。

My settings.py我的设置.py

LOGGING = {
    'version': 1,
    'loggers': {
        'django':{
            'handlers': ['file'] ,
            'level':'INFO'
        }
    },
    'handlers':{
        'file':{
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'debug.log',
        }
    }
}

My views.py我的观点.py

def Helloworld(request):
    try:
        s = HttpResponse("this is life")
        logging.debug("this is debug log - in hw page")
        logging.critical("this is critical log - in hw page")   
        logging.error("this is error log - in hw page")
    except Exception as e:
        print(e)
        logging.critical("this is critical log - in hw page - exception")
        logging.exception(e)
    return s

My console output我的控制台 output

Django version 3.0.6, using settings 'project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
CRITICAL:root:this is critical log - in hw page
ERROR:root:this is error log - in hw page
INFO:django.server:"GET /hw HTTP/1.1" 200 12

My output of log file我的 output 的日志文件

Watching for file changes with StatReloader
"GET /hw HTTP/1.1" 200 12
C:\Users\....\views.py changed, reloading.

    Watching for file changes with StatReloader
    Watching for file changes with StatReloader
    "GET /hw HTTP/1.1" 200 12
    "GET /hw HTTP/1.1" 200 12
    C:\Users\...\views.py changed, reloading.
    Watching for file changes with StatReloader
    C:\Users\...\views.py changed, reloading.
    Watching for file changes with StatReloader
    "GET /hw HTTP/1.1" 200 12

My Urls.py我的网址.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url('', include(router.urls)),
    url(r'^hw$', Helloworld),
    url('__debug__/', include(debug_toolbar.urls)),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

Please let me know where I am going wrong.请让我知道我哪里出错了。 I want the same text which is printed in the console to be written in the log file.我希望将控制台中打印的相同文本写入日志文件。

My logging configuration in settings.py was incorrect.我在 settings.py 中的日志配置不正确。 The correct configuration is below.正确的配置如下。

LOGGING = {
    'version': 1,
    'formatters': {
         'standard': {
             'format': '%(asctime)s %(levelname)s %(name)s %(message)s'
         },
     },
    'loggers': {
        '':{
            'handlers': ['file'] ,
            'level':'DEBUG'
        }
    },
    'handlers':{
        'file':{
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'debug.log',
            'formatter': 'standard'
        }
    }
}

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

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