Skip to content

Commit

Permalink
feat: Enhanced logging configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
pmhahn committed Jul 27, 2023
1 parent fa130ff commit 7c4d6d9
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 6 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ repos:
- id: mypy
additional_dependencies:
- typed_ast
- types-PyYAML

- repo: https://github.com/timothycrosley/isort
rev: 5.12.0
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
35 changes: 35 additions & 0 deletions logging.ini
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions logging.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ exclude = (?x)(
|^bdist/
|^build/
|^debian/
|^venv/
)

[mypy-yaml.*]
ignore_missing_imports = True
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ install_requires =

[options.extras_require]
lxml = lxml>=3,<4
yaml = PyYAML
31 changes: 25 additions & 6 deletions src/vncautomate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 7c4d6d9

Please sign in to comment.