From 7c4d6d9e45ca3409a140fb3064bc1ba2e9dd2c87 Mon Sep 17 00:00:00 2001 From: Philipp Hahn Date: Sat, 26 Nov 2022 16:30:12 +0100 Subject: [PATCH] feat: Enhanced logging configuration --- .pre-commit-config.yaml | 1 + Dockerfile | 2 ++ README.md | 1 + logging.ini | 35 +++++++++++++++++++++++++++++++++++ logging.yaml | 28 ++++++++++++++++++++++++++++ mypy.ini | 4 ++++ setup.cfg | 1 + src/vncautomate/__init__.py | 31 +++++++++++++++++++++++++------ 8 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 logging.ini create mode 100644 logging.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5a12834..46c5631 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -21,6 +21,7 @@ repos: - id: mypy additional_dependencies: - typed_ast + - types-PyYAML - repo: https://github.com/timothycrosley/isort rev: 5.12.0 diff --git a/Dockerfile b/Dockerfile index 7de1d0e..aded596 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,11 +11,13 @@ RUN apt-get -qq update \ python3-setuptools \ python3-twisted \ python3-wheel \ + python3-yaml \ python-pil \ python-pip \ python-setuptools \ python-twisted \ python-wheel \ + python-yaml \ && pip install vncdotool==1.0.0 \ && pip3 install vncdotool \ && rm -rf \ diff --git a/README.md b/README.md index 12b0ade..5797ee9 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ python utils/installation_test/vnc-install-ucs.py --vnc isala:2 --language deu - ``` mkdir dump/ +VNCAUTOMATE_DEBUG=logging.yaml \ VNCAUTOMATE_TMP=1 \ python3 -m vncautomate.cli \ --log debug \ diff --git a/logging.ini b/logging.ini new file mode 100644 index 0000000..2cd4da8 --- /dev/null +++ b/logging.ini @@ -0,0 +1,35 @@ +[loggers] +keys=root,pil + +[logger_root] +level=DEBUG +handlers=console,file + +[logger_pil] +level=ERROR +propagate=0 +qualname=PIL + +[handlers] +keys=console,file + +[handler_console] +class=StreamHandler +formatter=brief +level=INFO +args=(sys.stdout,) + +[handler_file] +class=FileHandler +formatter=precise +level=DEBUG +args=("vnc-automate2.log", "w") + +[formatters] +keys=brief,precise + +[formatter_brief] +format=%(asctime)s %(message)s + +[formatter_precise] +format=%(asctime)s %(levelname)s [%(name)s:%(module)s:%(funcName)s]: %(message)s diff --git a/logging.yaml b/logging.yaml new file mode 100644 index 0000000..966ab61 --- /dev/null +++ b/logging.yaml @@ -0,0 +1,28 @@ +version: 1 +formatters: + brief: + format: "%(asctime)s %(message)s" + precise: + format: "%(asctime)s %(levelname)s [%(name)s:%(module)s:%(funcName)s]: %(message)s" +handlers: + console: + class: logging.StreamHandler + formatter: brief + level: INFO + stream: ext://sys.stdout + file: + class: logging.FileHandler + formatter: precise + level: DEBUG + filename: vnc-automate3.log +loggers: + PIL: + level: ERROR + propagate: False +root: + level: DEBUG + handlers: + - console + - file +#incremental: True +#disable_existing_loggers: False diff --git a/mypy.ini b/mypy.ini index fafe001..f398017 100644 --- a/mypy.ini +++ b/mypy.ini @@ -8,4 +8,8 @@ exclude = (?x)( |^bdist/ |^build/ |^debian/ + |^venv/ ) + +[mypy-yaml.*] +ignore_missing_imports = True diff --git a/setup.cfg b/setup.cfg index f1f858e..10173cc 100644 --- a/setup.cfg +++ b/setup.cfg @@ -49,3 +49,4 @@ install_requires = [options.extras_require] lxml = lxml>=3,<4 +yaml = PyYAML diff --git a/src/vncautomate/__init__.py b/src/vncautomate/__init__.py index daeffd9..1e869a2 100644 --- a/src/vncautomate/__init__.py +++ b/src/vncautomate/__init__.py @@ -32,6 +32,8 @@ # import logging +import logging.config +import os import sys from types import TracebackType # noqa: F401 from typing import Optional, Type # noqa: F401 @@ -47,14 +49,31 @@ def init_logger(debug_level="info"): # type: (str) -> None + debug = os.getenv("VNCAUTOMATE_DEBUG", "") + if debug and os.path.exists(debug): + if debug.endswith((".ini", ".cfg")): + logging.config.fileConfig(debug) + elif debug.endswith((".yaml", ".yml", ".json")): + import yaml + + with open(debug, "r") as fd: + conf = yaml.safe_load(fd) + + logging.config.dictConfig(conf) + else: + sys.exit("Unknown log configuration %r" % (debug,)) + + return + try: - logging.basicConfig( - format="%(asctime)s %(levelname)s [%(name)s:%(module)s:%(funcName)s]: %(message)s", - level=getattr(logging, debug_level.upper()), - ) + level = getattr(logging, debug_level.upper()) except AttributeError: - logging.error('Given log level "%s" is unknown', debug_level) - sys.exit(1) + sys.exit("Unknown log level %r" % (debug_level,)) + + logging.basicConfig( + format="%(asctime)s %(levelname)s [%(name)s:%(module)s:%(funcName)s]: %(message)s", + level=level, + ) def connect_vnc(host):