DEV Community 👩‍💻👨‍💻

Yao-Ching Huang
Yao-Ching Huang

Posted on • Updated on

Python :: Gunicorn

logging

預設logging處理機制

  • root
    • INFO level
    • 送至 stdout
  • gunicorn.error
    • INFO level
    • 處理 error log
    • 送至 stderr
    • log 會接續送給 parent logger 的 handler
  • gunicorn.access
    • INFO level
    • 處理 access log
    • 送至 stdout
    • log 會接續送給 parent logger 的 handler

NOTE: 因為gunicorn預設propate=True,所以console會看到重複2筆log records的現象(root + gunicorn)

https://github.com/benoitc/gunicorn/blob/20.1.0/gunicorn/glogging.py#L48

CONFIG_DEFAULTS = dict(
        version=1,
        disable_existing_loggers=False,

        root={"level": "INFO", "handlers": ["console"]},
        loggers={
            "gunicorn.error": {
                "level": "INFO",
                "handlers": ["error_console"],
                "propagate": True,
                "qualname": "gunicorn.error"
            },

            "gunicorn.access": {
                "level": "INFO",
                "handlers": ["console"],
                "propagate": True,
                "qualname": "gunicorn.access"
            }
        },
        handlers={
            "console": {
                "class": "logging.StreamHandler",
                "formatter": "generic",
                "stream": "ext://sys.stdout"
            },
            "error_console": {
                "class": "logging.StreamHandler",
                "formatter": "generic",
                "stream": "ext://sys.stderr"
            },
        },
        formatters={
            "generic": {
                "format": "%(asctime)s [%(process)d] [%(levelname)s] %(message)s",
                "datefmt": "[%Y-%m-%d %H:%M:%S %z]",
                "class": "logging.Formatter"
            }
        }
)
Enter fullscreen mode Exit fullscreen mode

loglevel改動範圍

Gunicorn提供loglevel參數可調整預設的loglevel,但是內部如何實現?

影響範圍:

  • 不修改 root logger
  • 修改 error log 的loglevel
  • access log 仍維持 INFO level

https://github.com/benoitc/gunicorn/blob/20.1.0/gunicorn/glogging.py#L197

def setup(self, cfg):
    self.loglevel = self.LOG_LEVELS.get(cfg.loglevel.lower(), logging.INFO)
    self.error_log.setLevel(self.loglevel)
    self.access_log.setLevel(logging.INFO)
...
Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)

Tired of sifting through your feed?

Find the content you want to see.

Change your feed algorithm by adjusting your experience level and give weights to the tags you follow.