Create a timestamped run dir and attach a file handler. Returns the run dir.
(log_level_str: str)
| 452 | |
| 453 | |
| 454 | def _attach_run_log(log_level_str: str) -> str: |
| 455 | """Create a timestamped run dir and attach a file handler. Returns the run dir.""" |
| 456 | import datetime |
| 457 | |
| 458 | log_level = getattr(logging, log_level_str.upper()) |
| 459 | timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") |
| 460 | run_dir = os.path.join(_LOGS_ROOT, timestamp) |
| 461 | os.makedirs(run_dir, exist_ok=True) |
| 462 | log_path = os.path.join(run_dir, "run.log") |
| 463 | |
| 464 | file_handler = logging.FileHandler(log_path) |
| 465 | file_handler.setLevel(log_level) |
| 466 | file_handler.setFormatter(logging.Formatter(_LOG_FMT, datefmt=_LOG_DATEFMT)) |
| 467 | logging.getLogger("explainshell").addHandler(file_handler) |
| 468 | |
| 469 | # Logged after the file handler is attached so the run log captures it. |
| 470 | logger.info("command line: %s", " ".join(sys.argv)) |
| 471 | logger.info("logging to %s", log_path) |
| 472 | return run_dir |
| 473 | |
| 474 | |
| 475 | def _ensure_run_dir(ctx: click.Context) -> str: |