Skip to content

Commit 523aa07

Browse files
committed
fix: implement persistent alert logging and reduce log noise
1 parent 56ccecf commit 523aa07

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

deploy/dashboard/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949

5050
BLOCK_LOG_PATHS = [
5151
"/var/log/clawedr.log",
52-
"/tmp/clawedr_log_tailer.log",
5352
os.path.expanduser("~/Library/Logs/clawedr.log"),
53+
"/tmp/clawedr_log_tailer.log",
5454
]
5555

5656
_BLOCK_LINE_RE = re.compile(
@@ -83,7 +83,7 @@ def _find_openclaw() -> Optional[str]:
8383
return None
8484

8585

86-
def _parse_log_lines(max_lines: int = 200) -> list[dict]:
86+
def _parse_log_lines(max_lines: int = 1000) -> list[dict]:
8787
"""Parse recent BLOCKED entries from the log files."""
8888
alerts: list[dict] = []
8989
for log_path in BLOCK_LOG_PATHS:

deploy/install.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ install_macos() {
199199
mkdir -p "$CLAWEDR_DIR/dashboard/templates"
200200
mkdir -p "/etc/clawedr"
201201
chmod 777 "/etc/clawedr" || true
202+
203+
# Initialize persistent log file with wide permissions
204+
touch "/var/log/clawedr.log"
205+
chmod 666 "/var/log/clawedr.log" || true
206+
202207
cp "$tmpdir/clawedr.sb" "$CLAWEDR_DIR/"
203208
cp "$tmpdir/log_tailer.py" "$CLAWEDR_DIR/"
204209
cp "$tmpdir/apply_macos_policy.py" "$CLAWEDR_DIR/"
@@ -257,6 +262,11 @@ install_linux() {
257262
mkdir -p "$CLAWEDR_DIR/dashboard/templates"
258263
mkdir -p "/etc/clawedr"
259264
chmod 777 "/etc/clawedr" || true
265+
266+
# Initialize persistent log file with wide permissions
267+
touch "/var/log/clawedr.log"
268+
chmod 666 "/var/log/clawedr.log" || true
269+
260270
cp "$tmpdir/compiled_policy.json" "$CLAWEDR_DIR/"
261271
cp "$tmpdir/bpf_hooks.c" "$CLAWEDR_DIR/"
262272
cp "$tmpdir/monitor.py" "$CLAWEDR_DIR/"

deploy/linux/monitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def _print_event(cpu, data, size):
201201
# This is when deny_rules must run — at enter, cmdline was still the shell.
202202
matched_rule = _check_deny_rules(ns_pid, comm, filename)
203203
if not matched_rule:
204-
logger.info(
204+
logger.debug(
205205
"[observed] pid=%d uid=%d comm=%s file=%s",
206206
ns_pid, event.uid, comm, filename,
207207
)

deploy/macos/log_tailer.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from shared.user_rules import get_custom_rules, USER_RULES_PATH
2828

2929
logger = logging.getLogger("clawedr.log_tailer")
30+
block_logger = logging.getLogger("clawedr.blocked")
3031

3132
# Subsystem for macOS Console.app filtering (e.g. predicate: subsystem == "com.clawedr.shield")
3233
OSLOG_SUBSYSTEM = "com.clawedr.shield"
@@ -39,6 +40,7 @@
3940
"CLAWEDR_SB_PATH", "/usr/local/share/clawedr/clawedr.sb"
4041
)
4142
POLL_INTERVAL = int(os.environ.get("CLAWEDR_POLL_INTERVAL", "10"))
43+
BLOCK_LOG_FILE = "/var/log/clawedr.log"
4244

4345
# Regex to extract blocked path/process from sandbox violation log lines
4446
_DENY_RE = re.compile(r"deny\(?\d*\)?\s+([\w\-\*]+)\s+(.*)", re.IGNORECASE)
@@ -114,7 +116,7 @@ def tail_sandbox_log():
114116
if len(seen_events) > 5000:
115117
seen_events.clear()
116118

117-
logger.info("SANDBOX EVENT: %s", line)
119+
logger.debug("SANDBOX EVENT: %s", line)
118120

119121
# Try to extract what was denied and dispatch an alert
120122
match = _DENY_RE.search(line)
@@ -150,7 +152,7 @@ def tail_sandbox_log():
150152
continue
151153

152154
# Log the blocked event in the format expected by the dashboard
153-
logger.warning("BLOCKED [%s] action=%s target=%s", rule_id, action, target)
155+
block_logger.warning("BLOCKED [%s] action=%s target=%s", rule_id, action, target)
154156

155157
dispatch_alert_async(
156158
rule_id=rule_id,
@@ -226,6 +228,17 @@ def _configure_logging() -> None:
226228
stream.setFormatter(fmt)
227229
root.addHandler(stream)
228230

231+
# Dedicated alert logging to /var/log/clawedr.log (persistent)
232+
block_logger.setLevel(logging.WARNING)
233+
block_logger.propagate = False
234+
block_logger.addHandler(stream) # Also log to stderr for visibility in /tmp log
235+
try:
236+
block_fh = logging.FileHandler(BLOCK_LOG_FILE)
237+
block_fh.setFormatter(fmt)
238+
block_logger.addHandler(block_fh)
239+
except PermissionError:
240+
logger.warning("Cannot write to %s — block file logging disabled", BLOCK_LOG_FILE)
241+
229242
try:
230243
import pyoslog
231244
if pyoslog.is_supported():

0 commit comments

Comments
 (0)