Skip to content

Commit

Permalink
Removed coloring and fixed redirection of logging to file.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhellander committed Nov 13, 2023
1 parent 3965989 commit 771b007
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 74 deletions.
8 changes: 3 additions & 5 deletions fedn/cli/run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,15 @@ def run_cmd(ctx):
@click.option('-tr', '--trainer', required=False, default=True)
@click.option('-in', '--init', required=False, default=None,
help='Set to a filename to (re)init client from file state.')
@click.option('-l', '--logfile', required=False, default='{}-client.log'.format(time.strftime("%Y%m%d-%H%M%S")),
@click.option('-l', '--logfile', required=False, default=None,
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', '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,
verbosity, theme):
verbosity):
"""
:param ctx:
Expand All @@ -131,15 +130,14 @@ def client_cmd(ctx, discoverhost, discoverport, token, name, client_id, local_pa
: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, 'verbosity': verbosity, 'theme': theme}
'reconnect_after_missed_heartbeat': reconnect_after_missed_heartbeat, 'verbosity': verbosity}

if init:
apply_config(config)
Expand Down
49 changes: 0 additions & 49 deletions fedn/fedn/common/color_handler.py

This file was deleted.

27 changes: 15 additions & 12 deletions fedn/fedn/common/log_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

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')
handler = logging.StreamHandler()
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -38,15 +36,20 @@ def set_log_level_from_string(level_str):
# Set the log level
logger.setLevel(level)


def set_theme_from_string(theme_str):
def set_log_stream(log_file):
"""
Set the logging color theme based on a string input.
Redirect the log stream to a specified file, if log_file is set.
"""
# Check if the theme string is valid
valid_themes = ['dark', 'light', 'default']
if theme_str.lower() not in valid_themes:
raise ValueError(f"Invalid theme: {theme_str}. Valid themes are: {', '.join(valid_themes)}")
if not log_file:
return

# Remove existing handlers
for h in logger.handlers[:]:
logger.removeHandler(h)

# Create a FileHandler
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(formatter)

# Set the theme for the ColorizingStreamHandler
handler.set_theme(theme_str.lower())
# Add the file handler to the logger
logger.addHandler(file_handler)
8 changes: 3 additions & 5 deletions fedn/fedn/network/clients/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import fedn.common.net.grpc.fedn_pb2 as fedn
import fedn.common.net.grpc.fedn_pb2_grpc as rpc
from fedn.common.log_config import (logger, set_log_level_from_string,
set_theme_from_string)
set_log_stream)
from fedn.network.clients.connect import ConnectorClient, Status
from fedn.network.clients.package import PackageRuntime
from fedn.network.clients.state import ClientState, ClientStateToString
Expand Down Expand Up @@ -57,7 +57,7 @@ def __init__(self, config):
self.config = config

set_log_level_from_string(config.get('verbosity', "INFO"))
set_theme_from_string(config.get('theme', 'default'))
set_log_stream(config.get('logfile', None))

self.connector = ConnectorClient(host=config['discover_host'],
port=config['discover_port'],
Expand All @@ -79,8 +79,6 @@ def __init__(self, config):
self.run_path = os.path.join(os.getcwd(), dirname)
os.mkdir(self.run_path)

# self.logger = Logger(
# to_file=config['logfile'], file_path=self.run_path)
self.started_at = datetime.now()
self.logs = []

Expand Down Expand Up @@ -666,7 +664,7 @@ def _send_heartbeat(self, update_frequency=2.0):
self._missed_heartbeat = 0
except grpc.RpcError as e:
status_code = e.code()
logger.warning("CLIENT heartbeat: GRPC ERROR {} retrying..".format(
logger.warning("Client heartbeat: GRPC error, {}. Retrying.".format(
status_code.name))
logger.debug(e)
self._handle_combiner_failure()
Expand Down
6 changes: 3 additions & 3 deletions fedn/fedn/network/clients/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def unpack(self):
f = tarfile.open(os.path.join(
self.pkg_path, self.pkg_name), 'r:bz2')
else:
print(
logger.error(
"Failed to unpack compute package, no pkg_name set."
"Has the reducer been configured with a compute package?"
)
Expand All @@ -138,11 +138,11 @@ def unpack(self):

if f:
f.extractall()
print("Successfully extracted compute package content in {}".format(
logger.info("Successfully extracted compute package content in {}".format(
self.dir), flush=True)
return True
except Exception:
print("Error extracting files!")
logger.error("Error extracting files!")
return False

def dispatcher(self, run_path):
Expand Down

0 comments on commit 771b007

Please sign in to comment.