Skip to content

Commit

Permalink
Moved logging level setting to admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Casvt committed Mar 3, 2024
1 parent 42bc117 commit a88cc8a
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 15 deletions.
10 changes: 2 additions & 8 deletions MIND.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from backend.db import setup_db, setup_db_location
from backend.helpers import check_python_version
from backend.logging import setup_logging
from backend.reminders import ReminderHandler
from backend.server import SERVER, handle_flags
from backend.settings import get_setting
Expand All @@ -24,17 +25,10 @@
URL_PREFIX = '' # Must either be empty or start with '/' e.g. '/mind'
#=============================

LOGGING_LEVEL = logging.INFO

logging.basicConfig(
level=LOGGING_LEVEL,
format='[%(asctime)s][%(threadName)s][%(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)

def MIND() -> None:
"""The main function of MIND
"""
setup_logging()
logging.info('Starting up MIND')

if not check_python_version():
Expand Down
3 changes: 3 additions & 0 deletions backend/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from backend.custom_exceptions import (AccessUnauthorized, InvalidDatabaseFile,
UserNotFound)
from backend.helpers import RestartVars, folder_path
from backend.logging import set_log_level

DB_FILENAME = 'db', 'MIND.db'
__DATABASE_VERSION__ = 10
Expand Down Expand Up @@ -393,6 +394,8 @@ def setup_db() -> None:
)
)

set_log_level(get_setting('log_level'))

current_db_version = get_setting('database_version')
if current_db_version < __DATABASE_VERSION__:
logging.debug(
Expand Down
27 changes: 27 additions & 0 deletions backend/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#-*- coding: utf-8 -*-

import logging


def setup_logging() -> None:
"Setup the basic config of the logging module"
logging.basicConfig(
level=logging.INFO,
format='[%(asctime)s][%(threadName)s][%(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
force=True
)
return

def set_log_level(level: int) -> None:
"""Change the logging level
Args:
level (int): The level to set the logging to.
Should be a logging level, like `logging.INFO` or `logging.DEBUG`.
"""
logging.debug(f'Setting logging level: {level}')
logging.getLogger().setLevel(
level=level
)
return
17 changes: 15 additions & 2 deletions backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@
Getting and setting settings
"""

import logging
from json import dumps, loads
from typing import Any

from backend.custom_exceptions import InvalidKeyValue, KeyNotFound
from backend.db import __DATABASE_VERSION__, get_db
from backend.helpers import folder_path
from backend.logging import set_log_level

default_settings = {
'allow_new_accounts': True,
'login_time': 3600,
'login_time_reset': True,

'database_version': __DATABASE_VERSION__,

'host': '0.0.0.0',
'port': 8080,
'url_prefix': ''
'url_prefix': '',

'log_level': logging.INFO
}

def _format_setting(key: str, value):
Expand Down Expand Up @@ -64,6 +70,9 @@ def _format_setting(key: str, value):
if value:
value = '/' + value.strip('/')

elif key == 'log_level' and not value in (logging.INFO, logging.DEBUG):
raise InvalidKeyValue(key, value)

return value

def _reverse_format_setting(key: str, value: Any) -> Any:
Expand Down Expand Up @@ -120,7 +129,8 @@ def get_admin_settings() -> dict:
OR key = 'login_time_reset'
OR key = 'host'
OR key = 'port'
OR key = 'url_prefix';
OR key = 'url_prefix'
OR key = 'log_level';
"""
)
))
Expand Down Expand Up @@ -149,6 +159,9 @@ def set_setting(key: str, value: Any) -> None:
if key == 'url_prefix':
update_manifest(value)

elif key == 'log_level':
set_log_level(value)

return

