From 42111cf32023f72cbbfec02de229cada68bc78bb Mon Sep 17 00:00:00 2001 From: gazev Date: Sun, 17 Nov 2024 02:43:38 +0000 Subject: [PATCH] fix: logging on gunicorn fixes #4 --- .env.example | 1 - Dockerfile | 2 +- app/__init__.py | 26 +++++++++++++++----------- app/config.py | 5 ++--- docker-compose.yaml | 3 +-- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/.env.example b/.env.example index 7636a9f..b68fe01 100644 --- a/.env.example +++ b/.env.example @@ -6,7 +6,6 @@ ROLES_PATH="data/roles.json" STATIC_DIR="data/static/" MAX_FILE_UPLOAD_LENGTH="16777216" -LOG_LEVEL="INFO" LOGS_PATH="data/logs/app.log" ADMIN_USERNAME="admin" diff --git a/Dockerfile b/Dockerfile index 0bcbec0..2a87d57 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,4 +8,4 @@ EXPOSE 8000 RUN chmod u+x entrypoint.sh ENTRYPOINT [ "./entrypoint.sh" ] -CMD ["gunicorn", "--bind", "0.0.0.0:8000", "app:create_app()"] \ No newline at end of file +CMD ["gunicorn", "--bind", "0.0.0.0:8000", "app:create_app()", "--log-level", "info", "--access-logfile", "/hs-api/data/logs/access.log", "--error-logfile", "/hs-api/data/logs/error.log"] \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py index 30757d1..ba302a3 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -11,6 +11,7 @@ from app.extensions import roles_handler from app.extensions import logos_handler + def create_app(config_class=Config): flask_app = Flask(__name__) flask_app.config.from_object(config_class) @@ -76,19 +77,22 @@ def register_commands(app: Flask): register_create_admin_user_command(app) def setup_logger(app: Flask): + """ + If ran with gunicorn simply sets Flask logger to gunicorn ones, configured through cli arguments. + If ran with Flask development server simply logs at debug level or default to stdout. """ + if app.debug: + app.logger.setLevel(logging.DEBUG) + return + + # create logs folder logs_path = app.config.get("LOGS_PATH") - if app.debug or logs_path == basedir: # don't set logger in debug or if not log file - return - - levels = {"DEBUG": logging.DEBUG, "INFO": logging.INFO, "WARNING": logging.WARNING} - log_dir = os.path.dirname(logs_path) if not os.path.exists(log_dir): os.makedirs(log_dir) - app.logger.setLevel(levels[app.config.get("LOG_LEVEL")]) - handler = logging.FileHandler(logs_path) - BASIC_FORMAT = "[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s" - handler.setFormatter(logging.Formatter(BASIC_FORMAT)) - app.logger.addHandler(handler) - logging.getLogger("werkzeug").addHandler(handler) # Root logger for all logs + # ran in gunicorn, use gunicorn handlers set through cli arguments + if __name__ != '__main__': + gunicorn_logger = logging.getLogger('gunicorn.error') + app.logger.handlers = gunicorn_logger.handlers + app.logger.setLevel(gunicorn_logger.level) + return diff --git a/app/config.py b/app/config.py index 970a86e..87b2d19 100644 --- a/app/config.py +++ b/app/config.py @@ -41,12 +41,11 @@ class Config: MAX_CONTENT_LENGTH = _get_int_env_or_default("MAX_FILE_UPLOAD_LENGTH", 16 * 1024 * 1024) LOGS_PATH = os.path.join(basedir, _get_env_or_default("LOGS_PATH", "")) - LOG_LEVEL = _get_env_or_default("LOG_LEVEL", "INFO") FRONTEND_ORIGIN = _get_env_or_default("FRONTEND_ORIGIN", "http://localhost:3000") - ADMIN_USERNAME = _get_env_or_default("ADMIN_USERNAME", "admin") - ADMIN_PASSWORD = _get_env_or_default("ADMIN_PASSWORD", "admin") + ADMIN_USERNAME = _get_env_or_default("ADMIN_USERNAME", "") + ADMIN_PASSWORD = _get_env_or_default("ADMIN_PASSWORD", "") CLIENT_ID = _get_env_or_default("CLIENT_ID", "") CLIENT_SECRET = _get_env_or_default("CLIENT_SECRET", "") diff --git a/docker-compose.yaml b/docker-compose.yaml index be9f752..a32a621 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -12,8 +12,7 @@ services: - STATIC_DIR=data/static/ - ROLES_PATH=data/roles.json - SESSION_DIR=data/flask_sessions/ - - LOGS_PATH=data/logs/app.log - - LOG_LEVEL=INFO + - LOGS_PATH=data/logs/ - MAX_FILE_UPLOAD_LENGTH=16777216 - CLIENT_ID= - CLIENT_SECRET=