mirror of
https://github.com/donaldzou/WGDashboard.git
synced 2025-10-04 00:06:18 +00:00
refac: make WGDashboard logging level configurable (#915)
* feat: init configurable logging level * refac: correct some logging functions to debug from info. They were not that informational * fix: logging somehow was broken due to disablement of external loggers * refac: set default to info --------- Co-authored-by: Daan Selen <dselen@systemec.nl>
This commit is contained in:
@@ -72,7 +72,6 @@ def ResponseObject(status=True, message=None, data=None, status_code = 200) -> F
|
||||
'''
|
||||
Flask App
|
||||
'''
|
||||
app = Flask("WGDashboard", template_folder=os.path.abspath("./static/dist/WGDashboardAdmin"))
|
||||
|
||||
def peerInformationBackgroundThread():
|
||||
global WireguardConfigurations
|
||||
@@ -120,7 +119,8 @@ def peerJobScheduleBackgroundThread():
|
||||
def gunicornConfig():
|
||||
_, app_ip = DashboardConfig.GetConfig("Server", "app_ip")
|
||||
_, app_port = DashboardConfig.GetConfig("Server", "app_port")
|
||||
return app_ip, app_port
|
||||
|
||||
return app_ip, app_port, log_level
|
||||
|
||||
def ProtocolsEnabled() -> list[str]:
|
||||
from shutil import which
|
||||
@@ -172,16 +172,7 @@ def startThreads():
|
||||
scheduleJobThread = threading.Thread(target=peerJobScheduleBackgroundThread, daemon=True)
|
||||
scheduleJobThread.start()
|
||||
|
||||
dictConfig({
|
||||
'version': 1,
|
||||
'formatters': {'default': {
|
||||
'format': '[%(asctime)s] [%(levelname)s] in [%(module)s] %(message)s',
|
||||
}},
|
||||
'root': {
|
||||
'level': 'INFO'
|
||||
}
|
||||
})
|
||||
|
||||
app = Flask("WGDashboard", template_folder=os.path.abspath("./static/dist/WGDashboardAdmin"))
|
||||
|
||||
WireguardConfigurations: dict[str, WireguardConfiguration] = {}
|
||||
CONFIGURATION_PATH = os.getenv('CONFIGURATION_PATH', '.')
|
||||
@@ -189,6 +180,7 @@ CONFIGURATION_PATH = os.getenv('CONFIGURATION_PATH', '.')
|
||||
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 5206928
|
||||
app.secret_key = secrets.token_urlsafe(32)
|
||||
app.json = CustomJsonEncoder(app)
|
||||
|
||||
with app.app_context():
|
||||
SystemStatus = SystemStatus()
|
||||
DashboardConfig = DashboardConfig()
|
||||
@@ -199,19 +191,44 @@ with app.app_context():
|
||||
DashboardPlugins: DashboardPlugins = DashboardPlugins(app, WireguardConfigurations)
|
||||
DashboardWebHooks: DashboardWebHooks = DashboardWebHooks(DashboardConfig)
|
||||
NewConfigurationTemplates: NewConfigurationTemplates = NewConfigurationTemplates()
|
||||
|
||||
InitWireguardConfigurationsList(startup=True)
|
||||
|
||||
DashboardClients: DashboardClients = DashboardClients(WireguardConfigurations)
|
||||
app.register_blueprint(createClientBlueprint(WireguardConfigurations, DashboardConfig, DashboardClients))
|
||||
app.register_blueprint(
|
||||
createClientBlueprint(WireguardConfigurations, DashboardConfig, DashboardClients)
|
||||
)
|
||||
|
||||
_, APP_PREFIX = DashboardConfig.GetConfig("Server", "app_prefix")
|
||||
_, app_ip = DashboardConfig.GetConfig("Server", "app_ip")
|
||||
_, app_port = DashboardConfig.GetConfig("Server", "app_port")
|
||||
_, WG_CONF_PATH = DashboardConfig.GetConfig("Server", "wg_conf_path")
|
||||
_, log_level = DashboardConfig.GetConfig("Server", "log_level")
|
||||
|
||||
cors = CORS(app, resources={rf"{APP_PREFIX}/api/*": {
|
||||
"origins": "*",
|
||||
"methods": "DELETE, POST, GET, OPTIONS",
|
||||
"allow_headers": ["Content-Type", "wg-dashboard-apikey"]
|
||||
}})
|
||||
_, app_ip = DashboardConfig.GetConfig("Server", "app_ip")
|
||||
_, app_port = DashboardConfig.GetConfig("Server", "app_port")
|
||||
_, WG_CONF_PATH = DashboardConfig.GetConfig("Server", "wg_conf_path")
|
||||
|
||||
app.logger.setLevel(getattr(logging, log_level.upper(), logging.INFO))
|
||||
|
||||
dictConfig({
|
||||
'version': 1,
|
||||
'disable_existing_loggers': False,
|
||||
'formatters': {
|
||||
'default': {
|
||||
'format': '[%(asctime)s] [%(levelname)s] in [%(module)s] %(message)s',
|
||||
},
|
||||
},
|
||||
'handlers': {
|
||||
'wsgi': {
|
||||
'class': 'logging.StreamHandler',
|
||||
'stream': 'ext://flask.logging.wsgi_errors_stream',
|
||||
'formatter': 'default',
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
'''
|
||||
API Routes
|
||||
|
@@ -1,7 +1,7 @@
|
||||
import dashboard
|
||||
from datetime import datetime
|
||||
global sqldb, cursor, DashboardConfig, WireguardConfigurations, AllPeerJobs, JobLogger, Dash
|
||||
app_host, app_port = dashboard.gunicornConfig()
|
||||
app_host, app_port, log_level = dashboard.gunicornConfig()
|
||||
date = datetime.today().strftime('%Y_%m_%d_%H_%M_%S')
|
||||
|
||||
def post_worker_init(worker):
|
||||
@@ -16,7 +16,7 @@ daemon = True
|
||||
pidfile = './gunicorn.pid'
|
||||
wsgi_app = "dashboard:app"
|
||||
accesslog = f"./log/access_{date}.log"
|
||||
loglevel = "info"
|
||||
loglevel = f"{log_level}"
|
||||
capture_output = True
|
||||
errorlog = f"./log/error_{date}.log"
|
||||
pythonpath = "., ./modules"
|
||||
|
@@ -174,7 +174,7 @@ class AmneziaWireguardConfiguration(WireguardConfiguration):
|
||||
|
||||
def getPeers(self):
|
||||
self.Peers.clear()
|
||||
current_app.logger.info(f"Refreshing {self.Name} peer list")
|
||||
current_app.logger.debug(f"Refreshing {self.Name} peer list")
|
||||
|
||||
if self.configurationFileChanged():
|
||||
with open(self.configPath, 'r') as configFile:
|
||||
@@ -183,7 +183,7 @@ class AmneziaWireguardConfiguration(WireguardConfiguration):
|
||||
content = configFile.read().split('\n')
|
||||
try:
|
||||
if "[Peer]" not in content:
|
||||
current_app.logger.info(f"{self.Name} config has no [Peer] section")
|
||||
current_app.logger.debug(f"{self.Name} config has no [Peer] section")
|
||||
return
|
||||
|
||||
peerStarts = content.index("[Peer]")
|
||||
|
@@ -35,10 +35,13 @@ def ConnectionString(database_name: str) -> str:
|
||||
host = parser.get("Database", "host")
|
||||
cn = f"mysql+pymysql://{username}:{password}@{host}/{database_name}"
|
||||
else:
|
||||
cn = f"sqlite:///{os.path.join(SQLITE_PATH, f'{database_name}.db')}"
|
||||
cn = f'sqlite:///{os.path.join(sqlitePath, f"{database}.db")}'
|
||||
|
||||
# Ensure database exists
|
||||
if not database_exists(cn):
|
||||
create_database(cn)
|
||||
try:
|
||||
if not database_exists(cn):
|
||||
create_database(cn)
|
||||
except Exception as e:
|
||||
current_app.logger.critical("Database error. Terminating...", e)
|
||||
exit(1)
|
||||
|
||||
return cn
|
@@ -47,7 +47,8 @@ class DashboardConfig:
|
||||
"dashboard_sort": "status",
|
||||
"dashboard_theme": "dark",
|
||||
"dashboard_api_key": "false",
|
||||
"dashboard_language": "en-US"
|
||||
"dashboard_language": "en-US",
|
||||
"log_level": "INFO"
|
||||
},
|
||||
"Peers": {
|
||||
"peer_global_DNS": "1.1.1.1",
|
||||
|
@@ -141,7 +141,7 @@ class PeerJobs:
|
||||
|
||||
|
||||
def runJob(self):
|
||||
current_app.logger.info("Running scheduled jobs")
|
||||
current_app.logger.debug("Running scheduled jobs")
|
||||
needToDelete = []
|
||||
self.__getJobs()
|
||||
for job in self.Jobs:
|
||||
|
@@ -396,7 +396,7 @@ class WireguardConfiguration:
|
||||
|
||||
def getPeers(self):
|
||||
tmpList = []
|
||||
current_app.logger.info(f"Refreshing {self.Name} peer list")
|
||||
current_app.logger.debug(f"Refreshing {self.Name} peer list")
|
||||
|
||||
if self.configurationFileChanged():
|
||||
with open(self.configPath, 'r') as configFile:
|
||||
@@ -405,7 +405,7 @@ class WireguardConfiguration:
|
||||
content = configFile.read().split('\n')
|
||||
try:
|
||||
if "[Peer]" not in content:
|
||||
current_app.logger.info(f"{self.Name} config has no [Peer] section")
|
||||
current_app.logger.debug(f"{self.Name} config has no [Peer] section")
|
||||
return
|
||||
|
||||
peerStarts = content.index("[Peer]")
|
||||
|
Reference in New Issue
Block a user