2015-05-07 140 views
1

documentation爲Python 2.x的SysLogHandleremit()功能說:爲什麼Python記錄異常到syslog

The record is formatted, and then sent to the syslog server. If exception information is present, it is not sent to the server.

...爲什麼? source不是tell我多。我錯過了什麼?

然而,當我嘗試下面的代碼:

import logging 
from logging.handlers import SysLogHandler 

rootlogger = logging.getLogger() 
rootlogger.setLevel(logging.DEBUG) 
syslogh = SysLogHandler('/dev/log') 
rootlogger.addHandler(syslogh) 
# to see what's happening too 
consoleh = logging.StreamHandler() 
rootlogger.addHandler(consoleh) 

# this appears in the log 
rootlogger.info('foobar') 
try: 
    a = 42 
    a/0 
except ZeroDivisionError as e: 
    rootlogger.exception('myException!: {}'.format(e)) 

它編寫以下到syslog:

May 7 16:25:59 localhost foobar
May 7 16:25:54 localhost myException!: integer division or modulo by zero#012Traceback (most recent call last):#012 File "syslogonly.py", line 16, in #012 a/0#012ZeroDivisionError: integer division or modulo by zero

我使用rsyslog現在8.4.2在Debian喘息系統。

此外,我知道有多行的問題,但這是相關的嗎?

回答

1

你不叫Logger.emit()。你打電話Logger.exception(),其中根據documentation,總是添加異常信息的消息:

Logs a message with level ERROR on this logger. The arguments are interpreted as for debug(), except that any passed exc_info is not inspected. Exception info is always added to the logging message. This method should only be called from an exception handler.

我沒有深入到源代碼,以確定什麼叫emit(),什麼情況下,但是。

+0

好點。但是我仍然不確定sysloghandler爲什麼不記錄異常。也許這是文檔中的錯誤? – lorenzog