【问题标题】:How to perform log rotation with Gunicorn?如何使用 Gunicorn 执行日志轮换?
【发布时间】:2016-07-25 06:35:16
【问题描述】:

我在网上搜索但没有得到"how to use log rotation with Gunicorn?"的具体答案或示例。
如果有人提供一个例子,那就太好了。

【问题讨论】:

    标签: logging gunicorn logrotate


    【解决方案1】:

    Gunicorn 的文档说您可以使用 logrotate(一个 linux 命令)设置日志轮换:

    可以使用 logrotate 自动旋转和压缩日志。

    文档链接:http://docs.gunicorn.org/en/latest/install.html?highlight=logrotate#debian-gnu-linux

    所以我猜 Gunicorn 自己没有提供旋转日志的方法。

    这是我的配置文件的一个例子,放在/etc/logrotate.d/my_app:

    /path/to/my/logs/gunicorn-access.log /path/to/my/logs/gunicorn-error.log {
        monthly
        dateext
        dateformat -%Y-%m
        dateyesterday
        rotate 10000
    }
    

    每月轮换一次,将 -YEAR-MONTH 添加到轮换文件中,保留 10000 个轮换文件(请参阅man logrotate)。

    第一行的路径在我的gunicorn_start 脚本中声明,类似于:

    /my/virtualenv/bin/gunicorn OPTIONS \
        --access-logfile /path/to/my/logs/gunicorn-access.log \
        --error-logfile /path/to/my/logs/gunicorn-error.log
    

    【讨论】:

    • 我们在哪里指定 gunicorn 以使用来自 my_app 的设置?
    • 你没有。日志轮换由 logrotate 完成,而不是 gunicorn。
    【解决方案2】:

    如果您不想打扰 logrotare,您可以使用完整的 python/gunicorn 解决方案,使用 python 日志记录工具。

    使用以下内容创建一个名为 log.conf 的文件。这将每天轮换错误日志文件和访问日志文件,并将日志保留 90 天。日志级别设置为 INFO。

    然后,启动 gunicorn 添加命令行参数--log-config log.conf。删除 --access-logfile--error-logfile--log-level 参数,因为 log.conf 会处理所有事情。

    Python documentation 中提供了有关如何配置记录器的更多信息。

    下面是log.conf的内容。另一个例子请看gunicorn source code

    HTH

    [loggers]
    keys=root, gunicorn.error, gunicorn.access
    
    [handlers]
    keys=console, error_file, access_file
    
    [formatters]
    keys=generic, access
    
    [logger_root]
    level=INFO
    handlers=console
    
    [logger_gunicorn.error]
    level=INFO
    handlers=error_file
    propagate=1
    qualname=gunicorn.error
    
    [logger_gunicorn.access]
    level=INFO
    handlers=access_file
    propagate=0
    qualname=gunicorn.access
    
    [handler_console]
    class=StreamHandler
    formatter=generic
    args=(sys.stdout, )
    
    [handler_error_file]
    class=logging.handlers.TimedRotatingFileHandler
    formatter=generic
    args=('/var/log/gunicorn/gunicorn-error.log', 'midnight', 1, 90, 'utf-8')
    
    [handler_access_file]
    class=logging.handlers.TimedRotatingFileHandler
    formatter=access
    args=('/var/log/gunicorn/gunicorn-access.log', 'midnight', 1, 90, 'utf-8')
    
    [formatter_generic]
    format=%(asctime)s [%(process)d] [%(levelname)s] %(message)s
    datefmt=%Y-%m-%d %H:%M:%S
    class=logging.Formatter
    
    [formatter_access]
    format=%(message)s
    class=logging.Formatter
    

    【讨论】:

      【解决方案3】:

      文档链接:https://docs.gunicorn.org/en/latest/deploy.html#logging

      Logging
      Logging can be configured by using various flags detailed in the configuration documentation or by creating a logging configuration file. Send the USR1 signal to rotate logs if you are using the logrotate utility:
      kill -USR1 $(cat /var/run/gunicorn.pid)
      

      所以你可以这样写配置文件:

      /yourpath/log/gunicorn.* {
      daily
      rotate 30
      compress
      dateext
      dateformat .%Y-%m-%d
      notifempty
      sharedscripts
      postrotate
          kill -USR1 $(cat /yourpath/run/gunicorn.pid)
      endscript
      }
      

      每天轮换

      【讨论】:

        【解决方案4】:

        添加对查理的方法的更改,logging.handlers.TimedRotatingFileHandler 在最近的 python(3.8) 中不起作用,所以创建一个文件,

        simple_logger.py

        from logging.handlers import TimedRotatingFileHandler
        

        并更新log.conf中的类行,

        class=simple_logger.TimedRotatingFileHandler
        

        成功了!

        注意:轮换有效,但推送到日志的数据不是按顺序排列的。

        【讨论】:

          猜你喜欢
          • 2017-08-31
          • 1970-01-01
          • 2022-06-15
          • 1970-01-01
          • 2019-11-25
          • 2020-07-15
          • 2017-03-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多