Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/dippy/dippy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import sys
from pathlib import Path

from dippy.core.analyzer import analyze
from dippy.core.config import (
Config,
ConfigError,
Expand All @@ -29,8 +30,6 @@
match_after_mcp,
match_mcp,
)
from dippy.core.analyzer import analyze


# === Mode Detection ===

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading