简体   繁体   English

当控制台日志不存在时,为什么我的Python Django日志记录文件为空?

[英]Why is my Python Django logging file empty when my console log is not?

I am trying to set up a Django log to file. 我正在尝试将Django日志设置为文件。 Based on https://stackoverflow.com/a/19257221/214742 I came up with the following configuration: 基于https://stackoverflow.com/a/19257221/214742,我想到了以下配置:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'console':{
            'level':'DEBUG',
            'class':'logging.StreamHandler',
            'formatter': 'simple'
        },
        'applogfile': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(PROJECT_ROOT, 'MODELINGWEB.log'),
            'maxBytes': 1024*1024*15, # 15MB
            'backupCount': 10,
            'formatter': 'simple'
        },
    },
    'loggers': {
        '': {
          'level': 'DEBUG',
          'handlers': ['console','applogfile']
        },
    },
}

Now when I try to run and load a page my console log looks like: 现在,当我尝试运行并加载页面时,控制台日志如下所示:

Performing system checks...

System check identified no issues (0 silenced).
June 28, 2017 - 10:18:22
Django version 1.11, using settings 'Modeling.settings.dev'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[28/Jun/2017 10:18:27] "GET / HTTP/1.1" 200 12564

I also get a file MODELINGWEB.log but it is empty. 我还得到一个文件MODELINGWEB.log但它为空。 Why is that? 这是为什么? I was expecting it to contain the same things... 我期望它包含相同的东西...

Although you are setting the configuration for a logger , you aren't logging the info into the file: 尽管您正在设置logger的配置 ,但是您并未将信息记录到文件中:

import logging

logger = logging.getLogger(__name__)
logger.info("blah blah blah")  # Or loggger.error(..), or whatever

I would recommend getting a closer look to the docs (the link goes to the specific part of using this logger object). 我建议您仔细看一下文档 (链接指向使用此logger对象的特定部分)。

What I truly unknown is how to add a signal when running the development server to trigger the piece of code above. 我真正不知道的是,在运行开发服务器以触发上面的代码时如何添加信号。

As you can see in https://docs.djangoproject.com/en/dev/topics/logging/#using-logging Once you have configured your loggers, handlers, filters and formatters, you need to place logging calls into your code. 如您在https://docs.djangoproject.com/zh-CN/dev/topics/logging/#using-logging中看到的那样,配置记录器,处理程序,过滤器和格式化程序后,需要将记录调用放入代码中。 Using the logging framework is very simple. 使用日志记录框架非常简单。

So what you see in your console is not really logging but something else 因此,您在控制台中看到的不是真正的日志记录,而是其他内容

If you want to get the stuff in your console into my file you can use pipe and tee : 如果要将控制台中的内容放入我的文件中,可以使用pipe和tee

[the command you want to run] | tee you_log_file.log

For example: 例如:

ifconfig | tee ifconfig.log

you will get the output in your console and the file ifconfig.log. 您将在控制台中获得输出以及文件ifconfig.log。

What's your code for logging logs? 您记录日志的代码是什么? You need to obtain logger for logging into specified logger. 您需要获取记录器才能登录到指定记录器。 I notice that you don't even give a name to your logger, so I suppose you just use "logging.error, logging.debug, etc" 我注意到您甚至没有给记录器起一个名字,所以我想您只使用“ logging.error,logging.debug等”

'loggers': { '': { 'level': 'DEBUG', 'handlers': ['console','applogfile'] }, 'loggers:{'':{'level':'DEBUG','handlers:['console','applogfile']},

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

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