首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Gunicorn进行日志轮换?

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

Stack Overflow用户
提问于 2016-04-05 18:51:14
回答 4查看 11.9K关注 0票数 21

我在网上搜索,但没有得到"how to use log rotation with Gunicorn?"的具体答案或示例。

如果有人能提供一个例子那就太好了。

EN

回答 4

Stack Overflow用户

发布于 2017-04-22 01:14:51

Gunicorn的文档显示,您可以使用logrotate ( linux命令)设置日志轮换:

日志可以使用logrotate自动轮换和压缩。

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

所以我猜Gunicorn没有给自己提供轮换日志的方法。

下面是我的配置文件的一个示例,放在/etc/logrotate.d/my_app

代码语言:javascript
运行
复制
/path/to/my/logs/gunicorn-access.log /path/to/my/logs/gunicorn-error.log {
    monthly
    dateext
    dateformat -%Y-%m
    dateyesterday
    rotate 10000
}

每月轮换,对轮换的文件添加年-月,保留10000个轮换的文件(请参阅man logrotate)。

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

代码语言:javascript
运行
复制
/my/virtualenv/bin/gunicorn OPTIONS \
    --access-logfile /path/to/my/logs/gunicorn-access.log \
    --error-logfile /path/to/my/logs/gunicorn-error.log
票数 15
EN

Stack Overflow用户

发布于 2021-01-07 21:53:07

如果您不想为logrotare而烦恼,那么您可以使用使用python日志记录工具的完整python/gunicorn解决方案。

使用下面的内容创建一个名为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

代码语言:javascript
运行
复制
[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
票数 14
EN

Stack Overflow用户

发布于 2019-04-12 10:01:09

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

代码语言:javascript
运行
复制
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)

因此,您可以像这样编写配置文件:

代码语言:javascript
运行
复制
/yourpath/log/gunicorn.* {
daily
rotate 30
compress
dateext
dateformat .%Y-%m-%d
notifempty
sharedscripts
postrotate
    kill -USR1 $(cat /yourpath/run/gunicorn.pid)
endscript
}

每天轮换

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36424335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档