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.
This commit is contained in:
Dan Albert 2022-02-20 14:13:16 -08:00
parent c16ca40894
commit 9e6b1cf716
4 changed files with 53 additions and 27 deletions

6
.gitignore vendored
View File

@ -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

View File

@ -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,
)
)

View File

@ -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}")

View File

@ -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