Python SysLogHandler - >系统日志:logstash。设施没有改变

时间:2014-09-03 12:55:05

标签: python logging logstash syslog

我尝试使用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

但是如何添加呢?

1 个答案:

答案 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'。“