From 5e920ebbfe540570b1fed17c13ba099e853d477b Mon Sep 17 00:00:00 2001 From: Michael Grote Date: Thu, 13 Apr 2023 15:11:26 +0200 Subject: [PATCH] add logging --- app.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/app.py b/app.py index e8e7c6c..51995dd 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,9 @@ import re import uuid from flask import Flask, request, jsonify, send_from_directory import datetime +import logging +from logging.handlers import RotatingFileHandler +import sys app = Flask(__name__) app.config['UPLOAD_DIRECTORY'] = os.environ.get('UPLOAD_DIRECTORY', '/uploads') @@ -12,6 +15,28 @@ VALID_FILENAME_REGEX = r'^[a-zA-Z0-9\-_\.]+$' AUTH_TOKEN = os.environ.get('AUTH_TOKEN', 'myuploadtoken') + + +# Configure root logger to output to stdout +root = logging.getLogger() +root.setLevel(logging.DEBUG) +handler = logging.StreamHandler(sys.stdout) +handler.setLevel(logging.DEBUG) +formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') +handler.setFormatter(formatter) +root.addHandler(handler) + +# Configure Flask logger to output to file +flask_logger = logging.getLogger('werkzeug') +flask_logger.setLevel(logging.DEBUG) +log_file = os.path.join(app.root_path, 'logs', 'app.log') +handler = RotatingFileHandler(log_file, maxBytes=10000000, backupCount=5) +handler.setLevel(logging.DEBUG) +formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') +handler.setFormatter(formatter) +flask_logger.addHandler(handler) + + def is_valid_filename(filename): return bool(re.match(VALID_FILENAME_REGEX, filename))