Logging 如何使用Gunicorn执行日志旋转?
我在网上搜索了一下,但没有得到具体的答案或示例“如何使用Gunicorn的日志旋转?”Logging 如何使用Gunicorn执行日志旋转?,logging,gunicorn,logrotate,Logging,Gunicorn,Logrotate,我在网上搜索了一下,但没有得到具体的答案或示例“如何使用Gunicorn的日志旋转?” 如果有人提供一个例子,那就太好了。Gunicorn的文档说,您可以使用logrotate(一个linux命令)设置日志旋转: 可以使用logrotate自动旋转和压缩日志 文档链接: 所以我猜Gunicorn没有办法旋转原木 下面是我的配置文件示例,位于/etc/logrotate.d/my_app中: /path/to/my/logs/gunicorn-access.log /path/to/my/logs
如果有人提供一个例子,那就太好了。Gunicorn的文档说,您可以使用
logrotate
(一个linux命令)设置日志旋转:
可以使用logrotate自动旋转和压缩日志
文档链接:
所以我猜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\u start
脚本中声明,类似于:
/my/virtualenv/bin/gunicorn OPTIONS \
--access-logfile /path/to/my/logs/gunicorn-access.log \
--error-logfile /path/to/my/logs/gunicorn-error.log
文档链接:
因此,您可以这样编写配置文件:
/yourpath/log/gunicorn.* {
daily
rotate 30
compress
dateext
dateformat .%Y-%m-%d
notifempty
sharedscripts
postrotate
kill -USR1 $(cat /yourpath/run/gunicorn.pid)
endscript
}
每天轮换如果您不想麻烦使用logrotare,您可以使用使用python日志记录功能的完整python/gunicorn解决方案 使用下面的内容创建一个名为
log.conf
的文件。这将每天轮换错误日志文件和访问日志文件,并将日志保留90天。日志级别设置为INFO
然后,启动gunicorn添加命令行参数--log config log.conf
。删除--访问日志文件
、--错误日志文件
和--日志级别
参数,因为log.conf将处理所有问题
有关如何配置记录器的更多信息,请参阅
下面是log.conf
的内容。再举一个例子,看看
嗯
我们在哪里指定gunicorn使用my_app中的设置。日志旋转由logrotate完成,而不是gunicorn。
/yourpath/log/gunicorn.* {
daily
rotate 30
compress
dateext
dateformat .%Y-%m-%d
notifempty
sharedscripts
postrotate
kill -USR1 $(cat /yourpath/run/gunicorn.pid)
endscript
}
[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