-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bf97a4
commit 5ae0cff
Showing
13 changed files
with
186 additions
and
105 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
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,58 @@ | ||
import logging | ||
from termcolor import colored | ||
|
||
|
||
class ColorizingStreamHandler(logging.StreamHandler): | ||
dark_theme = { | ||
'DEBUG': 'white', | ||
'INFO': 'green', | ||
'WARNING': 'yellow', | ||
'ERROR': 'red', | ||
'CRITICAL': 'red', | ||
} | ||
|
||
light_theme = { | ||
'DEBUG': 'black', | ||
'INFO': 'blue', | ||
'WARNING': 'magenta', | ||
'ERROR': 'red', | ||
'CRITICAL': 'red', | ||
} | ||
|
||
vibrant_theme = { | ||
'DEBUG': 'cyan', | ||
'INFO': 'green', | ||
'WARNING': 'yellow', | ||
'ERROR': 'red', | ||
'CRITICAL': 'red', | ||
} | ||
|
||
def __init__(self, theme='dark'): | ||
super().__init__() | ||
self.set_theme(theme) | ||
|
||
def set_theme(self, theme): | ||
if theme == 'dark': | ||
self.color_map = self.dark_theme | ||
elif theme == 'light': | ||
self.color_map = self.light_theme | ||
elif theme == 'vibrant': | ||
self.color_map = self.vibrant_theme | ||
elif theme == 'default': | ||
self.color_map = {} # No color applied | ||
else: | ||
self.color_map = {} # No color applied | ||
|
||
def emit(self, record): | ||
try: | ||
# Separate the log level from the message | ||
level = '[{}]'.format(record.levelname) | ||
color = self.color_map.get(record.levelname, 'white') | ||
colored_level = colored(level, color) | ||
|
||
# Combine the colored log level with the rest of the message | ||
message = self.format(record).replace(level, colored_level) | ||
self.stream.write(message + "\n") | ||
self.flush() | ||
except Exception: | ||
self.handleError(record) |
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,49 @@ | ||
import logging | ||
import logging.config | ||
import urllib3 | ||
from fedn.common.color_handler import ColorizingStreamHandler | ||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) | ||
logging.getLogger("urllib3").setLevel(logging.ERROR) | ||
|
||
handler = ColorizingStreamHandler(theme='dark') | ||
logger = logging.getLogger() | ||
logger.addHandler(handler) | ||
logger.setLevel(logging.DEBUG) | ||
formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S') | ||
handler.setFormatter(formatter) | ||
|
||
|
||
def set_log_level_from_string(level_str): | ||
""" | ||
Set the log level based on a string input. | ||
""" | ||
# Mapping of string representation to logging constants | ||
level_mapping = { | ||
'CRITICAL': logging.CRITICAL, | ||
'ERROR': logging.ERROR, | ||
'WARNING': logging.WARNING, | ||
'INFO': logging.INFO, | ||
'DEBUG': logging.DEBUG, | ||
} | ||
|
||
# Get the logging level from the mapping | ||
level = level_mapping.get(level_str.upper()) | ||
|
||
if not level: | ||
raise ValueError(f"Invalid log level: {level_str}") | ||
|
||
# Set the log level | ||
logger.setLevel(level) | ||
|
||
|
||
def set_theme_from_string(theme_str): | ||
""" | ||
Set the logging color theme based on a string input. | ||
""" | ||
# Check if the theme string is valid | ||
valid_themes = ['dark', 'light', 'vibrant', 'default'] | ||
if theme_str.lower() not in valid_themes: | ||
raise ValueError(f"Invalid theme: {theme_str}. Valid themes are: {', '.join(valid_themes)}") | ||
|
||
# Set the theme for the ColorizingStreamHandler | ||
handler.set_theme(theme_str.lower()) |
Oops, something went wrong.