cron为什么将Python的日志记录事件捕获为错误? [英] Why is cron catching Python's logging events as errors?

查看:100
本文介绍了cron为什么将Python的日志记录事件捕获为错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用cron和使用logging模块的Python脚本进行了非常简单的测试设置,并且cron在遇到记录事件时表现异常.

I've got a very simple test setup with cron and a Python script that uses the logging module, and cron is behaving oddly when it encounters a logging event.

* * * * * ~/test.py >> ~/test.log

〜/test.py

#!/usr/bin/env python

import logging
logging.basicConfig(level=logging.DEBUG)

print 'Properly printed, to file'
logging.debug("Does not get printed, get's e-mailed as an error")
print 'Still running, though'

〜/test.log

cron运行后,日志中将显示以下两条消息:

~/test.log

After a cron run, the log is populated with these two messages:

Properly printed, to file
Still running, though

cron错误电子邮件

cron运行后,我还收到一条通知,通知我有新邮件:

cron Error E-mail

Also after a cron run, I get a notification that I have new mail:

From tomcat6@local  Thu May 23 16:35:01 2013
Date: Thu, 23 May 2013 16:35:01 -0700
From: root@local (Cron Daemon)
To: tomcat6@local
Subject: Cron <tomcat6@local> ~/test.py >> ~/test.log
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/usr/local/tomcat6>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=tomcat6>
X-Cron-Env: <USER=tomcat6>

DEBUG:root:Does not get printed, get's e-mailed as an error

看起来这不是一个明确的错误,否则最终消息仍在运行..."将不会打印到~/test.log,对吗?

It looks like it's not an explicit error, otherwise the final, 'Still running...', message would not be printed to ~/test.log, right?

为什么要使用cron和或log来执行此操作,并且有解决方法?

Why are cron and or logging doing this, and is there a workaround?

推荐答案

basicConfig()设置的默认配置将消息记录到stderr,并且cron将对stderr文件句柄的所有输出解释为可发送电子邮件;毕竟,您是不会重定向它的.

The default configuration set by basicConfig() logs messages to stderr, and cron interprets any output to the stderr filehandle as email-worthy; you are, after all, not redirecting it.

请参见 logging.basicConfig()文档:

通过创建具有默认值FormatterStreamHandler并将其添加到根记录器中,对记录系统进行基本配置,

Does basic configuration for the logging system by creating a StreamHandler with a default Formatter and adding it to the root logger,

StreamHandler文档:

如果指定了 stream ,实例将使用它来记录输出;否则,实例将使用它来记录输出.否则,将使用sys.stderr.

If stream is specified, the instance will use it for logging output; otherwise, sys.stderr will be used.

解决方法是不设置流处理程序,或选择其他流,或选择文件名登录:

The work-around is to not set up a stream handler, or pick a different stream, or to pick a filename to log to:

logging.basicConfig(level=logging.DEBUG, filename='/some/file/to/log/to')

logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)

通过登录到stdout,您可以将记录信息写入与print()相同的输出流中.

By logging to stdout you write logging information to the same output stream as print() does.

另一种解决方法是将stderr重定向到stdout:

Another work-around is to redirect stderr to stdout:

* * * * * ~/test.py >> ~/test.log 2>&1

这篇关于cron为什么将Python的日志记录事件捕获为错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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