如何使用Gunicorn进行日志轮换?

33

我在网上搜索,但没有获得如何在Gunicorn中使用日志轮换的具体答案或示例。
如果有人能提供一个示例,那就太好了。

我搜索了网络,但没有找到如何在 Gunicorn 中使用日志轮换的具体答案或示例。
如果有人能提供一个示例,那就太好了。

4个回答

26

如果您不想使用logrotate,可以使用完整的Python/gunicorn解决方案,并利用Python日志功能。

创建一个名为log.conf的文件,其中包含以下内容。这将使错误日志文件和访问日志文件每天旋转一次,并保留日志90天。日志级别设置为INFO。

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

有关如何配置记录器的更多信息,请参见Python文档

下面是log.conf的内容。另请参阅gunicorn源代码的其他示例。

希望对您有所帮助

[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

19

Gunicorn的文档说明,您可以使用Linux命令logrotate设置日志轮换:

可以使用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的设置? - hangc
2
你不需要这样做。日志轮换是由logrotate完成的,而不是gunicorn。 - pawamoy

14

文档链接: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
}

每天旋转


这个方法可行。我是通过supervisor运行gunicorn的,所以我必须在postrotate脚本中加入supervisordctl restart <program name>。感觉有点像hack,但现在可以用了。 - miniGweek

1

为了改进Charlie的方法,需要添加一项修改:logging.handlers.TimedRotatingFileHandler在最近的Python版本(3.8)中无法使用,因此需要创建一个文件:

simple_logger.py

from logging.handlers import TimedRotatingFileHandler

并更新 log.conf 中的类行。

class=simple_logger.TimedRotatingFileHandler

它有效了!

注意:旋转功能可用,但推送到日志的数据不是连续的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接