我正在运行 gunicorn 作为 guiconrn --bind=0.0.0.0:5000 --log-file gunicorn.log myapp:app
似乎 gunicorn.log 不断增长。有没有办法指定日志文件的最大大小,这样如果它达到最大大小,它就会覆盖它。
谢谢!!
我相信可能有一个“仅限 python”的解决方案,使用python 内部库中提供的旋转文件处理程序。(至少 3.10)
我创建了一个宠物项目供您摆弄:
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)
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)
./logs目录python test_logs.py正如您可能已经注意到的,允许此行为的设置是logging.handlers.RotatingFileHandler和提供的参数args=('./logs/logs.log', 'a', 1200, 10, 'utf-8')
RotatingFileHandler是写入文件的流处理程序。这允许 2 个感兴趣的参数:
maxBytes任意设置为1200backupCount任意设置为10行为是,达到1200 Bytes大小后,文件将被关闭,重命名为/logs/logs.log.<a number up to 10>并打开一个新文件。
BUT为任意maxBytes或backupCount为 0。不进行旋转!
根据文档,您可以提供配置文件。
这可能看起来像:
guiconrn --bind=0.0.0.0:5000 --log-config log.conf myapp:app
Run Code Online (Sandbox Code Playgroud)
您需要将其调整为您现有的设置。
| 归档时间: |
|
| 查看次数: |
201 次 |
| 最近记录: |