This commit is contained in:
2025-12-09 00:13:03 +01:00
parent 86d0da5ca7
commit daffc4f458
3 changed files with 149 additions and 30 deletions

View File

@@ -1,3 +1,4 @@
from logging.config import dictConfig
from flask import Flask
from flask_socketio import SocketIO
from .models import db
@@ -8,6 +9,60 @@ from .seed_db import seed_courses
socketio = SocketIO(async_mode='eventlet', cors_allowed_origins="*")
def create_app():
# Configure logging BEFORE creating the application object
dictConfig({
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
},
'plain': {
'format': '%(message)s',
}
},
'handlers': {
# Console handler (keeps logs visible in terminal)
'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
},
# File handler for Application Logs (slides.log)
'slides_file': {
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'slides.log',
'maxBytes': 1024 * 1024 * 10, # 10 MB
'backupCount': 5,
'formatter': 'default',
'level': 'INFO',
'encoding': 'utf8'
},
# File handler for Request Logs (requests.log)
'requests_file': {
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'requests.log',
'maxBytes': 1024 * 1024 * 10, # 10 MB
'backupCount': 5,
'formatter': 'plain', # Requests already have timestamps/formatting
'level': 'INFO',
'encoding': 'utf8'
}
},
'root': {
'level': 'INFO',
'handlers': ['wsgi', 'slides_file']
},
'loggers': {
# Isolate the Werkzeug request logger
'werkzeug': {
'level': 'INFO',
'handlers': ['wsgi', 'requests_file'],
'propagate': False # Prevent requests from leaking into slides.log
}
}
})
app = Flask(__name__, static_folder='static', static_url_path='/static')
app.config.from_object('config.Config')
db.init_app(app)
@@ -20,7 +75,6 @@ def create_app():
init_serializer(app)
init_auth(app)
socketio.init_app(app)
from .routes import main