From 81c6f32a354a1dee9be5f4933d0f6b69951c8776 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Thu, 1 Aug 2024 16:29:06 +0200 Subject: [PATCH] Simplify logging configuration --- app/main/cli.py | 1 - config.py | 65 ++++++++++++++++++++++++---------------------- docker-compose.yml | 1 - nopaque.env.tpl | 26 +++++++------------ 4 files changed, 43 insertions(+), 50 deletions(-) diff --git a/app/main/cli.py b/app/main/cli.py index a97fa71c..362f172d 100644 --- a/app/main/cli.py +++ b/app/main/cli.py @@ -21,7 +21,6 @@ def deploy(): print('Make default directories') base_dir = current_app.config['NOPAQUE_DATA_DIR'] default_dirs: List[Path] = [ - Path('/var/log/nopaque'), base_dir / 'tmp', base_dir / 'users' ] diff --git a/config.py b/config.py index 5f3d4f28..7e909500 100644 --- a/config.py +++ b/config.py @@ -20,6 +20,7 @@ class Config: APIFAIRY_UI_PATH = '/api' ''' # Flask # ''' + APPLICATION_ROOT = os.environ.get('APPLICATION_ROOT', '/') PREFERRED_URL_SCHEME = os.environ.get('PREFERRED_URL_SCHEME', 'http') SECRET_KEY = os.environ.get('SECRET_KEY', 'hard to guess string') SERVER_NAME = os.environ.get('SERVER_NAME', 'localhost:5000') @@ -34,7 +35,7 @@ class Config: ''' # Flask-Hashids ''' HASHIDS_MIN_LENGTH = int(os.environ.get('HASHIDS_MIN_LENGTH', '16')) - HASHIDS_SALT=os.environ.get('HASHIDS_SALT', 'hard to guess string') + HASHIDS_SALT = os.environ.get('HASHIDS_SALT', 'hard to guess string') ''' # Flask-Login # ''' REMEMBER_COOKIE_SECURE = \ @@ -85,17 +86,15 @@ class Config: '[%(asctime)s] %(levelname)s in ' '%(pathname)s (function: %(funcName)s, line: %(lineno)d): %(message)s' ) - NOPAQUE_LOG_LEVEL = os.environ.get('NOPAQUE_LOG_LEVEL', 'INFO') NOPAQUE_LOG_FILE_ENABLED = \ - os.environ.get('NOPAQUE_LOG_FILE_ENABLED', 'true').lower() == 'true' - NOPAQUE_LOG_FILE_DIR = \ - os.environ.get('NOPAQUE_LOGS_PATH', os.path.join(basedir, 'logs')) + os.environ.get('NOPAQUE_LOG_FILE_ENABLED', 'false').lower() == 'true' + NOPAQUE_LOG_FILE_DIR = Path(os.environ.get('NOPAQUE_LOG_FILE_DIR', '/var/log/nopaque')) NOPAQUE_LOG_FILE_LEVEL = \ - os.environ.get('NOPAQUE_LOG_FILE_LEVEL', NOPAQUE_LOG_LEVEL) + os.environ.get('NOPAQUE_LOG_FILE_LEVEL', None) NOPAQUE_LOG_STDERR_ENABLED = \ - os.environ.get('NOPAQUE_LOG_STDERR_ENABLED', 'false').lower() == 'true' + os.environ.get('NOPAQUE_LOG_STDERR_ENABLED', 'true').lower() == 'true' NOPAQUE_LOG_STDERR_LEVEL = \ - os.environ.get('NOPAQUE_LOG_STDERR_LEVEL', NOPAQUE_LOG_LEVEL) + os.environ.get('NOPAQUE_LOG_STDERR_LEVEL', None) NOPAQUE_PROXY_FIX_ENABLED = \ os.environ.get('NOPAQUE_PROXY_FIX_ENABLED', 'false').lower() == 'true' @@ -119,42 +118,35 @@ class Config: @staticmethod def init_app(app: Flask): - # Set up logging according to the corresponding (NOPAQUE_LOG_*) - # configurations - app.logger.setLevel(app.config['NOPAQUE_LOG_LEVEL']) - # Remove existing handlers for handler in app.logger.handlers: app.logger.removeHandler(handler) - # Setup handlers - formatter = logging.Formatter( + + log_formatter = logging.Formatter( fmt=app.config['NOPAQUE_LOG_FORMAT'], datefmt=app.config['NOPAQUE_LOG_DATE_FORMAT'] ) - if app.config['NOPAQUE_IS_PRIMARY_INSTANCE']: - app.config['JOBS'].append( - { - "id": "daemon", - "func": "app.daemon:daemon", - "args": (app,), - "trigger": "interval", - "seconds": 3, - } - ) + if app.config['NOPAQUE_LOG_STDERR_ENABLED']: + log_stderr_level: str | None = app.config['NOPAQUE_LOG_STDERR_LEVEL'] stream_handler = logging.StreamHandler() - stream_handler.setFormatter(formatter) - stream_handler.setLevel(app.config['NOPAQUE_LOG_STDERR_LEVEL']) + stream_handler.setFormatter(log_formatter) + if log_stderr_level is not None: + stream_handler.setLevel(log_stderr_level) app.logger.addHandler(stream_handler) + if app.config['NOPAQUE_LOG_FILE_ENABLED']: - if not os.path.exists(app.config['NOPAQUE_LOG_FILE_DIR']): - os.mkdir(app.config['NOPAQUE_LOG_FILE_DIR']) + log_file_dir: Path = app.config['NOPAQUE_LOG_FILE_DIR'] + log_file_level: str | None = app.config['NOPAQUE_LOG_FILE_LEVEL'] + if not log_file_dir.exists(): + log_file_dir.mkdir() rotating_file_handler = RotatingFileHandler( - os.path.join(app.config['NOPAQUE_LOG_FILE_DIR'], 'nopaque.log'), # noqa + log_file_dir / 'nopaque.log', maxBytes=10_240, backupCount=10 ) - rotating_file_handler.setFormatter(formatter) - rotating_file_handler.setLevel(app.config['NOPAQUE_LOG_FILE_LEVEL']) # noqa + rotating_file_handler.setFormatter(log_formatter) + if log_file_level is not None: + rotating_file_handler.setLevel(log_file_level) app.logger.addHandler(rotating_file_handler) if app.config['NOPAQUE_PROXY_FIX_ENABLED']: @@ -168,3 +160,14 @@ class Config: x_prefix=app.config['NOPAQUE_PROXY_FIX_X_PREFIX'], x_proto=app.config['NOPAQUE_PROXY_FIX_X_PROTO'] ) + + if app.config['NOPAQUE_IS_PRIMARY_INSTANCE']: + app.config['JOBS'].append( + { + "id": "daemon", + "func": "app.daemon:daemon", + "args": (app,), + "trigger": "interval", + "seconds": 3, + } + ) diff --git a/docker-compose.yml b/docker-compose.yml index a3a19a14..5f702b90 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -50,7 +50,6 @@ services: restart: "unless-stopped" volumes: - "/var/run/docker.sock:/var/run/docker.sock" - - "./volumes/nopaque/logs:/var/log/nopaque" # DANGER: Don't change the following mount within a Docker Compose # config file, use the `.env` file instead. - "${HOST_NOPAQUE_DATA_PATH}:${HOST_NOPAQUE_DATA_PATH}" diff --git a/nopaque.env.tpl b/nopaque.env.tpl index 74699e62..dacb3d4e 100644 --- a/nopaque.env.tpl +++ b/nopaque.env.tpl @@ -113,10 +113,10 @@ NOPAQUE_ADMIN= # DEFAULT: /mnt/nopaque # NOTES: # - This must be a network share and it must be available on all -# Docker Swarm nodes, mounted to the same path with the same -# user and group ownership +# Docker Swarm nodes, mounted to the same path. # - When running with Docker Compose, this gets overwritten in the -# `docker-compose.yml` file +# `docker-compose.yml` file with the value of the `HOST_NOPAQUE_DATA_PATH` +# environment variable from the `.env` file. # NOPAQUE_DATA_PATH= # CHOOSE ONE: False, True @@ -141,30 +141,22 @@ NOPAQUE_DOCKER_REGISTRY_PASSWORD= # DEFAULT: [%(asctime)s] %(levelname)s in %(pathname)s (function: %(funcName)s, line: %(lineno)d): %(message)s # NOPAQUE_LOG_FORMAT= -# DEFAULT: INFO -# CHOOSE ONE: CRITICAL, ERROR, WARNING, INFO, DEBUG -# NOPAQUE_LOG_LEVEL= - # CHOOSE ONE: False, True -# DEFAULT: True +# DEFAULT: False # NOPAQUE_LOG_FILE_ENABLED= -# DEFAULT: /logs -# NOTES: -# - Use `.` as -# - When running with Docker Compose, this gets overwritten in the -# `docker-compose.yml` file -# NOPAQUE_LOGS_PATH= +# DEFAULT: /var/log/nopaque +# NOPAQUE_LOG_FILE_DIR= -# DEFAULT: NOPAQUE_LOG_LEVEL +# DEFAULT: DEBUG if FLASK_DEBUG == True else WARNING # CHOOSE ONE: CRITICAL, ERROR, WARNING, INFO, DEBUG # NOPAQUE_LOG_FILE_LEVEL= # CHOOSE ONE: False, True -# DEFAULT: False +# DEFAULT: True # NOPAQUE_LOG_STDERR_ENABLED= -# DEFAULT: NOPAQUE_LOG_LEVEL +# DEFAULT: DEBUG if FLASK_DEBUG == True else WARNING # CHOOSE ONE: CRITICAL, ERROR, WARNING, INFO, DEBUG # NOPAQUE_LOG_STDERR_LEVEL=