From e411c0fded094722d7ff0dff5d3b0bbf9b9ce959 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sun, 14 Jul 2019 08:18:45 +0200 Subject: [PATCH] Fix logging in debug mode --- cps/constants.py | 10 +++++++++- cps/gdriveutils.py | 8 ++++---- cps/logger.py | 16 +++++++++------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/cps/constants.py b/cps/constants.py index 8d0002f1..b5fa8ba2 100644 --- a/cps/constants.py +++ b/cps/constants.py @@ -22,6 +22,7 @@ import sys import os from collections import namedtuple +HOME_CONFIG = False # Base dir is parent of current file, necessary if called from different folder if sys.version_info < (3, 0): @@ -33,7 +34,14 @@ else: STATIC_DIR = os.path.join(BASE_DIR, 'cps', 'static') TEMPLATES_DIR = os.path.join(BASE_DIR, 'cps', 'templates') TRANSLATIONS_DIR = os.path.join(BASE_DIR, 'cps', 'translations') -CONFIG_DIR = os.environ.get('CALIBRE_DBPATH', BASE_DIR) + +if HOME_CONFIG: + home_dir = os.path.join(os.path.expanduser("~"),".calibre-web") + if not os.path.exists(home_dir): + os.makedirs(home_dir) + CONFIG_DIR = os.environ.get('CALIBRE_DBPATH', home_dir) +else: + CONFIG_DIR = os.environ.get('CALIBRE_DBPATH', BASE_DIR) ROLE_USER = 0 << 0 diff --git a/cps/gdriveutils.py b/cps/gdriveutils.py index 4ec7f68e..4ebbb0c7 100644 --- a/cps/gdriveutils.py +++ b/cps/gdriveutils.py @@ -39,12 +39,12 @@ except ImportError: gdrive_support = False from . import logger, cli, config -from .constants import BASE_DIR as _BASE_DIR +from .constants import CONFIG_DIR as _CONFIG_DIR -SETTINGS_YAML = os.path.join(_BASE_DIR, 'settings.yaml') -CREDENTIALS = os.path.join(_BASE_DIR, 'gdrive_credentials') -CLIENT_SECRETS = os.path.join(_BASE_DIR, 'client_secrets.json') +SETTINGS_YAML = os.path.join(_CONFIG_DIR, 'settings.yaml') +CREDENTIALS = os.path.join(_CONFIG_DIR, 'gdrive_credentials') +CLIENT_SECRETS = os.path.join(_CONFIG_DIR, 'client_secrets.json') log = logger.create() diff --git a/cps/logger.py b/cps/logger.py index 3a540683..ddadb472 100644 --- a/cps/logger.py +++ b/cps/logger.py @@ -23,7 +23,7 @@ import logging from logging import Formatter, StreamHandler from logging.handlers import RotatingFileHandler -from .constants import BASE_DIR as _BASE_DIR +from .constants import CONFIG_DIR as _CONFIG_DIR ACCESS_FORMATTER_GEVENT = Formatter("%(message)s") @@ -31,8 +31,8 @@ ACCESS_FORMATTER_TORNADO = Formatter("[%(asctime)s] %(message)s") FORMATTER = Formatter("[%(asctime)s] %(levelname)5s {%(name)s:%(lineno)d} %(message)s") DEFAULT_LOG_LEVEL = logging.INFO -DEFAULT_LOG_FILE = os.path.join(_BASE_DIR, "calibre-web.log") -DEFAULT_ACCESS_LOG = os.path.join(_BASE_DIR, "access.log") +DEFAULT_LOG_FILE = os.path.join(_CONFIG_DIR, "calibre-web.log") +DEFAULT_ACCESS_LOG = os.path.join(_CONFIG_DIR, "access.log") LOG_TO_STDERR = '/dev/stderr' logging.addLevelName(logging.WARNING, "WARN") @@ -76,7 +76,7 @@ def is_valid_logfile(file_path): def _absolute_log_file(log_file, default_log_file): if log_file: if not os.path.dirname(log_file): - log_file = os.path.join(_BASE_DIR, log_file) + log_file = os.path.join(_CONFIG_DIR, log_file) return os.path.abspath(log_file) return default_log_file @@ -95,6 +95,11 @@ def setup(log_file, log_level=None): Configure the logging output. May be called multiple times. ''' + # if debugging, start logging to stderr immediately + if os.environ.get('FLASK_DEBUG', None): + log_file = LOG_TO_STDERR + log_level = logging.DEBUG + log_file = _absolute_log_file(log_file, DEFAULT_LOG_FILE) r = logging.root @@ -159,6 +164,3 @@ class StderrLogger(object): self.log.debug("Logging Error") -# if debugging, start logging to stderr immediately -if os.environ.get('FLASK_DEBUG', None): - setup(LOG_TO_STDERR, logging.DEBUG)