如何指定 gunicorn 日志最大大小

Fei*_*iii 5 gunicorn

我正在运行 gunicorn 作为 guiconrn --bind=0.0.0.0:5000 --log-file gunicorn.log myapp:app

似乎 gunicorn.log 不断增长。有没有办法指定日志文件的最大大小,这样如果它达到最大大小,它就会覆盖它。

谢谢!!

Pau*_*ulo 0

太长了;

我相信可能有一个“仅限 python”的解决方案,使用python 内部库中提供的旋转文件处理程序。(至少 3.10)

去测试

我创建了一个宠物项目供您摆弄:

  1. 创建以下 python 文件

test_logs.py

import logging
import logging.config
import time

logging.config.fileConfig(fname='log.conf', disable_existing_loggers=False)


while True:
    time.sleep(0.5)
    logging.debug('This is a debug message')
    logging.info('This is an info message')
    logging.warning('This is a warning message')
    logging.error('This is an error message')
    logging.critical('This is a critical message')
Run Code Online (Sandbox Code Playgroud)
  1. 创建以下配置文件

log.conf

[loggers]
keys=root

[handlers]
keys=rotatingHandler

[formatters]
keys=sampleFormatter

[logger_root]
level=DEBUG
handlers=rotatingHandler

[handler_rotatingHandler]
class=logging.handlers.RotatingFileHandler
level=DEBUG
formatter=sampleFormatter
args=('./logs/logs.log', 'a', 1200, 1, 'utf-8')

[formatter_sampleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
Run Code Online (Sandbox Code Playgroud)
  1. 创建./logs目录
  2. 跑步python test_logs.py

了解

正如您可能已经注意到的,允许此行为的设置是logging.handlers.RotatingFileHandler和提供的参数args=('./logs/logs.log', 'a', 1200, 10, 'utf-8')

RotatingFileHandler写入文件的流处理程序。这允许 2 个感兴趣的参数:

  • maxBytes任意设置为1200
  • backupCount任意设置为10

行为是,达到1200 Bytes大小后,文件将被关闭,重命名为/logs/logs.log.<a number up to 10>并打开一个新文件。

BUT为任意maxBytesbackupCount为 0。不进行旋转!

在古尼康

根据文档,您可以提供配置文件。

这可能看起来像:

guiconrn --bind=0.0.0.0:5000 --log-config log.conf myapp:app
Run Code Online (Sandbox Code Playgroud)

您需要将其调整为您现有的设置。