From 9e6b1cf716e5673b4b83f6709e84d8e3333b3af2 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Sun, 20 Feb 2022 14:13:16 -0800 Subject: [PATCH] Move logging config to a file. By default logging configuration is defined by resources/default_logging.yaml. Very noisy loggers (like the uvicorn.access logger) are kept out of the console and UI logs by default. Developers (or weird users) can customize their default logging config by copying the file to resources/logging.yaml and editing as needed. It would be preferable to load this file form the Liberation user directory, but because first-time initialization requires the UI, we want to configure logging before we necessarily know where to find that. --- .gitignore | 6 +---- game/server/server.py | 3 ++- qt_ui/logging_config.py | 31 +++++++++----------------- resources/default_logging.yaml | 40 ++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 resources/default_logging.yaml diff --git a/.gitignore b/.gitignore index 7d556efc..a05b7650 100644 --- a/.gitignore +++ b/.gitignore @@ -3,12 +3,9 @@ __pycache__ build/** resources/payloads/*.lua venv -logs.txt .DS_Store .vscode/settings.json dist/** -a.py -resources/tools/a.miz # User-specific stuff .idea/ .env @@ -19,7 +16,6 @@ env/ /state.json /logs/ - -qt_ui/logs/liberation.log +/resources/logging.yaml *.psd diff --git a/game/server/server.py b/game/server/server.py index 34fcfe02..fdbb2151 100644 --- a/game/server/server.py +++ b/game/server/server.py @@ -17,7 +17,8 @@ class Server(uvicorn.Server): app=app, host=ServerSettings.get().server_bind_address, port=ServerSettings.get().server_port, - log_level="info", + # Configured explicitly with default_logging.yaml or logging.yaml. + log_config=None, ) ) diff --git a/qt_ui/logging_config.py b/qt_ui/logging_config.py index 8d7a26af..2272e5b0 100644 --- a/qt_ui/logging_config.py +++ b/qt_ui/logging_config.py @@ -1,9 +1,10 @@ """Logging APIs.""" import logging +import logging.config import os -from logging.handlers import RotatingFileHandler +from pathlib import Path -from qt_ui.logging_handler import HookableInMemoryHandler +import yaml def init_logging(version: str) -> None: @@ -11,23 +12,11 @@ def init_logging(version: str) -> None: if not os.path.isdir("./logs"): os.mkdir("logs") - fmt = "%(asctime)s :: %(levelname)s :: %(message)s" - formatter = logging.Formatter(fmt) + resources = Path("resources") + log_config = resources / "default_logging.yaml" + if (custom_log_config := resources / "logging.yaml").exists(): + log_config = custom_log_config + with log_config.open() as log_file: + logging.config.dictConfig(yaml.safe_load(log_file)) - logging.basicConfig(level=logging.DEBUG, format=fmt) - logger = logging.getLogger() - - rotating_file_handler = RotatingFileHandler( - "./logs/liberation.log", "a", 5000000, 1 - ) - rotating_file_handler.setLevel(logging.DEBUG) - rotating_file_handler.setFormatter(formatter) - - hookable_in_memory_handler = HookableInMemoryHandler() - hookable_in_memory_handler.setLevel(logging.DEBUG) - hookable_in_memory_handler.setFormatter(formatter) - - logger.addHandler(rotating_file_handler) - logger.addHandler(hookable_in_memory_handler) - - logger.info(f"DCS Liberation {version}") + logging.info(f"DCS Liberation {version}") diff --git a/resources/default_logging.yaml b/resources/default_logging.yaml new file mode 100644 index 00000000..9e2fe9d9 --- /dev/null +++ b/resources/default_logging.yaml @@ -0,0 +1,40 @@ +# To override default logging behavior, copy this file to resources/logging.yaml +# and make changes. logging.yaml will take precedence over the default config if +# it exists, but is not tracked by git. +# +# See https://gist.github.com/kingspp/9451566a5555fb022215ca2b7b802f19 and +# https://docs.python.org/3/howto/logging.html for examples. +version: 1 +formatters: + default: + format: "%(asctime)s :: %(name)s :: %(levelname)s :: %(message)s" +handlers: + console: + class: logging.StreamHandler + level: DEBUG + formatter: default + stream: ext://sys.stderr + file: + class: logging.handlers.RotatingFileHandler + level: DEBUG + formatter: default + filename: logs/liberation.log + maxBytes: 5242880 + backupCount: 1 + encoding: utf8 + in_memory: + class: qt_ui.logging_handler.HookableInMemoryHandler + level: DEBUG + formatter: default +root: + level: DEBUG + handlers: [console, file, in_memory] +loggers: + uvicorn.access: + # uvicorn.access logs every HTTP request and its result. It's useful while + # debugging that interface, but otherwise is very noisy, so by default is + # only logged to the file. To include this in the console, add console to + # the list of handlers, or set propagate to true, which will also log to the + # UI's log window. + handlers: [file] + propagate: false