-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configuration: move logging to separate class
- Loading branch information
Christoph Weiss
committed
Mar 1, 2024
1 parent
b8eefa0
commit 5742033
Showing
2 changed files
with
50 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env python | ||
# SPDX-License-Identifier: GTDGmbH | ||
# Copyright 2024 by GTD GmbH. | ||
"""Class configuring the OCGraph logging.""" | ||
import logging | ||
|
||
# fmt: off | ||
logging_preset: dict[str, dict] = { | ||
"development": { | ||
"file_log": True, | ||
"file_level": logging.DEBUG, | ||
"console_log": True, | ||
"console_level": logging.DEBUG | ||
}, | ||
|
||
"module": { | ||
"file_log": False, | ||
"file_level": logging.ERROR, | ||
"console_log": False, | ||
"console_level": logging.ERROR | ||
}, | ||
|
||
"default": { | ||
"file_log": True, | ||
"file_level": logging.INFO, | ||
"console_log": True, | ||
"console_level": logging.INFO | ||
}, | ||
} | ||
# fmt: on | ||
|
||
|
||
class OCGraphLogger(logging.Logger): | ||
"""Logging mechanism for module""" | ||
|
||
def __init__(self, name: str, preset="default", file=""): | ||
super().__init__(name) | ||
log_config = logging_preset.get(preset) | ||
if log_config["file_log"]: | ||
logging_file = file + ".log" | ||
file_stream: logging.FileHandler = logging.FileHandler(logging_file) | ||
file_stream.setLevel(log_config["file_level"]) | ||
self.addHandler(file_stream) | ||
if log_config["console_log"]: | ||
console_stream: logging.StreamHandler = logging.StreamHandler() | ||
console_stream.setLevel(log_config["console_level"]) | ||
self.addHandler(console_stream) |