flask 和 gunicorn 重复记录
flask and gunicorn duplicated logging
我是 运行 一个由 gunicorn 提供服务的烧瓶应用程序。我不确定为什么 app.logger.info('logging here')
在我的日志文件中被记录两次。
在provision.py
中:
# provision.py
Class ProvisionService(object):
...
def provision_order():
...
app.logger.log('logging starts here')
...
...
以下是/logs/provision_app/app-api_error.log
中的日志输出
[2021-03-18 11:54:35,810] INFO in provision: logging starts here
[2021-03-18 11:54:35 +0800] [13268] [INFO] logging starts here
我的应用服务设置如下:provision-app.service
[Unit]
Description=gunicorn service for provision API
After=network.target
[Service]
User=provision
Group=provision
PIDFile=/var/provision/pid-provision-api
WorkingDirectory=/opt/provision
ExecStart=/opt/provision/bin/gunicorn \
--pid /var/provision/pid-provision-api \
--error-logfile /logs/provision_app/app-api_error.log \
--access-logfile /logs/provision_app/app-api_access.log \
--bind=127.0.0.1:8084 provision.api:app
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
init.py
我正在设置日志记录的地方:
from flask import Flask
from flask_restful import Api
from flask_sqlalchemy import SQLAlchemy
from provision.conf import load_from_toml
import logging
import os.path
if os.path.isfile('/opt/provision/provision.toml'):
config = load_from_toml('/opt/provision/provision.toml')
else:
config = load_from_toml()
db = SQLAlchemy()
app = Flask(__name__)
api = Api(app)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = config['mysql']['db_uri']
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'pool_recycle': 3000, 'pool_pre_ping': True, }
db.init_app(app)
@app.before_first_request
def setup_logging():
gunicorn_error_logger = logging.getLogger('gunicorn.error')
app.logger.handlers.extend(gunicorn_error_logger.handlers)
app.logger.setLevel(gunicorn_error_logger.level)
知道日志记录设置中是否存在问题吗?
尽管配置了自定义记录器/处理程序,但 flask 可能使用默认记录器/处理程序,我的设置中有这些行
from flask.logging import default_handler
app.logger.removeHandler(default_handler) # use custom loggers instead
如果问题解决了请告诉我
我是 运行 一个由 gunicorn 提供服务的烧瓶应用程序。我不确定为什么 app.logger.info('logging here')
在我的日志文件中被记录两次。
在provision.py
中:
# provision.py
Class ProvisionService(object):
...
def provision_order():
...
app.logger.log('logging starts here')
...
...
以下是/logs/provision_app/app-api_error.log
[2021-03-18 11:54:35,810] INFO in provision: logging starts here
[2021-03-18 11:54:35 +0800] [13268] [INFO] logging starts here
我的应用服务设置如下:provision-app.service
[Unit]
Description=gunicorn service for provision API
After=network.target
[Service]
User=provision
Group=provision
PIDFile=/var/provision/pid-provision-api
WorkingDirectory=/opt/provision
ExecStart=/opt/provision/bin/gunicorn \
--pid /var/provision/pid-provision-api \
--error-logfile /logs/provision_app/app-api_error.log \
--access-logfile /logs/provision_app/app-api_access.log \
--bind=127.0.0.1:8084 provision.api:app
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
init.py
我正在设置日志记录的地方:
from flask import Flask
from flask_restful import Api
from flask_sqlalchemy import SQLAlchemy
from provision.conf import load_from_toml
import logging
import os.path
if os.path.isfile('/opt/provision/provision.toml'):
config = load_from_toml('/opt/provision/provision.toml')
else:
config = load_from_toml()
db = SQLAlchemy()
app = Flask(__name__)
api = Api(app)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = config['mysql']['db_uri']
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'pool_recycle': 3000, 'pool_pre_ping': True, }
db.init_app(app)
@app.before_first_request
def setup_logging():
gunicorn_error_logger = logging.getLogger('gunicorn.error')
app.logger.handlers.extend(gunicorn_error_logger.handlers)
app.logger.setLevel(gunicorn_error_logger.level)
知道日志记录设置中是否存在问题吗?
尽管配置了自定义记录器/处理程序,但 flask 可能使用默认记录器/处理程序,我的设置中有这些行
from flask.logging import default_handler
app.logger.removeHandler(default_handler) # use custom loggers instead
如果问题解决了请告诉我