带gunicorn的日志模块

2024-06-16 11:10:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我用gunicorn这样运行我的代码:

gunicorn --bind 0.0.0.0:6435 --access-logfile=allOuts.log --error-logfile=allErs.log --certfile=cert.pem --keyfile=key.pem --ssl-version=5  --ciphers=EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH app:app -w=5 --timeout=500 --daemon

我的日志模块代码是:

import logging

logger = logging.getLogger(__name__)

logger.setLevel(logging.DEBUG)
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='/app/python/logs/firstlogs.log', filemode='w')

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s\n')

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

但我只能看到一些日志。我的直觉是我能够看到一些日志,这些日志只来自一个特定的工人。如何将所有worker的日志定向到同一个日志文件?或者如何为不同的工作人员维护不同的日志文件

我在这里遇到了一个类似的问题:How to use the logging module in Python with gunicorn,但找不到解决方案


Tags: to代码debuglogappformatterloggingch