Make it possible to configure different log levels for stderr and file logging

This commit is contained in:
Patrick Jentsch 2021-09-16 11:04:47 +02:00
parent e2c68a1a80
commit 2af74db46f
2 changed files with 39 additions and 20 deletions

View File

@ -121,10 +121,6 @@ NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI=
# DEFAULT: %Y-%m-%d %H:%M:%S
# NOPAQUE_LOG_DATE_FORMAT=
# DEFAULT: <nopaque-basedir>/logs
# NOTE: Use `.` as <nopaque-basedir>
# NOPAQUE_LOG_DIR=
# DEFAULT: [%(asctime)s] %(levelname)s in %(pathname)s (function: %(funcName)s, line: %(lineno)d): %(message)s
# NOPAQUE_LOG_FORMAT=
@ -132,9 +128,25 @@ NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI=
# CHOOSE ONE: CRITICAL, ERROR, WARNING, INFO, DEBUG
# NOPAQUE_LOG_LEVEL=
# CHOOSE ONE: False, True
# DEFAULT: True
# NOPAQUE_LOG_FILE_ENABLED=
# DEFAULT: <nopaque-basedir>/logs
# NOTE: Use `.` as <nopaque-basedir>
# NOPAQUE_LOG_FILE_DIR=
# DEFAULT: NOPAQUE_LOG_LEVEL
# CHOOSE ONE: CRITICAL, ERROR, WARNING, INFO, DEBUG
# NOPAQUE_LOG_FILE_LEVEL=
# CHOOSE ONE: False, True
# DEFAULT: False
# NOPAQUE_LOG_TO_STDOUT=
# NOPAQUE_LOG_STDERR_ENABLED=
# DEFAULT: NOPAQUE_LOG_LEVEL
# CHOOSE ONE: CRITICAL, ERROR, WARNING, INFO, DEBUG
# NOPAQUE_LOG_STDERR_LEVEL=
# DEFAULT: 0
# Number of values to trust for X-Forwarded-For

View File

@ -51,18 +51,24 @@ class Config:
NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI = \
os.environ.get('NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI')
NOPAQUE_LOG_DATE_FORMAT = os.environ.get('NOPAQUE_LOG_DATE_FORMAT',
'%Y-%m-%d %H:%M:%S')
NOPAQUE_LOG_DIR = os.environ.get('NOPAQUE_LOG_DIR',
os.path.join(basedir, 'logs'))
NOPAQUE_LOG_DATE_FORMAT = \
os.environ.get('NOPAQUE_LOG_DATE_FORMAT', '%Y-%m-%d %H:%M:%S')
NOPAQUE_LOG_FORMAT = os.environ.get(
'NOPAQUE_LOG_DATE_FORMAT',
'[%(asctime)s] %(levelname)s in '
'%(pathname)s (function: %(funcName)s, line: %(lineno)d): %(message)s'
)
NOPAQUE_LOG_LEVEL = os.environ.get('NOPAQUE_LOG_LEVEL', 'INFO')
NOPAQUE_LOG_TO_STDOUT = \
os.environ.get('NOPAQUE_LOG_TO_STDOUT', 'false').lower() == 'true'
NOPAQUE_LOG_FILE_ENABLED = \
os.environ.get('NOPAQUE_LOG_FILE_ENABLED', 'true').lower() == 'true'
NOPAQUE_LOG_FILE_DIR = \
os.environ.get('NOPAQUE_LOG_FILE_DIR', os.path.join(basedir, 'logs'))
NOPAQUE_LOG_FILE_LEVEL = \
os.environ.get('NOPAQUE_LOG_FILE_LEVEL', NOPAQUE_LOG_LEVEL)
NOPAQUE_LOG_STDERR_ENABLED = \
os.environ.get('NOPAQUE_LOG_STDERR_ENABLED', 'false').lower() == 'true'
NOPAQUE_LOG_STDERR_LEVEL = \
os.environ.get('NOPAQUE_LOG_STDERR_LEVEL', NOPAQUE_LOG_LEVEL)
NOPAQUE_PROXY_FIX_X_FOR = \
int(os.environ.get('NOPAQUE_PROXY_FIX_X_FOR', '0'))
@ -79,30 +85,31 @@ class Config:
def init_app(cls, app):
# Set up logging according to the corresponding (NOPAQUE_LOG_*)
# configurations
# ... But first remove all existing handlers
app.logger.setLevel(app.config.get('NOPAQUE_LOG_LEVEL'))
# Remove existing handlers
for handler in app.logger.handlers:
app.logger.removeHandler(handler)
# Setup handlers
formatter = logging.Formatter(
fmt=app.config.get('NOPAQUE_LOG_FORMAT'),
datefmt=app.config.get('NOPAQUE_LOG_DATE_FORMAT')
)
if app.config.get('NOPAQUE_LOG_TO_STDOUT'):
if app.config.get('NOPAQUE_LOG_STDERR_ENABLED'):
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
stream_handler.setLevel(app.config.get('NOPAQUE_LOG_LEVEL'))
stream_handler.setLevel(app.config.get('NOPAQUE_LOG_STDERR_LEVEL'))
app.logger.addHandler(stream_handler)
else:
if not os.path.exists(app.config.get('NOPAQUE_LOG_DIR')):
os.mkdir(app.config.get('NOPAQUE_LOG_DIR'))
if app.config.get('NOPAQUE_LOG_FILE_ENABLED'):
if not os.path.exists(app.config.get('NOPAQUE_LOG_FILE_DIR')):
os.mkdir(app.config.get('NOPAQUE_LOG_FILE_DIR'))
rotating_file_handler = RotatingFileHandler(
os.path.join(app.config.get('NOPAQUE_LOG_DIR'), 'nopaque.log'),
os.path.join(app.config.get('NOPAQUE_LOG_FILE_DIR'), 'nopaque.log'), # noqa
maxBytes=10240,
backupCount=10
)
rotating_file_handler.setFormatter(formatter)
rotating_file_handler.setLevel(app.config.get('NOPAQUE_LOG_LEVEL'))
rotating_file_handler.setLevel(app.config.get('NOPAQUE_LOG_FILE_LEVEL')) # noqa
app.logger.addHandler(rotating_file_handler)
app.logger.setLevel(app.config.get('NOPAQUE_LOG_LEVEL'))
# Set up and apply the ProxyFix middleware according to the
# corresponding (NOPAQUE_PROXY_FIX_*) configurations