Adding range input in settings so the user can change the API log level.

This commit is contained in:
Roland 2023-09-08 17:10:15 -06:00
parent bf3c2822a3
commit 95e870d659
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
7 changed files with 179 additions and 19 deletions

View file

@ -109,6 +109,41 @@ export function RestartButton() {
</APIButton>
}
const levelNameMap = {
5: 'All',
4: 'Debug',
3: 'Info',
2: 'Warning',
1: 'Critical'
}
function logLevelToName(logLevel) {
// Convert app log level to name.
return levelNameMap[logLevel]
}
function fromApiLogLevel(logLevel) {
// Python API uses levels on the left. App uses levels on the right to display <input> direction correctly.
return {
40: 1,
30: 2,
20: 3,
10: 4,
0: 5,
}[logLevel]
}
function toApiLogLevel(logLevel) {
// Reverse the above levels.
return {
5: 0,
4: 10,
3: 20,
2: 30,
1: 40,
}[logLevel]
}
export class SettingsPage extends React.Component {
constructor(props) {
@ -128,6 +163,7 @@ export class SettingsPage extends React.Component {
hotspot_ssid: null,
hotspot_status: null,
ignore_outdated_zims: null,
log_level: null,
throttle_on_startup: null,
throttle_status: null,
}
@ -149,6 +185,7 @@ export class SettingsPage extends React.Component {
hotspot_ssid: settings.hotspot_ssid,
hotspot_status: settings.hotspot_status,
ignore_outdated_zims: settings.ignore_outdated_zims,
log_level: fromApiLogLevel(settings.log_level),
throttle_on_startup: settings.throttle_on_startup,
throttle_status: settings.throttle_status,
}, this.handleHotspotChange);
@ -170,10 +207,25 @@ export class SettingsPage extends React.Component {
hotspot_password: this.state.hotspot_password,
hotspot_ssid: this.state.hotspot_ssid,
ignore_outdated_zims: this.state.ignore_outdated_zims,
log_level: toApiLogLevel(this.state.log_level),
throttle_on_startup: this.state.throttle_on_startup,
}
await saveSettings(settings);
this.setState({disabled: false, pending: false});
try {
const response = await saveSettings(settings);
if (response.status !== 204) {
throw Error('Failed to save settings');
}
} catch (e) {
toast({
type: 'error',
title: 'Failed',
description: 'Failed to save settings.',
time: 5000,
});
throw e;
} finally {
this.setState({disabled: false, pending: false});
}
}
handleInputChange = async (e, name, value) => {
@ -219,6 +271,7 @@ export class SettingsPage extends React.Component {
hotspot_password,
hotspot_ssid,
ignore_outdated_zims,
log_level,
pending,
qrCodeValue,
throttle_on_startup,
@ -320,6 +373,27 @@ export class SettingsPage extends React.Component {
<br/>
<label htmlFor='log_levels_input'>Log Level: {logLevelToName(log_level)}</label>
<br/>
<input type='range'
id='log_levels_input'
list='log_levels'
min='1'
max='5'
value={log_level}
onChange={e => this.setState({'log_level': parseInt(e.target.value)})}
style={{marginBottom: '1em'}}
/>
<datalist id='log_levels'>
<option value='1'>Critical</option>
<option value='2'>Warning</option>
<option value='3'>Info</option>
<option value='4'>Debug</option>
<option value='5'>All</option>
</datalist>
<br/>
<APIButton
color='violet'
size='big'

29
main.py
View file

@ -114,13 +114,18 @@ def main():
return 1
logger.warning(f'Starting with: {sys.argv}')
if args.verbose == 1:
set_log_level(logging.INFO)
elif args.verbose and args.verbose == 2:
set_log_level(logging.DEBUG)
elif args.verbose and args.verbose >= 3:
# Log everything. Add SQLAlchemy debug logging.
set_log_level(logging.NOTSET)
from wrolpi.common import LOG_LEVEL
with LOG_LEVEL.get_lock():
if args.verbose == 1:
LOG_LEVEL.value = logging.INFO
set_log_level(logging.INFO)
elif args.verbose and args.verbose == 2:
LOG_LEVEL.value = logging.DEBUG
set_log_level(logging.DEBUG)
elif args.verbose and args.verbose >= 3:
# Log everything. Add SQLAlchemy debug logging.
LOG_LEVEL.value = logging.NOTSET
set_log_level(logging.NOTSET)
logger.info(get_version_string())
if DOCKERIZED:
@ -184,6 +189,16 @@ async def periodic_check_db_is_up(app: Sanic):
await asyncio.sleep(10)
@api_app.after_server_start
async def periodic_check_log_level(app: Sanic):
from wrolpi.common import LOG_LEVEL
while True:
log_level = LOG_LEVEL.value
if log_level != logger.getEffectiveLevel():
set_log_level(log_level)
await asyncio.sleep(1)
@api_app.after_server_start
async def periodic_downloads(app: Sanic):
"""

View file

@ -1,6 +1,7 @@
import asyncio
import atexit
import contextlib
import ctypes
import inspect
import json
import logging
@ -36,12 +37,14 @@ from sqlalchemy.orm import Session
from wrolpi import dates
from wrolpi.dates import now, from_timestamp, seconds_to_timestamp
from wrolpi.errors import WROLModeEnabled, NativeOnly, UnrecoverableDownloadError
from wrolpi.errors import WROLModeEnabled, NativeOnly, UnrecoverableDownloadError, LogLevelError
from wrolpi.vars import PYTEST, DOCKERIZED, CONFIG_DIR, MEDIA_DIRECTORY, DEFAULT_HTTP_HEADERS
LOG_LEVEL = multiprocessing.Value(ctypes.c_int, 20)
logger = logging.getLogger()
ch = logging.StreamHandler()
formatter = logging.Formatter('[%(asctime)s] [%(name)s:%(lineno)d] [%(levelname)s] %(message)s')
formatter = logging.Formatter('[%(asctime)s] [%(process)d] [%(name)s:%(lineno)d] [%(levelname)s] %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
@ -50,12 +53,11 @@ logger_ = logger.getChild(__name__)
def set_log_level(level, warn_level: bool = True):
"""Set the level of the root logger so all children that have been created (or will be created) share the same
level."""
level.
@warning: Child processes will not share this level. See set_global_log_level
"""
logger.setLevel(level)
if level == logging.NOTSET:
# Custom maximum logging level which will include SQLAlchemy debugging.
sa_logger = logging.getLogger('sqlalchemy.engine')
sa_logger.setLevel(logging.DEBUG)
# Always warn about the log level, so we know what should have been logged.
effective_level = logger.getEffectiveLevel()
@ -63,6 +65,24 @@ def set_log_level(level, warn_level: bool = True):
if warn_level:
logger.warning(f'Logging level: {level_name}')
# Enable debug logging in SQLAlchemy when logging is NOTSET.
sa_logger = logging.getLogger('sqlalchemy.engine')
sa_level = logging.DEBUG if level == logging.NOTSET else logging.WARNING
sa_logger.setLevel(sa_level)
# Hide sanic access logs when not at least "INFO".
sanic_access = logging.getLogger('sanic.access')
sanic_level = logging.INFO if level <= 20 else logging.WARNING
sanic_access.setLevel(sanic_level)
def set_global_log_level(log_level: int):
"""Set the global (shared between processes) log level."""
if not isinstance(log_level, int) or 0 > log_level or log_level > 40:
raise LogLevelError()
with LOG_LEVEL.get_lock():
LOG_LEVEL.value = log_level
@contextlib.contextmanager
def log_level_context(level):

View file

@ -73,6 +73,12 @@ class HotspotError(APIError):
status = HTTPStatus.INTERNAL_SERVER_ERROR
class LogLevelError(APIError):
code = 'LOG_LEVEL_ERROR'
summary = 'Log level is invalid'
status = HTTPStatus.BAD_REQUEST
class UnknownArchive(APIError):
code = 'UNKNOWN_ARCHIVE'
summary = 'Unable to find the archive'
@ -125,3 +131,9 @@ class ShutdownFailed(APIError):
code = 'SHUTDOWN_FAILED'
summary = 'Unable to shutdown the WROLPi'
status = HTTPStatus.INTERNAL_SERVER_ERROR
class InvalidConfig(APIError):
code = 'INVALID_CONFIG'
summary = 'Config is invalid'
status = HTTPStatus.BAD_REQUEST

View file

@ -21,11 +21,12 @@ from wrolpi import admin, status, flags, schema
from wrolpi import tags
from wrolpi.admin import HotspotStatus
from wrolpi.common import logger, get_config, wrol_mode_enabled, Base, get_media_directory, \
wrol_mode_check, native_only, disable_wrol_mode, enable_wrol_mode, get_global_statistics, url_strip_host
wrol_mode_check, native_only, disable_wrol_mode, enable_wrol_mode, get_global_statistics, url_strip_host, LOG_LEVEL, \
set_global_log_level
from wrolpi.dates import now, strptime
from wrolpi.downloader import download_manager
from wrolpi.errors import WROLModeEnabled, APIError, HotspotError, InvalidDownload, \
HotspotPasswordTooShort, NativeOnly
HotspotPasswordTooShort, NativeOnly, InvalidConfig
from wrolpi.events import get_events, Events
from wrolpi.files.lib import get_file_statistics, estimate_search
from wrolpi.vars import API_HOST, API_PORT, DOCKERIZED, API_DEBUG, API_ACCESS_LOG, API_WORKERS, API_AUTO_RELOAD, \
@ -149,6 +150,7 @@ def get_settings(_: Request):
'hotspot_password': config.hotspot_password,
'hotspot_ssid': config.hotspot_ssid,
'hotspot_status': admin.hotspot_status().name,
'log_level': LOG_LEVEL.value,
'ignore_outdated_zims': config.ignore_outdated_zims,
'media_directory': str(get_media_directory()), # Convert to string to avoid conversion to relative.
'throttle_on_startup': config.throttle_on_startup,
@ -162,7 +164,7 @@ def get_settings(_: Request):
@api_bp.patch('/settings')
@openapi.description('Update WROLPi settings')
@validate(json=schema.SettingsRequest)
def update_settings(_: Request, body: schema.SettingsRequest):
def update_settings(request: Request, body: schema.SettingsRequest):
if wrol_mode_enabled() and body.wrol_mode is None:
# Cannot update settings while WROL Mode is enabled, unless you want to disable WROL Mode.
raise WROLModeEnabled()
@ -182,6 +184,14 @@ def update_settings(_: Request, body: schema.SettingsRequest):
# Remove any keys with None values, then save the config.
config = {k: v for k, v in body.__dict__.items() if v is not None}
wrolpi_config = get_config()
if not config:
raise InvalidConfig()
log_level = config.pop('log_level', None)
if isinstance(log_level, int):
set_global_log_level(log_level)
old_password = wrolpi_config.hotspot_password
wrolpi_config.update(config)

View file

@ -22,6 +22,7 @@ class SettingsRequest:
hotspot_ssid: Optional[str] = None
hotspot_status: Optional[bool] = None
ignore_outdated_zims: Optional[bool] = None
log_level: Optional[int] = None
media_directory: Optional[str] = None
throttle_on: Optional[bool] = None
throttle_on_startup: Optional[bool] = None

View file

@ -57,6 +57,34 @@ async def test_get_settings(test_session, test_async_client):
assert response.status_code == HTTPStatus.OK
@pytest.mark.asyncio
async def test_log_level(test_async_client):
"""Log level must be between 0 and 40."""
data = {'log_level': 0}
request, response = await test_async_client.patch('/api/settings', content=json.dumps(data))
assert response.status_code == HTTPStatus.NO_CONTENT
data = {'log_level': -1}
request, response = await test_async_client.patch('/api/settings', content=json.dumps(data))
assert response.status_code == HTTPStatus.BAD_REQUEST
data = {'log_level': 40}
request, response = await test_async_client.patch('/api/settings', content=json.dumps(data))
assert response.status_code == HTTPStatus.NO_CONTENT
data = {'log_level': 41}
request, response = await test_async_client.patch('/api/settings', content=json.dumps(data))
assert response.status_code == HTTPStatus.BAD_REQUEST
data = {'log_level': None}
request, response = await test_async_client.patch('/api/settings', content=json.dumps(data))
assert response.status_code == HTTPStatus.BAD_REQUEST
data = {}
request, response = await test_async_client.patch('/api/settings', content=json.dumps(data))
assert response.status_code == HTTPStatus.BAD_REQUEST
@pytest.mark.asyncio
async def test_get_downloads(test_session, test_async_client):
request, response = await test_async_client.get('/api/download')