upd set_logging for improved readability

This commit is contained in:
onyx-and-iris 2026-02-09 10:32:13 +00:00
parent 71d4a81855
commit 998e72f43e
2 changed files with 12 additions and 6 deletions

View File

@ -50,7 +50,7 @@ The CLI should now be discoverable as `obsws-cli`
- --timeout/-T: Websocket timeout
- --version/-v: Print the obsws-cli version
- --loglevel/-l: Set the application's logging level
- One of *CRITICAL, FATAL, ERROR, WARNING, INFO, DEBUG*
- One of *NOTSET, DEBUG, INFO, WARN, WARNING, ERROR, CRITICAL, FATAL*
Pass `--host`, `--port` and `--password` as flags on the root command, for example:

View File

@ -30,13 +30,19 @@ def version_callback(value: bool):
def setup_logging(loglevel: str):
"""Set up logging for the application."""
numeric_loglevel = logging.getLevelNamesMapping().get(loglevel.upper())
if numeric_loglevel is None:
raise typer.BadParameter(
f'Invalid log level: {loglevel}. Valid options are: {", ".join(logging.getLevelNamesMapping().keys())}'
level_map = logging.getLevelNamesMapping()
try:
level_int = level_map[loglevel.upper()]
except KeyError:
possible_levels = ', '.join(
sorted(level_map.keys(), key=lambda k: level_map[k])
)
raise typer.BadParameter(
f'Invalid log level: {loglevel}. Valid options are: {possible_levels}'
) from None
logging.basicConfig(
level=numeric_loglevel,
level=level_int,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
)