问题描述
因此,我已将Python应用程序配置为使用Python的SysLogHandler登录到syslog,并且一切正常.多行处理除外.并不是我需要非常严重地发出多行日志记录(我做了一点),但是我需要能够读取Python的异常.我正在将Ubuntu与rsyslog 4.2.0一起使用.这就是我得到的:
So I've configured my Python application to log to syslog with Python's SysLogHandler, and everything works fine. Except for multi-line handling. Not that I need to emit multiline log records so badly (I do a little), but I need to be able to read Python's exceptions. I'm using Ubuntu with rsyslog 4.2.0. This is what I'm getting:
Mar 28 20:11:59 telemachos root: ERROR 'EXCEPTION'#012Traceback (most recent call last):#012 File "./test.py", line 22, in <module>#012 foo()#012 File "./test.py", line 13, in foo#012 bar()#012 File "./test.py", line 16, in bar#012 bla()#012 File "./test.py", line 19, in bla#012 raise Exception("EXCEPTION!")#012Exception: EXCEPTION!
在需要时测试代码:
import logging
from logging.handlers import SysLogHandler
logger = logging.getLogger()
logger.setLevel(logging.INFO)
syslog = SysLogHandler(address='/dev/log', facility='local0')
formatter = logging.Formatter('%(name)s: %(levelname)s %(message)r')
syslog.setFormatter(formatter)
logger.addHandler(syslog)
def foo():
bar()
def bar():
bla()
def bla():
raise Exception("EXCEPTION!")
try:
foo()
except:
logger.exception("EXCEPTION")
推荐答案
好的,终于知道了...
OK, figured it out finally...
rsyslog会转义所有奇怪的字符(ASCII< 32),其中包括换行符(以及制表符和其他字符).只需将其添加到您的rsyslog配置中即可将其关闭:
rsyslog by default escapes all weird characters (ASCII < 32), and this include newlines (as well as tabs and others). Simply add this to your rsyslog config to turn this off:
$EscapeControlCharactersOnReceive off
这篇关于syslog中的多行日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!