简体   繁体   English

Pygelf-如何发送具有“通知”严重性级别的日志

[英]Pygelf - How to send logs with “notice” severity level

I'm using pygelf logging handler to integrate a Flask app with Graylog . 我正在使用pygelf日志记录处理程序将Flask应用程序与Graylog集成在一起

According to documentation , Graylog supports eight syslog severity levels, based on RFC 3164 , namely: 根据文档 ,Graylog支持基于RFC 3164的八个syslog严重级别,即:

(...)
        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
(...)

While Graylog supports level 5, which is notice , Python's logging package does not seem to have neither a notice() logging method (like info() or debug() ) nor a corresponding logging level defined: 虽然Graylog支持5级(即notice ,但Python的日志记录包似乎既没有notification notice()日志记录方法(如info()debug() ),也没有定义相应的日志记录级别:

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

Question : Is there a way to force pygelf to use the notice log level? 问题 :是否有一种方法可以强制pygelf使用notice日志级别?


Additional background: 其他背景:

I'm using Flask framework, which by default uses log levels 6 ( info ) and 7 ( debug ) to log it's own internal http request data that looks like: 我正在使用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 -

Because of those internal logs, my own info and debug -level logs get lost in the crowd. 由于这些内部日志,我自己的infodebug级别的日志在人群中迷路了。 I don't want to completely filter them off, but I still want to have some distinct informative logging level that's not as high as warning - that's why I could use a notice -level logging, which unfortunately I can't use out-of-the-box. 我不想完全过滤掉它们,但我仍然希望拥有一些不如warning级别高的独特信息日志记录级别-这就是为什么我可以使用notice级别的日志记录的原因,不幸的是,我不能使用超出范围的日志记录盒子。

Pygelf internally holds a dict , that maps Python logging levels and their corresponding Graylog logging levels, in gelf.py : Pygelf内部拥有dict ,它在gelf.py中映射了Python日志记录级别及其对应的Graylog日志记录级别:

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

My solution : In order to log on Graylog's level 5 (notify) I had to: 我的解决方案 :为了登录Graylog的5级(通知),我必须:

  1. Add a new log-level mapping in the logger configuration: 在记录器配置中添加新的日志级别映射:
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. Change the logger calls from the default methods to ones with custom log level: 将记录器调用从默认方法更改为具有自定义日志级别的记录器:
import logging
logger = logging.getLogger()

(...)

# logger.debug('Old logging to "debug"-level logger')
logger.log(NOTIFY, 'New logging to "notify"-level logger')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM