[FIX] Actually log to a file (#44)

This commit is contained in:
CaliBrain
2025-01-07 14:04:50 -05:00
committed by GitHub
parent abff8de155
commit 2b80de35a8
5 changed files with 41 additions and 23 deletions
+8
View File
@@ -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):
"""
+4 -2
View File
@@ -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))
+1 -1
View File
@@ -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 "$@"
+15 -11
View File
@@ -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
+13 -9
View File
@@ -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