feat(logging): implement request logger

This commit is contained in:
Julian Lobbes 2022-12-01 19:58:40 +01:00
parent 1cda01eb1f
commit f501acf839
5 changed files with 49 additions and 2 deletions

View File

@ -30,6 +30,13 @@ LDAP_USERS_OU = 'ou=users,dc=example,dc=com'
# DN of the organizational unit beneath which groups are located.
LDAP_GROUPS_OU = 'ou=groups,dc=example,dc=com'
# If specified, the HTTP access log is saved to this file.
# Make sure the directory for the log file exists and is writeable by lumi2.
#LOG_FILE_PATH = '/path/to/file.log'
# Maximum log file size in Bytes. When exceeded, the log gets rotated.
# Set to 0 to disable log file rotation (can eat up disk space!)
#LOG_FILE_MAX_SIZE = 32_000_000
# Maximum size in Bytes for incoming requests, both for improved security and
# to limit the size of uploaded user profile pictures.
MAX_CONTENT_LENGTH = 8_000_000

View File

@ -8,11 +8,13 @@ services:
container_name: lumi2
command: flask --app /app/lumi2 --debug run --host 0.0.0.0 --port 80
volumes:
- ./config.py/:/app/config.py:ro
- ./lumi2/__init__.py:/app/lumi2/__init__.py:ro
- ./lumi2/auth.py:/app/lumi2/auth.py:ro
- ./lumi2/default_configuration.py:/app/lumi2/default_configuration.py:ro
- ./lumi2/exceptions.py:/app/lumi2/exceptions.py:ro
- ./lumi2/ldap.py:/app/lumi2/ldap.py:ro
- ./lumi2/logging.py:/app/lumi2/logging.py:ro
- ./lumi2/static/css:/app/lumi2/static/css:ro
- ./lumi2/static/images/base:/app/lumi2/static/images/base:ro
- ./lumi2/static/images/default:/app/lumi2/static/images/default:ro
@ -22,7 +24,6 @@ services:
- ./lumi2/usermodel.py:/app/lumi2/usermodel.py:ro
- ./lumi2/webapi.py:/app/lumi2/webapi.py:ro
- ./tests/fakedata.py/:/app/tests/fakedata.py:ro
- ./config.py/:/app/config.py:ro
environment:
- LUMI_CONFIG=/app/config.py
ports:

View File

@ -1,4 +1,5 @@
import os
import os, logging
from logging.handlers import RotatingFileHandler
from flask import Flask
from flask_restful import Api
@ -30,6 +31,27 @@ def create_app(test_config=None):
except OSError:
pass
# Set up logging
if not app.config['DEBUG']:
app.logger = logging.getLogger('lumi2')
app.logger.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
app.logger.addHandler(console_handler)
if 'LOG_FILE_PATH' in app.config:
file_handler = RotatingFileHandler(
app.config['LOG_FILE_PATH'],
maxBytes=app.config['LOG_FILE_MAX_SIZE'],
backupCount=1,
)
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
from lumi2.logging import log_request
app.after_request(log_request)
from . import auth
app.register_blueprint(auth.bp)

View File

@ -19,4 +19,6 @@ LDAP_BASE_DN = 'dc=example,dc=com'
LDAP_USERS_OU = 'ou=users,dc=example,dc=com'
LDAP_GROUPS_OU = 'ou=groups,dc=example,dc=com'
LOG_FILE_MAX_SIZE = 0
MAX_CONTENT_LENGTH = 8_000_000

15
lumi2/logging.py Normal file
View File

@ -0,0 +1,15 @@
"""Logging operations for lumi2."""
import logging as l
from time import strftime
from flask import request
def log_request(response):
logger = l.getLogger('lumi2')
timestamp = strftime('[%Y-%b-%d %H:%M]')
logger.info(f"{timestamp} {request.remote_addr} {request.method} {request.scheme} {request.full_path} {response.status_code}")
return response