From 2b80de35a8a8c1ee4554bfa90da6afc6aa5249e8 Mon Sep 17 00:00:00 2001 From: CaliBrain Date: Tue, 7 Jan 2025 14:04:50 -0500 Subject: [PATCH] [FIX] Actually log to a file (#44) --- app.py | 8 ++++++++ config.py | 6 ++++-- entrypoint.sh | 2 +- logger.py | 26 +++++++++++++++----------- readme.md | 22 +++++++++++++--------- 5 files changed, 41 insertions(+), 23 deletions(-) diff --git a/app.py b/app.py index 09076cde..ada7abab 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,6 @@ """Flask web application for book download service with URL rewrite support.""" +import logging import io, re, os from flask import Flask, request, jsonify, render_template, send_file, send_from_directory from werkzeug.middleware.proxy_fix import ProxyFix @@ -16,6 +17,13 @@ app.wsgi_app = ProxyFix(app.wsgi_app) # type: ignore app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 # Disable caching app.config['APPLICATION_ROOT'] = '/' +# Flask logger +app.logger.handlers = logger.handlers +app.logger.setLevel(logger.level) +# Also handle Werkzeug's logger +werkzeug_logger = logging.getLogger('werkzeug') +werkzeug_logger.handlers = logger.handlers +werkzeug_logger.setLevel(logger.level) def register_dual_routes(app): """ diff --git a/config.py b/config.py index 31e1a906..fa66c66a 100644 --- a/config.py +++ b/config.py @@ -7,7 +7,8 @@ _SUPPORTED_BOOK_LANGUAGE = ['en','zh','ru','es','fr','de','it','pt','pl','bg','n # Directory settings BASE_DIR = Path(__file__).resolve().parent -LOG_DIR = Path("/var/logs") +LOG_DIR = Path("/var/log/cwa-book-downloader") +LOG_DIR.mkdir(exist_ok=True) TMP_DIR = Path(os.getenv("TMP_DIR", "/tmp/cwa-book-downloader")) @@ -43,5 +44,6 @@ FLASK_PORT = int(os.getenv("FLASK_PORT", 5003)) FLASK_DEBUG = os.getenv("FLASK_DEBUG", "False").lower() == "true" # Logging settings -LOG_FILE = f"{LOG_DIR}/cwa-bookd-ownloader.log" +ENABLE_LOGGING = os.getenv("ENABLE_LOGGING", "true").lower() in ["true", "yes", "1", "y"] +LOG_FILE = LOG_DIR / "cwa-bookd-downloader.log" MAIN_LOOP_SLEEP_TIME = int(os.getenv("MAIN_LOOP_SLEEP_TIME", 5)) diff --git a/entrypoint.sh b/entrypoint.sh index 4497f1e6..ca8f07b5 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -15,7 +15,7 @@ if ! id -u "$UID" >/dev/null 2>&1; then fi # Adjust ownership of application directories -chown -R $UID:$GID /app "$INGEST_DIR" /var/logs +chown -R $UID:$GID /app "$INGEST_DIR" /var/log/cwa-book-downloader # Switch to the created user and execute the main command exec gosu $UID "$@" diff --git a/logger.py b/logger.py index 8153f30e..1747d605 100644 --- a/logger.py +++ b/logger.py @@ -2,10 +2,11 @@ import logging import sys +from pathlib import Path from logging.handlers import RotatingFileHandler -from config import FLASK_DEBUG +from config import FLASK_DEBUG, LOG_FILE, ENABLE_LOGGING -def setup_logger(name: str, log_file: str = "") -> logging.Logger: +def setup_logger(name: str, log_file: Path = LOG_FILE) -> logging.Logger: """Set up and configure a logger instance. Args: @@ -39,13 +40,16 @@ def setup_logger(name: str, log_file: str = "") -> logging.Logger: logger.addHandler(error_handler) # File handler if log file is specified - if log_file.strip() != "": - file_handler = RotatingFileHandler( - log_file.strip(), - maxBytes=10485760, # 10MB - backupCount=5 - ) - file_handler.setFormatter(formatter) - logger.addHandler(file_handler) - + try: + if ENABLE_LOGGING: + file_handler = RotatingFileHandler( + log_file, + maxBytes=10485760, # 10MB + backupCount=5 + ) + file_handler.setFormatter(formatter) + logger.addHandler(file_handler) + except Exception as e: + logger.error(f"Failed to create log file: {e}") + return logger \ No newline at end of file diff --git a/readme.md b/readme.md index 887ec6ff..31b4b2c6 100644 --- a/readme.md +++ b/readme.md @@ -51,14 +51,17 @@ An intuitive web interface for searching and requesting book downloads, designed #### Application Settings -| Variable | Description | Default Value | -| ------------- | ----------------------- | ------------------ | -| `FLASK_PORT` | Web interface port | `8084` | -| `FLASK_DEBUG` | Debug mode toggle | `false` | -| `FLASK_HOST` | Web interface binding | `0.0.0.0` | -| `INGEST_DIR` | Book download directory | `/cwa-book-ingest` | -| `UID` | Runtime user ID | `1000` | -| `GID` | Runtime group ID | `100` | +| Variable | Description | Default Value | +| ----------------- | ----------------------- | ------------------ | +| `FLASK_PORT` | Web interface port | `8084` | +| `FLASK_DEBUG` | Debug mode toggle | `false` | +| `FLASK_HOST` | Web interface binding | `0.0.0.0` | +| `INGEST_DIR` | Book download directory | `/cwa-book-ingest` | +| `UID` | Runtime user ID | `1000` | +| `GID` | Runtime group ID | `100` | +| `ENABLE_LOGGING` | Enable log file | `true` | + +If logging is enabld, log folder default location is `var/log/cwa-book-downloader` #### Download Settings @@ -71,7 +74,8 @@ An intuitive web interface for searching and requesting book downloads, designed | `BOOK_LANGUAGE` | Preferred language for books | `en` | | `AA_DONATOR_KEY` | Optional Donator key for Anna's Archive fast download API | `` | -Note that PDF are NOT supported at the moment (they do not get ingested by CWA, but if you want to just download them locally, you can add `pdf` to the `SUPPORTED_FORMATS` env +Note that PDF are NOT supported at the moment (they do not get ingested by CWA, but if you want to just download them locally, you can add `pdf` to the `SUPPORTED_FORMATS` env) +If you change `BOOK_LANGUAGE`, you can add multiple comma separated languages, such as `en,fr,ru` etc. #### AA