From 927d45c70f4d124d055cdc3d0f9267841f7e0f07 Mon Sep 17 00:00:00 2001 From: DaanSelen Date: Tue, 3 Mar 2026 11:55:01 +0100 Subject: [PATCH] feat: configurable loglevel --- src/config.ini | 8 ++++--- src/main.py | 25 ++++++++++++++++---- src/modules/config/utilities.py | 14 ----------- src/modules/routes/response.py | 5 ++-- src/modules/routes/routes.py | 2 +- src/modules/utilities/logger.py | 37 ++++++++++++++++++++++++++++++ src/modules/utilities/utilities.py | 4 +--- 7 files changed, 67 insertions(+), 28 deletions(-) create mode 100644 src/modules/utilities/logger.py diff --git a/src/config.ini b/src/config.ini index f4d74ee..d7d2b45 100644 --- a/src/config.ini +++ b/src/config.ini @@ -7,19 +7,21 @@ peer_mtu = 1420 peer_keep_alive = 21 [Server] -app_port = 10086 +hostname = 0.0.0.0 +port = 10086 +debug_enabled = true wg_conf_path = /etc/wireguard awg_conf_path = /etc/amnezia/amneziawg app_prefix = -app_ip = 0.0.0.0 auth_req = true -version = v4.3.2 +version = v5.0.0 dashboard_refresh_interval = 60000 dashboard_peer_list_display = grid dashboard_sort = status dashboard_theme = dark wgdashboard_apikey = false dashboard_language = en-US +log_level = DEBUG [Account] username = admin diff --git a/src/main.py b/src/main.py index 8699a88..3d61514 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,7 @@ #!/bin/env python3 import logging as log +from logging.config import dictConfig import flask import json @@ -8,29 +9,45 @@ import os from modules.config.reader import reader from modules.database.database import database + from modules.utilities.utilities import utilities as util +from modules.utilities.logger import setup_logger from modules.routes.routes import routes if __name__ == '__main__': - log.basicConfig(level=log.DEBUG) - + # Read the config file (ini) ok, config_contents = reader.read_config() if not ok: exit(1) + found, config_server = util.filter_config(config_contents, 'SERVER') + # Configure the loglevel of WGDashboard + wanted_loglevel = config_server.get('log_level', 'DEBUG').upper() + setup_logger(wanted_loglevel) + + # Get the database configuration from thee config found, config_database = util.filter_config(config_contents, 'DATABASE') if not found: exit(1) + # Make the engine and create the infrastructure ok, engine, session = database.create_session(config_database) ok = database.ensure_contents(engine) + # Configure the Flask app app = flask.Flask("WGDashboard", template_folder=os.path.abspath("./static/dist/WGDashboardAdmin")) app.register_blueprint(routes) - app.wgdashboard_config = config_contents + app.wgd_config = config_contents app.engine = engine app.db_session = session - app.run(debug=True, use_reloader=False) \ No newline at end of file + debug_enabled = config_server.get('debug_enabled', False) + hostname = config_server.get('hostname', '0.0.0.0') + port = config_server.get('port', '10086') + app.run( + debug=debug_enabled, + host=hostname, + port=port, + use_reloader=False) \ No newline at end of file diff --git a/src/modules/config/utilities.py b/src/modules/config/utilities.py index 62fa5d2..4a4892c 100644 --- a/src/modules/config/utilities.py +++ b/src/modules/config/utilities.py @@ -19,20 +19,14 @@ class checks(): ] try: - log.debug('searching predefined locations for the config file, takes first one.') - for path in possible_config_locations: - log.debug(f'testing path: {path}') - if os.path.exists(path): - log.debug(f'found a file at: {path}') return True, path else: continue return False, '' except Exception as err: - log.error(f'error occured while searching for the config file: {err}') return False, '' @staticmethod @@ -43,19 +37,13 @@ class checks(): config = cp.ConfigParser() try: - log.debug('looking through the given config file') - config.read(config_path) if len(config.sections()) == 0: - log.error('empty config, no sections') return False, {} for section in config.sections(): - log.debug(f'checking integrity of section: {section}') - if len(config.items(section)) == 0: - log.warn(f'empty section: {section}, removing at runtime due to irrelevance') config.remove_section(section) config_dict = {} @@ -66,9 +54,7 @@ class checks(): return True, config_dict except cp.ParsingError as err: - log.error(f'error parsing the ini config file: {err}') return False, {} except Exception as err: - log.error(f'error occured while looking through the config file: {err}') return False, {} \ No newline at end of file diff --git a/src/modules/routes/response.py b/src/modules/routes/response.py index c9433f7..36149df 100644 --- a/src/modules/routes/response.py +++ b/src/modules/routes/response.py @@ -4,10 +4,9 @@ import flask def make_resp_obj(success: bool = True, message: str = "", data: dict = {}, http_code: int = 200) -> flask.wrappers.Response: response = flask.make_response({ - "status": success, "message": message, + "status": success, "data": data - }, http_code - ) + }, http_code) return response \ No newline at end of file diff --git a/src/modules/routes/routes.py b/src/modules/routes/routes.py index d977d91..fe26ef1 100644 --- a/src/modules/routes/routes.py +++ b/src/modules/routes/routes.py @@ -13,7 +13,7 @@ def auth_required(): if flask.request.method.lower() == "options": return make_resp_obj(True, "", flask.jsonify({"status": True}), 200) - ok, config_server = utilities.filter_config(flask.current_app.wgdashboard_config, 'SERVER') + ok, config_server = utilities.filter_config(flask.current_app.wgd_config, 'SERVER') if not ok: return make_resp_obj(False, "Internal Error", {}, 500) diff --git a/src/modules/utilities/logger.py b/src/modules/utilities/logger.py new file mode 100644 index 0000000..2023bb5 --- /dev/null +++ b/src/modules/utilities/logger.py @@ -0,0 +1,37 @@ +#!/bin/env python3 + +from logging.config import dictConfig + +@staticmethod +def setup_logger(level: str = 'DEBUG') -> None: + dictConfig({ + 'version': 1, + 'formatters': { + 'default': { + 'format': '[%(asctime)s] [%(levelname)s] in [%(module)s] %(message)s' + } + }, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + 'formatter': 'default', + 'level': level + } + }, + 'root': { + 'handlers': ['console'], + 'level': level + }, + 'loggers': { + 'werkzeug': { # make werkzeug logs match + 'handlers': ['console'], + 'level': level, + 'propagate': False + }, + 'flask.app': { # make Flask internal logs match + 'handlers': ['console'], + 'level': level, + 'propagate': False + } + } + }) \ No newline at end of file diff --git a/src/modules/utilities/utilities.py b/src/modules/utilities/utilities.py index 94912f9..301dd74 100644 --- a/src/modules/utilities/utilities.py +++ b/src/modules/utilities/utilities.py @@ -34,6 +34,4 @@ class utilities(): except Exception as err: log.critical('failed to create directory') - return False - - \ No newline at end of file + return False \ No newline at end of file