From a2fec3593f02950264645bc53d13f8f7bdd3065c Mon Sep 17 00:00:00 2001 From: stefanhellander Date: Fri, 20 Oct 2023 10:18:20 +0200 Subject: [PATCH] Fixed some stuff again after merging in develop branch. --- fedn/cli/main.py | 5 --- fedn/fedn/network/clients/client.py | 53 ++++++++++++--------------- fedn/fedn/network/clients/connect.py | 4 +- fedn/fedn/network/combiner/connect.py | 4 +- fedn/fedn/utils/dispatcher.py | 5 +-- fedn/fedn/utils/logger.py | 33 ----------------- fedn/fedn/utils/process.py | 5 +-- 7 files changed, 30 insertions(+), 79 deletions(-) delete mode 100644 fedn/fedn/utils/logger.py diff --git a/fedn/cli/main.py b/fedn/cli/main.py index cc33c579b..d004c9605 100644 --- a/fedn/cli/main.py +++ b/fedn/cli/main.py @@ -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'], diff --git a/fedn/fedn/network/clients/client.py b/fedn/fedn/network/clients/client.py index 8a65d9442..f11598817 100644 --- a/fedn/fedn/network/clients/client.py +++ b/fedn/fedn/network/clients/client.py @@ -19,6 +19,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 from fedn.network.clients.connect import ConnectorClient, Status from fedn.network.clients.package import PackageRuntime from fedn.network.clients.state import ClientState, ClientStateToString @@ -49,7 +50,13 @@ class Client: def __init__(self, config): """Initialize the client.""" - + print(""" _____ _ _ ______ ______ _____ + / ____| | | | | | ____| ____| __ \ + | (___ ___ __ _| | ___ ___ _ _| |_ | |__ | |__ | | | |_ __ + \___ \ / __/ _` | |/ _ \/ _ \| | | | __| | __| | __| | | | | '_ \ + ____) | (_| (_| | | __/ (_) | |_| | |_ | | | |____| |__| | | | | + |_____/ \___\__,_|_|\___|\___/ \__,_|\__| |_| |______|_____/|_| |_| +""") self.state = None self.error_state = False self._attached = False @@ -107,27 +114,26 @@ def _assign(self): :rtype: dict """ - print("Asking for assignment!", flush=True) + logger.info("Initiating assignment request.") while True: status, response = self.connector.assign() if status == Status.TryAgain: - print(response, flush=True) + logger.info(response) time.sleep(5) continue if status == Status.Assigned: client_config = response break if status == Status.UnAuthorized: - print(response, flush=True) + logger.warning(response) sys.exit("Exiting: Unauthorized") if status == Status.UnMatchedConfig: - print(response, flush=True) + logger.warning(response) sys.exit("Exiting: UnMatchedConfig") time.sleep(5) - print(".", end=' ', flush=True) - print("Got assigned!", flush=True) - print("Received combiner config: {}".format(client_config), flush=True) + logger.info("Assignment successfully received.") + logger.info("Received combiner configuration: {}".format(client_config)) return client_config def _connect(self, client_config): @@ -146,10 +152,10 @@ def _connect(self, client_config): host = client_config['fqdn'] # assuming https if fqdn is used port = 443 - print(f"CLIENT: Connecting to combiner host: {host}:{port}", flush=True) + logger.info(f"Initiating connection to combiner host at: {host}:{port}", flush=True) if client_config['certificate']: - print("CLIENT: using certificate from Reducer for GRPC channel") + logger.info("Utilizing CA certificate for GRPC channel authentication.") secure = True cert = base64.b64decode( client_config['certificate']) # .decode('utf-8') @@ -157,13 +163,13 @@ def _connect(self, client_config): channel = grpc.secure_channel("{}:{}".format(host, str(port)), credentials) elif os.getenv("FEDN_GRPC_ROOT_CERT_PATH"): secure = True - print("CLIENT: using root certificate from environment variable for GRPC channel") + logger.info("Using root certificate from environment variable for GRPC channel.") with open(os.environ["FEDN_GRPC_ROOT_CERT_PATH"], 'rb') as f: credentials = grpc.ssl_channel_credentials(f.read()) channel = grpc.secure_channel("{}:{}".format(host, str(port)), credentials) elif self.config['secure']: secure = True - print("CLIENT: using CA certificate for GRPC channel") + logger.info("Using CA certificate for GRPC channel.") cert = ssl.get_server_certificate((host, port)) credentials = grpc.ssl_channel_credentials(cert.encode('utf-8')) @@ -174,7 +180,7 @@ def _connect(self, client_config): else: channel = grpc.secure_channel("{}:{}".format(host, str(port)), credentials) else: - print("CLIENT: using insecure GRPC channel") + logger.info("Using insecure GRPC channel.") if port == 443: port = 80 channel = grpc.insecure_channel("{}:{}".format( @@ -187,13 +193,13 @@ def _connect(self, client_config): self.combinerStub = rpc.CombinerStub(channel) self.modelStub = rpc.ModelServiceStub(channel) - print("Client: {} connected {} to {}:{}".format(self.name, - "SECURED" if secure else "INSECURE", + logger.info("Successfully established {} connection to {}:{}".format(self.name, + "secure" if secure else "insecure", host, port), flush=True) - print("Client: Using {} compute package.".format( + logger.info("Using {} compute package.".format( client_config["package"])) def _disconnect(self): @@ -310,7 +316,7 @@ def _initialize_dispatcher(self, config): except KeyError: pass except Exception as e: - print(f"Caught exception: {type(e).__name__}") + logger.error(f"Caught exception: {type(e).__name__}") else: # TODO: Deprecate dispatch_config = {'entry_points': @@ -530,16 +536,6 @@ def _process_training_request(self, model_id): os.unlink(inpath) os.unlink(outpath) - # except FileNotFoundError as e: - # print("File not found.") - # except Exception as e: - # print("ERROR could not process training request due to error: {}".format( - # e)) - # print(type(e).__name__) - # print(inpath) - # print(outpath) - # updated_model_id = None - # meta = {'status': 'failed', 'error': str(e)} self.state = ClientState.idle @@ -740,9 +736,6 @@ def run(self): if self.state != old_state: logger.info("Client in {} state.".format(ClientStateToString(self.state))) if cnt > 5: - # logger.info("CLIENT active.") - # print("{}:CLIENT active".format( - # datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) cnt = 0 if not self._attached: logger.info("Detached from combiner.") diff --git a/fedn/fedn/network/clients/connect.py b/fedn/fedn/network/clients/connect.py index 2f8acfa8d..07329ee3a 100644 --- a/fedn/fedn/network/clients/connect.py +++ b/fedn/fedn/network/clients/connect.py @@ -8,6 +8,7 @@ import requests +from fedn.common.log_config import logger class Status(enum.Enum): """ Enum for representing the status of a client assignment.""" @@ -63,8 +64,7 @@ def __init__(self, host, port, token, name, remote_package, force_ssl=False, ver self.connect_string = "{}{}".format( self.prefix, self.host) - print("\n\nsetting the connection string to {}\n\n".format( - self.connect_string), flush=True) + logger.info("Setting connection string to {}.".format(self.connect_string)) def assign(self): """ diff --git a/fedn/fedn/network/combiner/connect.py b/fedn/fedn/network/combiner/connect.py index de705a56c..30446c35f 100644 --- a/fedn/fedn/network/combiner/connect.py +++ b/fedn/fedn/network/combiner/connect.py @@ -8,6 +8,7 @@ import requests +from fedn.common.log_config import logger class Status(enum.Enum): """ Enum for representing the status of a combiner announcement.""" @@ -83,8 +84,7 @@ def __init__(self, host, port, myhost, fqdn, myport, token, name, secure=False, self.connect_string = "{}{}".format( self.prefix, self.host) - print("\n\nsetting the connection string to {}\n\n".format( - self.connect_string), flush=True) + logger.info("Setting connection string to {}".format(self.connect_string)) def announce(self): """ diff --git a/fedn/fedn/utils/dispatcher.py b/fedn/fedn/utils/dispatcher.py index 652f700e5..97179551f 100644 --- a/fedn/fedn/utils/dispatcher.py +++ b/fedn/fedn/utils/dispatcher.py @@ -1,10 +1,7 @@ import platform -import logging from fedn.utils.process import run_process - -logger = logging.getLogger(__name__) - +from fedn.common.log_config import logger class Dispatcher: """ Dispatcher class for compute packages. diff --git a/fedn/fedn/utils/logger.py b/fedn/fedn/utils/logger.py deleted file mode 100644 index 563012996..000000000 --- a/fedn/fedn/utils/logger.py +++ /dev/null @@ -1,33 +0,0 @@ -import logging -import os - - -class Logger: - """ Logger class for Fedn. - - :param log_level: The log level. - :type log_level: int - :param to_file: The name of the file to log to. - :type to_file: str - :param file_path: The path to the log file. - :type file_path: str - """ - - def __init__(self, log_level=logging.DEBUG, to_file='', file_path=os.getcwd()): - """ Initialize the logger.""" - root = logging.getLogger() - root.setLevel(log_level) - - # sh = logging.StreamHandler(sys.stdout) - sh = logging.StreamHandler() - sh.setLevel(log_level) - log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' - formatter = logging.Formatter(log_format) - sh.setFormatter(formatter) - root.addHandler(sh) - - if to_file != '': - fh = logging.FileHandler(os.path.join( - file_path, '{}'.format(to_file))) - fh.setFormatter(logging.Formatter(log_format)) - root.addHandler(fh) diff --git a/fedn/fedn/utils/process.py b/fedn/fedn/utils/process.py index bd31f9441..95e6eaa63 100644 --- a/fedn/fedn/utils/process.py +++ b/fedn/fedn/utils/process.py @@ -1,7 +1,6 @@ -import logging import subprocess -logger = logging.getLogger() +from fedn.common.log_config import logger def run_process(args, cwd): @@ -25,7 +24,7 @@ def check_io(): while True: output = status.stdout.readline().decode() if output: - logger.log(logging.INFO, output) + logger.info(output) else: break