简体   繁体   中英

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

I'm using pygelf logging handler to integrate a Flask app with Graylog .

According to documentation , Graylog supports eight syslog severity levels, based on RFC 3164 , namely:

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

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?


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:

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

Pygelf internally holds a dict , that maps Python logging levels and their corresponding Graylog logging levels, in gelf.py :

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:

  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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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