我在网上搜索,但没有得到"how to use log rotation with Gunicorn?"
的具体答案或示例。
如果有人能提供一个例子那就太好了。
发布于 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
中
/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
脚本中声明的,类似于:
/my/virtualenv/bin/gunicorn OPTIONS \
--access-logfile /path/to/my/logs/gunicorn-access.log \
--error-logfile /path/to/my/logs/gunicorn-error.log
发布于 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
[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
发布于 2019-04-12 10:01:09
文档链接: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
}
每天轮换
https://stackoverflow.com/questions/36424335
复制相似问题