syslog中的多行日志记录
因此,我已经将我的 Python 应用程序配置为使用 Python 的 SysLogHandler 记录到 syslog,并且一切正常。多行处理除外。并不是说我需要如此严重地发出多行日志记录(我做了一点),而是我需要能够读取 Python 的异常。我正在使用带有 rsyslog 4.2.0 的 Ubuntu。这就是我得到的:
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")
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!
Test code in case you need it:
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")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者,如果您想让系统日志在一行中保持完整以便解析,您可以在查看日志时替换字符。
Alternatively, if you want to keep your syslog intact on one line for parsing, you can just replace the characters when viewing the log.
好吧,终于弄清楚了...
rsyslog 默认情况下会转义所有奇怪的字符(ASCII < 32),这包括换行符(以及制表符和其他字符)。
$EscapeControlCharactersOnReceive:
您只需将其添加到 rsyslog 配置中即可将其关闭:
或者使用“新”高级语法:
OK, figured it out finally...
rsyslog by default escapes all weird characters (ASCII < 32), and this include newlines (as well as tabs and others).
$EscapeControlCharactersOnReceive:
You can simply add this to your rsyslog config to turn it off:
or, with the "new" advanced syntax:
另一种选择是子类化 SysLogHandler 并覆盖
emit()
- 然后您可以为发送的文本中的每一行调用超类emit()
。像这样的东西:Another option would be to subclass the SysLogHandler and override
emit()
- you could then call the superclassemit()
for each line in the text you're sent. Something like: