Skip to content

Commit

Permalink
Started cleaning up the log.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhellander committed Oct 20, 2023
1 parent 9bf97a4 commit 5ae0cff
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 105 deletions.
5 changes: 0 additions & 5 deletions fedn/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import logging

import click

logging.basicConfig(format='%(asctime)s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p') # , level=logging.DEBUG)

CONTEXT_SETTINGS = dict(
# Support -h as a shortcut for --help
help_option_names=['-h', '--help'],
Expand Down
9 changes: 7 additions & 2 deletions fedn/cli/run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ def run_cmd(ctx):
help='Set logfile for client log to file.')
@click.option('--heartbeat-interval', required=False, default=2)
@click.option('--reconnect-after-missed-heartbeat', required=False, default=30)
@click.option('--verbosity', required=False, default='INFO', type=click.Choice(['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'], case_sensitive=False))
@click.option('--theme', required=False, default='default', type=click.Choice(['dark', 'light', 'vibrant', 'default'], case_sensitive=False))
@click.pass_context
def client_cmd(ctx, discoverhost, discoverport, token, name, client_id, local_package, force_ssl, dry_run, secure, preshared_cert,
verify, preferred_combiner, validator, trainer, init, logfile, heartbeat_interval, reconnect_after_missed_heartbeat):
verify, preferred_combiner, validator, trainer, init, logfile, heartbeat_interval, reconnect_after_missed_heartbeat,
verbosity, theme):
"""
:param ctx:
Expand All @@ -127,14 +130,16 @@ def client_cmd(ctx, discoverhost, discoverport, token, name, client_id, local_pa
:param logfile:
:param hearbeat_interval
:param reconnect_after_missed_heartbeat
:param verbosity
:param theme
:return:
"""
remote = False if local_package else True
config = {'discover_host': discoverhost, 'discover_port': discoverport, 'token': token, 'name': name,
'client_id': client_id, 'remote_compute_context': remote, 'force_ssl': force_ssl, 'dry_run': dry_run, 'secure': secure,
'preshared_cert': preshared_cert, 'verify': verify, 'preferred_combiner': preferred_combiner,
'validator': validator, 'trainer': trainer, 'init': init, 'logfile': logfile, 'heartbeat_interval': heartbeat_interval,
'reconnect_after_missed_heartbeat': reconnect_after_missed_heartbeat}
'reconnect_after_missed_heartbeat': reconnect_after_missed_heartbeat, 'verbosity': verbosity, 'theme': theme}

if init:
apply_config(config)
Expand Down
58 changes: 58 additions & 0 deletions fedn/fedn/common/color_handler.py
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)
49 changes: 49 additions & 0 deletions fedn/fedn/common/log_config.py
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())
Loading

0 comments on commit 5ae0cff

Please sign in to comment.