【问题标题】:Pygelf - How to send logs with "notice" severity levelPygelf - 如何发送具有“通知”严重性级别的日志
【发布时间】:2019-09-03 16:12:58
【问题描述】:

我正在使用 pygelf 日志处理程序将 Flask 应用程序与 Graylog 集成。

根据documentation,Graylog 支持八个 syslog 严重级别,基于RFC 3164,即:

(...)
        Numerical         Severity
          Code

           0       Emergency: system is unusable
           1       Alert: action must be taken immediately
           2       Critical: critical conditions
           3       Error: error conditions
           4       Warning: warning conditions
           5       Notice: normal but significant condition
           6       Informational: informational messages
           7       Debug: debug-level messages
(...)

虽然 Graylog 支持级别 5,即 notice,但 Python 的 logging 包似乎既没有 notice() 日志记录方法(如 info()debug())也没有相应的定义的日志记录级别:

CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0

问题: 有没有办法强制pygelf 使用notice 日志级别?


附加背景:

我正在使用 Flask 框架,默认情况下使用日志级别 6 (info) 和 7 (debug) 来记录它自己的内部 http 请求数据,如下所示:

101.101.101.101 - - [03/Sep/2019 14:15:55] "GET /static/images/favicon.ico HTTP/1.0" 200 -

由于这些内部日志,我自己的infodebug 级别的日志在人群中消失了。我不想完全过滤掉它们,但我仍然希望有一些独特的信息日志记录级别,它不如warning 高——这就是为什么我可以使用notice 级别的日志记录,不幸的是我不能开箱即用。

【问题讨论】:

    标签: python python-3.x graylog graylog3


    【解决方案1】:

    Pygelf 内部拥有一个 dict,它映射 Python 日志记录级别及其对应的 Graylog 日志记录级别,位于 gelf.py

    LEVELS = {
        logging.DEBUG: 7,
        logging.INFO: 6,
        logging.WARNING: 4,
        logging.ERROR: 3,
        logging.CRITICAL: 2
    }
    

    我的解决方案:为了登录 Graylog 的 5 级(通知),我必须:

    1. 在记录器配置中添加新的日志级别映射:
    from pygelf import GelfTcpHandler, gelf
    
    NOTIFY = 35
    GELF_NOTIFY_LEVEL = 5
    
    (...)
    
    gelf.LEVELS.update({NOTIFY: GELF_NOTIFY_LEVEL})
    handler = GelfTcpHandler(
        ...
    )
    logging.getLogger().addHandler(handler)
    
    1. 将记录器调用从默认方法更改为具有自定义日志级别的方法:
    import logging
    logger = logging.getLogger()
    
    (...)
    
    # logger.debug('Old logging to "debug"-level logger')
    logger.log(NOTIFY, 'New logging to "notify"-level logger')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 2022-10-17
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多