为什么我会得到一个假的';]';Python';OSX上的SysLogHandler是什么?
使用OS X 10.10.4上的Python3.5,我在输出的syslog消息中得到了虚假的为什么我会得到一个假的';]';Python';OSX上的SysLogHandler是什么?,python,macos,python-3.x,logging,syslog,Python,Macos,Python 3.x,Logging,Syslog,使用OS X 10.10.4上的Python3.5,我在输出的syslog消息中得到了虚假的]字符。这可以通过以下示例程序看到: #!/usr/bin/env python3 import logging import logging.handlers logger = logging.getLogger('test') syslog_handler = logging.handlers.SysLogHandler(address='/var/run/syslog') syslog_for
]
字符。这可以通过以下示例程序看到:
#!/usr/bin/env python3
import logging
import logging.handlers
logger = logging.getLogger('test')
syslog_handler = logging.handlers.SysLogHandler(address='/var/run/syslog')
syslog_formatter = logging.Formatter('[{process}] {message}', style='{')
syslog_handler.setFormatter(syslog_formatter)
logger.addHandler(syslog_handler)
logger.error("Test : ABC")
如果我运行此命令,我会看到如下的系统日志输出:
Dec 16 12:38:33 mymachinename [76399] Test]: ABC
(注意测试后的伪]
字符)
如果我稍微更改格式化程序字符串以删除初始的[
字符,则附加的]
将消失。但是,我希望这个文本字符出现在格式化字符串中(即使它不在格式化字符串的开头,我也有同样的问题)
为什么会出现这种虚假的]
,我如何避免它
OSX的asl.conf
,它是配置日志的地方,如下所示。我没有修改默认设置:
##
# configuration file for syslogd and aslmanager
##
# authpriv messages are root/admin readable
? [= Facility authpriv] access 0 80
# remoteauth critical, alert, and emergency messages are root/admin readable
? [= Facility remoteauth] [<= Level critical] access 0 80
# broadcast emergency messages
? [= Level emergency] broadcast
# save kernel [PID 0] and launchd [PID 1] messages
? [<= PID 1] store
# ignore "internal" facility
? [= Facility internal] ignore
# save everything from emergency to notice
? [<= Level notice] store
# Rules for /var/log/system.log
> system.log mode=0640 format=bsd rotate=seq compress file_max=5M all_max=50M
? [= Sender kernel] file system.log
? [<= Level notice] file system.log
? [= Facility auth] [<= Level info] file system.log
? [= Facility authpriv] [<= Level info] file system.log
? [= Facility user] [<= Level debug] file system.log
# Facility com.apple.alf.logging gets saved in appfirewall.log
? [= Facility com.apple.alf.logging] file appfirewall.log file_max=5M all_max=
##
#syslogd和aslmanager的配置文件
##
#authpriv消息是根/管理员可读的
? [=Facility authpriv]访问权限0 80
#remoteauth关键、警报和紧急消息可由root/admin读取
? [=设施远程身份验证][更新:
明白了。请修复格式字符串以符合BSD系统日志格式:
syslog_formatter = logging.Formatter('[{process}]: {message}', style='{')
...
logger.error("Test : ABC")
你会得到:
Aug 23 21:38:02 mymachine [65057]: Test : ABC
背景/原稿
我在OS X 10.11.6上重现了您的错误
Aug 23 21:10:15 machine-name [64643] Test]: ABC
冒号肯定是罪魁祸首。我将消息更改为:
logger.error("Test ABC")
然后日志条目是正常的:
Aug 23 21:15:03 machine Unknown: [64701] Test ABC
我认为冒号被解释为格式化字符,因为您使用的是BSD格式化选项。从:
因此,可能是OS X系统日志试图遵守BSD系统日志RFC并产生混淆?看起来Test
被解释为一个标记。请显示您的rsyslog配置好吗?谢谢。@Alexe,好主意,但我不这么认为,因为即使我用任何其他单词替换Test,错误也是一样的。删除>:
似乎也删除了]
(这让我觉得它是相关的),但我想在消息中添加一个:
。@Alexe,OS X不使用rsyslog,但我在asl.conf
中进行了编辑,这是配置日志的地方。非常好,谢谢!我为此挣扎了一段时间。这很有效。唯一的缺点是,在Linux上,我现在得到了一个额外的冒号,它与其他系统日志消息不匹配,但至少看起来更整洁。如果我也在乎的话,我也可以使用特定于平台的格式化代码。谢谢没问题,我认为这个问题非常有趣,因为越来越多的NetOpts工具都在分析Syslog消息。您可能认为这会导致更严格的格式要求。
the first character of the CONTENT field that signifies the conclusion of
the TAG field has been seen to be the left square bracket character ("["),
a colon character (":"), or a space character. This is explained in more
detail in Section 5.3.