def update_manifest(url_base: str) -> None:
Expand Down
6 changes: 3 additions & 3 deletions frontend/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
EditTimeVariable, EditTitleVariable,
EditURLVariable, HostVariable,
LoginTimeResetVariable,
LoginTimeVariable, Method, Methods,
NewPasswordVariable,
LoginTimeVariable, LogLevelVariable,
Method, Methods, NewPasswordVariable,
NotificationServicesVariable,
PasswordCreateVariable,
PasswordVariable, PortVariable,
Expand Down Expand Up @@ -705,7 +705,7 @@ def api_settings():
put=Method(
vars=[AllowNewAccountsVariable, LoginTimeVariable,
LoginTimeResetVariable, HostVariable, PortVariable,
UrlPrefixVariable],
UrlPrefixVariable, LogLevelVariable],
description='Edit the admin settings. Supplying a hosting setting will automatically restart MIND.'
)
),
Expand Down
25 changes: 25 additions & 0 deletions frontend/input_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from abc import ABC, abstractmethod
from dataclasses import dataclass, field
import logging
from os.path import splitext
from re import compile
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Type, Union
Expand Down Expand Up @@ -368,6 +369,14 @@ def validate(self) -> bool:
and all(v in self._options for v in self.value)
)

def __repr__(self) -> str:
return '| {n} | {r} | {t} | {d} | {v} |'.format(
n=self.name,
r="Yes" if self.required else "No",
t=",".join(self.data_type),
d=self.description,
v=", ".join(f'`{o}`' for o in self._options)
)

class ColorVariable(NonRequiredVersion, BaseInputVariable):
name = 'color'
Expand Down Expand Up @@ -432,6 +441,22 @@ class UrlPrefixVariable(NonRequiredVersion, AdminSettingsVariable):
description = 'The base url to run on. Useful for reverse proxies. Empty string to disable.'


class LogLevelVariable(NonRequiredVersion, AdminSettingsVariable):
name = 'log_level'
description = 'The level to log on.'
data_type = [DataType.INT]
_options = [logging.INFO, logging.DEBUG]

def __repr__(self) -> str:
return '| {n} | {r} | {t} | {d} | {v} |'.format(
n=self.name,
r="Yes" if self.required else "No",
t=",".join(self.data_type),
d=self.description,
v=", ".join(f'`{o}`' for o in self._options)
)


class DatabaseFileVariable(BaseInputVariable):
name = 'file'
description = 'The MIND database file'
Expand Down
7 changes: 5 additions & 2 deletions frontend/static/js/admin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const setting_inputs = {
allow_new_accounts: document.querySelector('#allow-new-accounts-input'),
login_time: document.querySelector('#login-time-input'),
login_time_reset: document.querySelector('#login-time-reset-input')
login_time_reset: document.querySelector('#login-time-reset-input'),
log_level: document.querySelector('#log-level-input')
};

const hosting_inputs = {
Expand Down Expand Up @@ -53,6 +54,7 @@ function loadSettings() {
setting_inputs.allow_new_accounts.checked = json.result.allow_new_accounts;
setting_inputs.login_time.value = Math.round(json.result.login_time / 60);
setting_inputs.login_time_reset.value = json.result.login_time_reset.toString();
setting_inputs.log_level.value = json.result.log_level;
hosting_inputs.host.value = json.result.host;
hosting_inputs.port.value = json.result.port;
hosting_inputs.url_prefix.value = json.result.url_prefix;
Expand All @@ -63,7 +65,8 @@ function submitSettings() {
const data = {
'allow_new_accounts': setting_inputs.allow_new_accounts.checked,
'login_time': setting_inputs.login_time.value * 60,
'login_time_reset': setting_inputs.login_time_reset.value === 'true'
'login_time_reset': setting_inputs.login_time_reset.value === 'true',
'log_level': parseInt(setting_inputs.log_level.value)
};
fetch(`${url_prefix}/api/admin/settings?api_key=${api_key}`, {
'method': 'PUT',
Expand Down
16 changes: 16 additions & 0 deletions frontend/templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ <h2>Authentication</h2>
</tbody>
</table>
</div>
<h2>Logging</h2>
<div class="settings-table-container">
<table class="settings-table">
<tbody>
<tr>
<td><label for="log-level-input">Logging Level</label></td>
<td>
<select id="log-level-input">
<option value="20">Info</option>
<option value="10">Debug</option>
</select>
</td>
</tr>
</tbody>
</table>
</div>
</form>
<form id="hosting-form">
<h2>Hosting</h2>
Expand Down

0 comments on commit a88cc8a

Please sign in to comment.