From 95c16fd3a5789e57bc2da2721a889a3d1b7fad0c Mon Sep 17 00:00:00 2001 From: sreekaroo Date: Sun, 4 Feb 2024 20:11:15 -0500 Subject: [PATCH] refactor logging configuration --- bcipy/simulator/__init__.py | 31 ------------------------------- bcipy/simulator/__main__.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/bcipy/simulator/__init__.py b/bcipy/simulator/__init__.py index 27ca39387..b3cc41c2d 100644 --- a/bcipy/simulator/__init__.py +++ b/bcipy/simulator/__init__.py @@ -1,32 +1 @@ """ Simulator package. View README """ - -import datetime -import logging -import sys - -log = logging.getLogger(__name__) -log.setLevel(logging.DEBUG) -# Create handlers for logging to the standard output and a file -stdoutHandler = logging.StreamHandler(stream=sys.stdout) -fileHandler = logging.FileHandler(f"bcipy/simulator/sim_output_{datetime.datetime.now()}.log") - -# Set the log levels on the handlers -stdoutHandler.setLevel(logging.INFO) -fileHandler.setLevel(logging.DEBUG) - -# Create a log format using Log Record attributes -fmt_file = logging.Formatter( - "%(levelname)s | %(filename)s:%(lineno)s >> %(message)s" -) - -fmt = logging.Formatter( - "%(levelname)s >> %(message)s" -) - -# Set the log format on each handler -stdoutHandler.setFormatter(fmt) -fileHandler.setFormatter(fmt_file) - -# Add each handler to the Logger object -log.addHandler(stdoutHandler) -log.addHandler(fileHandler) diff --git a/bcipy/simulator/__main__.py b/bcipy/simulator/__main__.py index 826275c2b..488528de5 100644 --- a/bcipy/simulator/__main__.py +++ b/bcipy/simulator/__main__.py @@ -1,11 +1,44 @@ """ Entry point to run Simulator """ import argparse +import datetime +import logging +import sys from pathlib import Path from bcipy.simulator.sim_factory import SimulationFactoryV2 from bcipy.simulator.simulator_base import Simulator + +def configure_logger(): + """ configures logger for standard out nad file output """ + + log = logging.getLogger(None) # configuring root logger + log.setLevel(logging.DEBUG) + # Create handlers for logging to the standard output and a file + stdoutHandler = logging.StreamHandler(stream=sys.stdout) + file_name = datetime.datetime.now().strftime("%m-%d-%H:%M") + output_path = "bcipy/simulator/generated" + fileHandler = logging.FileHandler(f"{output_path}/{file_name}.log") + + # Set the log levels on the handlers + stdoutHandler.setLevel(logging.INFO) + fileHandler.setLevel(logging.DEBUG) + + # Create a log format using Log Record attributes + fmt_file = logging.Formatter("%(levelname)s | %(filename)s >> %(message)s") + + fmt = logging.Formatter("%(message)s") + + # Set the log format on each handler + stdoutHandler.setFormatter(fmt) + fileHandler.setFormatter(fmt_file) + + # Add each handler to the Logger object + log.addHandler(stdoutHandler) + log.addHandler(fileHandler) + + if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( @@ -33,6 +66,9 @@ args = vars(parser.parse_args()) + # setting up logging + configure_logger() + simulator: Simulator = SimulationFactoryV2.create(**args) simulator.run()