mirror of
https://github.com/dcs-liberation/dcs_liberation.git
synced 2025-11-10 14:22:26 +00:00
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:
parent
c16ca40894
commit
9e6b1cf716
6
.gitignore
vendored
6
.gitignore
vendored
@ -3,12 +3,9 @@ __pycache__
|
|||||||
build/**
|
build/**
|
||||||
resources/payloads/*.lua
|
resources/payloads/*.lua
|
||||||
venv
|
venv
|
||||||
logs.txt
|
|
||||||
.DS_Store
|
.DS_Store
|
||||||
.vscode/settings.json
|
.vscode/settings.json
|
||||||
dist/**
|
dist/**
|
||||||
a.py
|
|
||||||
resources/tools/a.miz
|
|
||||||
# User-specific stuff
|
# User-specific stuff
|
||||||
.idea/
|
.idea/
|
||||||
.env
|
.env
|
||||||
@ -19,7 +16,6 @@ env/
|
|||||||
/state.json
|
/state.json
|
||||||
|
|
||||||
/logs/
|
/logs/
|
||||||
|
/resources/logging.yaml
|
||||||
qt_ui/logs/liberation.log
|
|
||||||
|
|
||||||
*.psd
|
*.psd
|
||||||
|
|||||||
@ -17,7 +17,8 @@ class Server(uvicorn.Server):
|
|||||||
app=app,
|
app=app,
|
||||||
host=ServerSettings.get().server_bind_address,
|
host=ServerSettings.get().server_bind_address,
|
||||||
port=ServerSettings.get().server_port,
|
port=ServerSettings.get().server_port,
|
||||||
log_level="info",
|
# Configured explicitly with default_logging.yaml or logging.yaml.
|
||||||
|
log_config=None,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
"""Logging APIs."""
|
"""Logging APIs."""
|
||||||
import logging
|
import logging
|
||||||
|
import logging.config
|
||||||
import os
|
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:
|
def init_logging(version: str) -> None:
|
||||||
@ -11,23 +12,11 @@ def init_logging(version: str) -> None:
|
|||||||
if not os.path.isdir("./logs"):
|
if not os.path.isdir("./logs"):
|
||||||
os.mkdir("logs")
|
os.mkdir("logs")
|
||||||
|
|
||||||
fmt = "%(asctime)s :: %(levelname)s :: %(message)s"
|
resources = Path("resources")
|
||||||
formatter = logging.Formatter(fmt)
|
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)
|
logging.info(f"DCS Liberation {version}")
|
||||||
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}")
|
|
||||||
|
|||||||
40
resources/default_logging.yaml
Normal file
40
resources/default_logging.yaml
Normal 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
|
||||||
Loading…
x
Reference in New Issue
Block a user