diff --git a/src/dippy/dippy.py b/src/dippy/dippy.py index eb069da..c08624f 100644 --- a/src/dippy/dippy.py +++ b/src/dippy/dippy.py @@ -20,6 +20,7 @@ import sys from pathlib import Path +from dippy.core.analyzer import analyze from dippy.core.config import ( Config, ConfigError, @@ -29,8 +30,6 @@ match_after_mcp, match_mcp, ) -from dippy.core.analyzer import analyze - # === Mode Detection === @@ -86,17 +85,23 @@ def _get_log_file() -> Path: return Path.home() / ".claude" / "hook-approvals.log" +_LOG_FORMAT = "%(asctime)s [%(levelname)s] %(message)s" +_LOG_DATEFMT = "%Y-%m-%d %H:%M:%S" + + def setup_logging(): - """Configure logging to file. Fails silently if unable to write.""" + """Add a file handler for the detected mode's log directory. + + Called after mode is finalized so the log file goes to the correct + directory (~/.claude/, ~/.cursor/, etc.). Fails silently. + """ try: log_file = _get_log_file() log_file.parent.mkdir(parents=True, exist_ok=True) - logging.basicConfig( - filename=str(log_file), - level=logging.INFO, - format="%(asctime)s [%(levelname)s] %(message)s", - datefmt="%Y-%m-%d %H:%M:%S", - ) + handler = logging.FileHandler(str(log_file)) + handler.setLevel(logging.INFO) + handler.setFormatter(logging.Formatter(_LOG_FORMAT, datefmt=_LOG_DATEFMT)) + logging.getLogger().addHandler(handler) except (OSError, PermissionError): pass # Logging is optional - don't crash if we can't write @@ -304,7 +309,8 @@ def main(): print("Run 'dippy --help' for usage.") raise SystemExit(0) - setup_logging() + # Stderr fallback so early errors (bad JSON, unknown tool) are visible + logging.basicConfig(level=logging.INFO, format=_LOG_FORMAT, datefmt=_LOG_DATEFMT) try: # Read hook input from stdin @@ -313,6 +319,11 @@ def main(): # Auto-detect mode from input if no explicit flag/env was set if _EXPLICIT_MODE is None: MODE = _detect_mode_from_input(input_data) + + # Add file handler now that mode (and thus log directory) is known + setup_logging() + + if _EXPLICIT_MODE is None: logging.info(f"Auto-detected mode: {MODE}") # Extract cwd from input