-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor logging mechanism for kuksa-client #28
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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,121 @@ | ||
# /******************************************************************************** | ||
# * Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
# * | ||
# * See the NOTICE file(s) distributed with this work for additional | ||
# * information regarding copyright ownership. | ||
# * | ||
# * This program and the accompanying materials are made available under the | ||
# * terms of the Apache License 2.0 which is available at | ||
# * http://www.apache.org/licenses/LICENSE-2.0 | ||
# * | ||
# * SPDX-License-Identifier: Apache-2.0 | ||
# ********************************************************************************/ | ||
|
||
# This file contains a logger that could be useful for clients using kuksa-python-sdk | ||
|
||
import logging | ||
import sys | ||
import os | ||
|
||
|
||
class KuksaLogger: | ||
def init_logging(self): | ||
# Example | ||
# | ||
# Set log level to debug | ||
# LOG_LEVEL=debug kuksa-client | ||
# | ||
# Set log level to INFO, but for dbcfeederlib.databrokerclientwrapper set it to DEBUG | ||
# LOG_LEVEL=info,dbcfeederlib.databrokerclientwrapper=debug kuksa-client | ||
# | ||
# | ||
|
||
loglevels = self.parse_env_log(os.environ.get("LOG_LEVEL")) | ||
|
||
# set root loglevel etc | ||
self.init_root_logging(loglevels["root"]) | ||
|
||
# set loglevels for other loggers | ||
for logger, level in loglevels.items(): | ||
if logger != "root": | ||
logging.getLogger(logger).setLevel(level) | ||
|
||
def init_root_logging(self, loglevel): | ||
"""Set up console logger""" | ||
# create console handler and set level to debug. This just means that it can show DEBUG messages. | ||
# What actually is shown is controlled by logging configuration | ||
console_logger = logging.StreamHandler() | ||
console_logger.setLevel(logging.DEBUG) | ||
|
||
# create formatter | ||
if sys.stdout.isatty(): | ||
formatter = ColorFormatter() | ||
else: | ||
formatter = logging.Formatter( | ||
fmt="%(asctime)s %(levelname)s %(name)s: %(message)s" | ||
) | ||
|
||
# add formatter to console_logger | ||
console_logger.setFormatter(formatter) | ||
|
||
# add console_logger as a global handler | ||
root_logger = logging.getLogger() | ||
root_logger.setLevel(loglevel) | ||
root_logger.addHandler(console_logger) | ||
|
||
def parse_env_log(self, env_log, default=logging.INFO): | ||
def parse_level(specified_level, default=default): | ||
if isinstance(specified_level, str): | ||
if specified_level.lower() in [ | ||
"debug", | ||
"info", | ||
"warn", | ||
"warning", | ||
"error", | ||
"critical", | ||
]: | ||
return specified_level.upper() | ||
raise ValueError(f"could not parse '{specified_level}' as a log level") | ||
return default | ||
|
||
parsed_loglevels = {} | ||
|
||
if env_log is not None: | ||
log_specs = env_log.split(",") | ||
for log_spec in log_specs: | ||
spec_parts = log_spec.split("=") | ||
if len(spec_parts) == 1: | ||
# This is a root level spec | ||
if "root" in parsed_loglevels: | ||
raise ValueError("multiple root loglevels specified") | ||
parsed_loglevels["root"] = parse_level(spec_parts[0]) | ||
if len(spec_parts) == 2: | ||
logger_name = spec_parts[0] | ||
logger_level = spec_parts[1] | ||
parsed_loglevels[logger_name] = parse_level(logger_level) | ||
|
||
if "root" not in parsed_loglevels: | ||
parsed_loglevels["root"] = default | ||
|
||
return parsed_loglevels | ||
|
||
|
||
class ColorFormatter(logging.Formatter): | ||
"""Color formatter that can be used for terminals""" | ||
FORMAT = "{time} {{loglevel}} {logger} {msg}".format( | ||
time="\x1b[2m%(asctime)s\x1b[0m", # grey | ||
logger="\x1b[2m%(name)s:\x1b[0m", # grey | ||
msg="%(message)s", | ||
) | ||
FORMATS = { | ||
logging.DEBUG: FORMAT.format(loglevel="\x1b[34mDEBUG\x1b[0m"), # blue | ||
logging.INFO: FORMAT.format(loglevel="\x1b[32mINFO\x1b[0m"), # green | ||
logging.WARNING: FORMAT.format(loglevel="\x1b[33mWARNING\x1b[0m"), # yellow | ||
logging.ERROR: FORMAT.format(loglevel="\x1b[31mERROR\x1b[0m"), # red | ||
logging.CRITICAL: FORMAT.format(loglevel="\x1b[31mCRITICAL\x1b[0m"), # red | ||
} | ||
|
||
def format(self, record): | ||
log_fmt = self.FORMATS.get(record.levelno) | ||
formatter = logging.Formatter(log_fmt) | ||
return formatter.format(record) |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an improvement related to #27 - give an error if we by some reason cannot dump the response as json.