Django登录到控制台 [英] Django logging to console

查看:81
本文介绍了Django登录到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个记录器,该记录器将记录到控制台(我想要这样做,因为我将Heroku与Papertrails(Heroku的记录插件)一起使用,并且写入控制台的内容将显示在Papertrails中,使其可过滤以及所有出色的Papertrail功能.)

I'm trying to set up a logger that will log to the console (I want this because I'm using Heroku with Papertrails (Heroku's logging addon) and stuff written to the console will show up in Papertrails, making it filterable and all the nice Papertrail features.)

在设置中,我首先尝试以下操作:

In settings I was first trying the following:

LOGGING = {
    'handlers' = {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'mysite.log',
            'formatter': 'verbose'
        },
        'console':{
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
    },
    (...)
    'loggers'={
        (...)
        'page_processors': {
            'handlers': ['console','file'],
            'level': 'DEBUG',
        }
    }
    (...)
}

根据Django的日志记录页面(对于不使用Mezzanine的用户,page_processors是Mezzanine在您打开页面时运行的内容;您可以将它们视为类似于Django的视图,但它们仅用于上下文,而不用于呈现).

as per the Django's logging page (for those who don't use Mezzanine, page_processors are what Mezzanine runs whenever you open a page; you can think of them as being like Django's views, but they only do the context, not the rendering).

我在page_processors.py上

On page_processors.py I have

import logging
logger = logging.getLogger(__name__)

@process_for(MyPage):
def myfunc(request, Page):
    logger.info('page_processor logging test')
    print 'my page_processor print'
    (...)

刷新页面时,我看不到记录器,但看到打印和记录到文件的记录:

When I refresh the page I don't see the logger but I see the print AND the log to the file:

[02/Mar/2014 23:07:10] INFO [myApp.page_processors:13] page_progessor logging test

,所以我知道逻辑在起作用.稍作搜索后,我发现此页面正好解决了此问题.他说默认情况下记录日志.StreamHandler记录到STDERR.如果我们要登录到STDOUT,则应将关键字参数'stream'添加到logging.StreamHandler构造中,然后按以下方式配置处理程序:

and so I know the logic is working. After googling a bit, I found this and this page that addresses precisely this issue. He says that by default logging.StreamHandler logs to STDERR. If we want to log to STDOUT you should add the keyword argument 'stream' to the logging.StreamHandler construct, and so configure the handler as such:

'handlers':{
    (...)
    'console':{
        'level': 'DEBUG',
        'class': 'logging.StreamHandler',
        'stream': sys.stdout
    },
}

结果证明这仍然行不通,并且我没有收到任何错误或任何提示,并且仍然可以看到打印内容和文件日志.只是不是控制台记录器.

Turns out this still doesn't work, and I don't get any error or anything, and I still see the print and the file log. Just not the console logger.

这是怎么回事?

我尝试了,没有任何区别.

I tried this, doesn't make a difference.

推荐答案

我终于明白了.这就是正在发生的事情.

I finally got it. Here's what was happening.

在使用getLogger定义记录器时,在这种情况下,您需要为记录器指定一个名称

When you define a logger using getLogger, you give a logger a name, in this case

logger = logging.getLogger(__name__)

,然后必须定义具有该名称的记录器在LOGGING配置中的行为.在这种情况下,由于该文件位于模块内部,因此记录器的名称为myApp.page_processors,而不是page_processors,因此永远不会调用LOGGING dict中名为"page_processors"的记录器.那么,为什么记录到文件的工作正常?因为在代码中显示的(...)中,有一个名为"myApp"的记录器显然已被调用,并且该记录器将写入文件.

and you then have to define how a logger with that name behaves in the LOGGING configuration. In this case, since that file is inside a module, the logger's name becomes myApp.page_processors, not page_processors, so the logger named 'page_processors' in the LOGGING dict is never called. So why was the logging to the file working? Because in the (...) that I show in the code there is another logger named 'myApp' that apparently gets called instead, and that one writes to the file.

因此,此问题的解决方案是正确命名记录器:

So the solution to this question is just to properly name the logger:

LOGGING = {
    # (...)
    'loggers': {
        # (...)
        'myApp.page_processors': {
            'handlers': ['console','file'],
            'level': 'DEBUG',
        }
    }
    # (...)
}

这篇关于Django登录到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