我在网上搜索但没有得到具体的答案或例子"how to use log rotation with Gunicorn?".
如果有人提供一个例子,那就太棒了.
Cha*_*lie 11
如果您不想打扰 logrotare,您可以使用使用 python 日志记录工具的完整 python/gunicorn 解决方案。
创建一个以log.conf以下内容命名的文件。这将每天轮换错误日志文件和访问日志文件,并将日志保留 90 天。日志级别设置为 INFO。
然后,启动 gunicorn 添加命令行参数--log-config log.conf。删除--access-logfile, --error-logfile和--log-level参数,因为 log.conf 会处理一切。
Python 文档中提供了有关如何配置记录器的更多信息。
下面是内容log.conf。另一个例子请看gunicorn 源代码。
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
Run Code Online (Sandbox Code Playgroud)
paw*_*moy 10
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
}
Run Code Online (Sandbox Code Playgroud)
每月旋转,为旋转的文件添加-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
Run Code Online (Sandbox Code Playgroud)
小智 9
文档链接: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)
Run Code Online (Sandbox Code Playgroud)
所以你可以像这样编写配置文件:
/yourpath/log/gunicorn.* {
daily
rotate 30
compress
dateext
dateformat .%Y-%m-%d
notifempty
sharedscripts
postrotate
kill -USR1 $(cat /yourpath/run/gunicorn.pid)
endscript
}
Run Code Online (Sandbox Code Playgroud)
每天轮换
| 归档时间: |
|
| 查看次数: |
5066 次 |
| 最近记录: |