我尝试使用logging.handlers.SysLogHandler进行日志记录并将其发送到logstash。
Python代码:
import logging
from logging import handlers
# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = handlers.SysLogHandler(facility=handlers.SysLogHandler.LOG_AUTH)
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
logger.info('go')
logstash conf:
input {
syslog {
}
}
output {
stdout {codec => rubydebug {}}
}
输出logstash:
{
"message" => "<38>2014-09-03 12:48:36,700 - simple_example - INFO - go\u0000",
"@version" => "1",
"@timestamp" => "2014-09-03T12:48:36.702Z",
"host" => "127.0.0.1",
"tags" => [
[0] "_grokparsefailure"
],
"priority" => 13,
"severity" => 5,
"facility" => 1,
"facility_label" => "user-level",
"severity_label" => "Notice"
}
但是,如果我改变facility = handlers.SysLogHandler.LOG_DAEMON 不是更改输出logstash:
{
"message" => "<30>2014-09-03 12:51:52,307 - simple_example - INFO - go\u0000",
"@version" => "1",
"@timestamp" => "2014-09-03T12:51:52.307Z",
"host" => "127.0.0.1",
"tags" => [
[0] "_grokparsefailure"
],
"priority" => 13,
"severity" => 5,
"facility" => 1,
"facility_label" => "user-level",
"severity_label" => "Notice"
}
如何更改:设施,严重性,优先级,facility_label,severity_label?
很可能是因为Python没有添加这些信息:
output {
stdout {}
}
2014-09-03T13:19:14.862+0000 127.0.0.1 <30>2014-09-03 13:19:14,860 - simple_example - INFO - go
但是如何添加呢?
答案 0 :(得分:1)
如何更改:设施,严重性,优先级,facility_label, severity_label?
查看logging.handlers的文档:
您可以在SysLogHandler初始化中包含facility
。允许的级别列在上面链接的文档中:
ch = handlers.SysLogHandler(facility=handlers.SysLogHandler.LOG_AUTH, facility=LOG_LOCAL0)
priority
从日志级别映射,该级别由邮件发件人logger.warn()
与logger.info()
等设置。mapPriority
个州的文档,“默认算法将DEBUG,INFO,WARNING,ERROR和CRITICAL映射到等效的syslog名称,将所有其他级别名称映射到'warning'。“