Skip to content

Commit

Permalink
refactor logging configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
sreekaroo committed Feb 5, 2024
1 parent ee7bb01 commit 95c16fd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
31 changes: 0 additions & 31 deletions bcipy/simulator/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
36 changes: 36 additions & 0 deletions bcipy/simulator/__main__.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -33,6 +66,9 @@

args = vars(parser.parse_args())

# setting up logging
configure_logger()

simulator: Simulator = SimulationFactoryV2.create(**args)

simulator.run()

0 comments on commit 95c16fd

Please sign in to comment.