Refactor logging and drop deprecated FLASK_ENV

Move logging setup into max/log.py. The file handler is only used outside
debug mode and its directory is configurable via LOG_DIR. FLASK_ENV is
deprecated in Flask; debug mode is now controlled via FLASK_DEBUG.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
finnandClaude Opus 5.5 committed 2026-10-21 08:21:07 +00:00
1 parent 65dddea3bc
commit 7c2b9fb3f4
5 files changed
+40 -40

No files matched your search

-2
View File
@@ -9,8 +9,6 @@ services:
- home:/home/dev
environment:
- PYTHONPATH=/workspaces/max
- FLASK_APP=max.app
- FLASK_ENV=development
- PGSQL_HOST=postgresql
- PGSQL_PORT=5432
- PGSQL_USER=max
+1
View File
@@ -1 +1,2 @@
*.pyc
log/
-1
View File
@@ -12,7 +12,6 @@ RUN mkdir log
ENV PYTHONPATH=/app/
ENV FLASK_APP=max.app
ENV FLASK_ENV=production
COPY max/ ./max
COPY scripts/entrypoint.sh .
+1 -37
View File
@@ -1,8 +1,5 @@
import base64
import logging
import os
import sys
from logging.config import dictConfig
from flask import Flask
from werkzeug.routing import Rule as WerkzeugRule
@@ -11,6 +8,7 @@ from .auth import AuthMiddleware, init as init_auth
from .cli import register_cli
from .csrf import CsrfMiddleware
from .db import database
from .log import init_logging
from .middlewares import (
ExceptionMiddleware,
I18NMiddleware,
@@ -27,40 +25,6 @@ class Rule(WerkzeugRule):
super().__init__(*args, **kwargs) # do not set methods
def init_logging(app):
dictConfig(
{
"version": 1,
"formatters": {
"default": {"format": "[%(asctime)s] [%(levelname)s]: %(message)s"}
},
"handlers": {
"wsgi": {
"class": "logging.StreamHandler",
"stream": "ext://flask.logging.wsgi_errors_stream",
"formatter": "default",
},
"stdout": {
"class": "logging.StreamHandler",
"stream": sys.stdout,
"formatter": "default",
},
"file": {
"class": "logging.handlers.RotatingFileHandler",
"filename": "/app/log/max.log",
"formatter": "default",
"maxBytes": 1024 * 1024,
"backupCount": 5,
},
},
"root": {"level": "INFO", "handlers": ["stdout", "file"]},
"gunicorn": {"level": "INFO", "handlers": ["stdout", "file"]},
}
)
logger = logging.getLogger(app.name)
logger.setLevel(logging.DEBUG if app.debug else logging.INFO)
def main():
app = Flask(__name__)
init_logging(app)
+38
View File
@@ -0,0 +1,38 @@
import logging
import os
import sys
from logging.config import dictConfig
def init_logging(app):
handlers = {
"stdout": {
"class": "logging.StreamHandler",
"stream": sys.stdout,
"formatter": "default",
},
}
active_handlers = ["stdout"]
if not app.debug:
handlers["file"] = {
"class": "logging.handlers.RotatingFileHandler",
"filename": os.path.join(os.environ.get("LOG_DIR", "/app/log"), "max.log"),
"formatter": "default",
"maxBytes": 1024 * 1024,
"backupCount": 5,
}
active_handlers.append("file")
dictConfig(
{
"version": 1,
"formatters": {
"default": {"format": "[%(asctime)s] [%(levelname)s]: %(message)s"}
},
"handlers": handlers,
"root": {"level": "INFO", "handlers": active_handlers},
"gunicorn": {"level": "INFO", "handlers": active_handlers},
}
)
logger = logging.getLogger(app.name)
logger.setLevel(logging.DEBUG if app.debug else logging.INFO)