|
25 | 25 | from ohmo.session_storage import OhmoSessionBackend |
26 | 26 | from ohmo.workspace import ( |
27 | 27 | get_gateway_config_path, |
| 28 | + get_logs_dir, |
28 | 29 | get_workspace_root, |
29 | 30 | get_soul_path, |
30 | 31 | get_state_path, |
@@ -407,18 +408,42 @@ def _maybe_restart_gateway(*, cwd: str | Path, workspace: str | Path) -> None: |
407 | 408 | print(f"ohmo gateway restarted (pid={pid})") |
408 | 409 |
|
409 | 410 |
|
410 | | -def _configure_gateway_logging(workspace: str | Path | None = None) -> None: |
| 411 | +def _configure_gateway_logging( |
| 412 | + workspace: str | Path | None = None, |
| 413 | + *, |
| 414 | + console: bool = True, |
| 415 | + log_file: bool = True, |
| 416 | +) -> None: |
411 | 417 | """Configure foreground gateway logging.""" |
412 | 418 | config = load_gateway_config(workspace) |
413 | 419 | level_name = str(config.log_level or "INFO").upper() |
414 | 420 | level = getattr(logging, level_name, logging.INFO) |
| 421 | + handlers = _build_gateway_logging_handlers(workspace, console=console, log_file=log_file) |
415 | 422 | logging.basicConfig( |
416 | 423 | level=level, |
417 | 424 | format="%(asctime)s [%(name)s] %(levelname)s %(message)s", |
| 425 | + handlers=handlers, |
418 | 426 | force=True, |
419 | 427 | ) |
420 | 428 |
|
421 | 429 |
|
| 430 | +def _build_gateway_logging_handlers( |
| 431 | + workspace: str | Path | None = None, |
| 432 | + *, |
| 433 | + console: bool, |
| 434 | + log_file: bool, |
| 435 | +) -> list[logging.Handler]: |
| 436 | + """Build gateway log handlers for foreground and daemon modes.""" |
| 437 | + handlers: list[logging.Handler] = [] |
| 438 | + if console: |
| 439 | + handlers.append(logging.StreamHandler()) |
| 440 | + if log_file: |
| 441 | + log_path = get_logs_dir(workspace) / "gateway.log" |
| 442 | + log_path.parent.mkdir(parents=True, exist_ok=True) |
| 443 | + handlers.append(logging.FileHandler(log_path, encoding="utf-8", delay=True)) |
| 444 | + return handlers |
| 445 | + |
| 446 | + |
422 | 447 | @app.callback(invoke_without_command=True) |
423 | 448 | def main( |
424 | 449 | ctx: typer.Context, |
@@ -637,9 +662,11 @@ def user_edit_cmd( |
637 | 662 | def gateway_run_cmd( |
638 | 663 | cwd: str = typer.Option(str(Path.cwd()), "--cwd", help="Project working directory"), |
639 | 664 | workspace: str | None = typer.Option(None, "--workspace", help=_WORKSPACE_HELP), |
| 665 | + console_log: bool = typer.Option(True, "--console-log/--no-console-log", hidden=True), |
| 666 | + log_file: bool = typer.Option(True, "--log-file/--no-log-file", hidden=True), |
640 | 667 | ) -> None: |
641 | 668 | """Run the ohmo gateway in the foreground.""" |
642 | | - _configure_gateway_logging(workspace) |
| 669 | + _configure_gateway_logging(workspace, console=console_log, log_file=log_file) |
643 | 670 | service = OhmoGatewayService(cwd, workspace) |
644 | 671 | raise SystemExit(asyncio.run(service.run_foreground())) |
645 | 672 |
|
|
0 commit comments