Python Django 登录到控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22134895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django logging to console
提问by oneloop
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.)
我正在尝试设置一个记录到控制台的记录器(我想要这个,因为我将 Heroku 与 Papertrails(Heroku 的日志插件)一起使用,写入控制台的内容将显示在 Papertrails 中,使其可过滤和所有不错的 Papertrail 功能。)
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',
}
}
(...)
}
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).
根据 Django 的日志页面(对于那些不使用 Mezzanine 的人来说,page_processors 是 Mezzanine 在您打开页面时运行的内容;您可以将它们视为 Django 的视图,但它们只执行上下文,而不执行渲染)。
On page_processors.py I have
在 page_processors.py 上我有
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
and so I know the logic is working. After googling a bit, I found thisand this pagethat 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:
所以我知道逻辑是有效的。在谷歌搜索了一下之后,我发现这个和这个页面正好解决了这个问题。他说默认情况下 logging.StreamHandler 会记录到 STDERR。如果我们想记录到 STDOUT,您应该将关键字参数“stream”添加到 logging.StreamHandler 构造中,然后将处理程序配置为:
'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.
结果这仍然不起作用,我没有收到任何错误或任何东西,我仍然看到打印和文件日志。只是不是控制台记录器。
What's going on?
这是怎么回事?
EDIT: I tried this, doesn't make a difference.
编辑:我试过这个,没有什么区别。
采纳答案by oneloop
I finally got it. Here's what was happening.
我终于明白了。这就是发生的事情。
When you define a logger using getLogger, you give a logger a name, in this case
当你使用 getLogger 定义一个记录器时,你给一个记录器一个名字,在这种情况下
logger = logging.getLogger(__name__)
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.
然后您必须定义具有该名称的记录器在 LOGGING 配置中的行为方式。在这种情况下,由于该文件位于模块内,因此记录器的名称变为 myApp.page_processors,而不是 page_processors,因此永远不会调用 LOGGING dict 中名为“page_processors”的记录器。那么为什么记录到文件的工作正常?因为在我在代码中显示的 (...) 中,有另一个名为“myApp”的记录器显然被调用了,并且该记录器写入文件。
So the solution to this question is just to properly name the logger:
所以这个问题的解决方案只是正确命名记录器:
LOGGING = {
# (...)
'loggers': {
# (...)
'myApp.page_processors': {
'handlers': ['console','file'],
'level': 'DEBUG',
}
}
# (...)
}
回答by Vinay Sajip
The following script:
以下脚本:
import logging, logging.config
import sys
LOGGING = {
'version': 1,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'stream': sys.stdout,
}
},
'root': {
'handlers': ['console'],
'level': 'INFO'
}
}
logging.config.dictConfig(LOGGING)
logging.info('Hello')
writes Helloto sys.stdout, as can be verified by piping its output to a file. So your problem is likely to be somewhere else (or possibly that sys.stdout isn't what you expect). You could try with sys.__stdout__to see if that makes a difference.
写入Hello到sys.stdout,这可通过其输出通过管道到一个文件进行验证。所以你的问题很可能在其他地方(或者 sys.stdout 可能不是你所期望的)。你可以试试看sys.__stdout__是否有区别。
回答by rogoro
I am writing this for easy understanding dummies like me.
我写这篇文章是为了让像我这样的傻瓜更容易理解。
In settings.py
在settings.py中
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'app_api': {
'handlers': ['console'],
'level': 'INFO',
},
},
}
Somewhere in your application views
在您的应用程序视图中的某处
import logging
logger = logging.getLogger('app_api') #from LOGGING.loggers in settings.py
try:
one = 1/0
except Exception as e:
logger.error(e)

