Skip to content

Commit

Permalink
Issue #106: Log to file option
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark2000 committed Dec 23, 2023
1 parent 2cbf7c7 commit 6e973ea
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/bsk_rl/envs/general_satellite_tasking/gym_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(
time_limit: float = float("inf"),
terminate_on_time_limit: bool = False,
log_level: Union[int, str] = logging.WARNING,
log_dir: Optional[str] = None,
render_mode=None,
) -> None:
"""A Gymnasium environment adaptable to a wide range satellite tasking problems
Expand Down Expand Up @@ -82,10 +83,11 @@ def __init__(
terminate_on_time_limit: Send terminations signal time_limit instead of just
truncation.
log_level: Logging level for the environment. Default is WARNING.
log_dir: Directory to write logs to in addition to the console.
render_mode: Unused.
"""
self.seed = None
self._configure_logging(log_level)
self._configure_logging(log_level, log_dir)
if isinstance(satellites, Satellite):
satellites = [satellites]
self.satellites = satellites
Expand Down Expand Up @@ -113,7 +115,7 @@ def __init__(
self.latest_step_duration = 0
self.render_mode = render_mode

def _configure_logging(self, log_level):
def _configure_logging(self, log_level, log_dir=None):
if isinstance(log_level, str):
log_level = log_level.upper()
logger = logging.getLogger("bsk_rl.envs.general_satellite_tasking")
Expand All @@ -129,7 +131,7 @@ def _configure_logging(self, log_level):
warn_new_env = True

ch = logging.StreamHandler()
ch.setFormatter(logging_config.SimFormatter())
ch.setFormatter(logging_config.SimFormatter(color_output=True))
ch.addFilter(logging_config.ContextFilter(env=self, proc_id=os.getpid()))
logger.addHandler(ch)
if warn_new_env:
Expand All @@ -138,6 +140,12 @@ def _configure_logging(self, log_level):
"Old environments in process may now log times incorrectly."
)

if log_dir is not None:
fh = logging.FileHandler(log_dir)
fh.setFormatter(logging_config.SimFormatter(color_output=False))
fh.addFilter(logging_config.ContextFilter(env=self, proc_id=os.getpid()))
logger.addHandler(fh)

def _generate_env_args(self) -> None:
"""Instantiate env_args from any randomizers in provided env_args."""
self.env_args = {
Expand Down

0 comments on commit 6e973ea

Please sign in to comment.