Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

patch: Preserve server logs across restarts (#1940)

Ali Khokhar0ed2cde3b21a
diff --git a/src/free_claude_code/config/logging_config.py b/src/free_claude_code/config/logging_config.py
index d07880e6..7f0f95ef 100644
--- a/src/free_claude_code/config/logging_config.py
+++ b/src/free_claude_code/config/logging_config.py
@@ -1,7 +1,7 @@
"""Loguru-based structured logging configuration.
Structured logs are written as JSON lines to a configurable path (default
-``logs/server.log``). Stdlib logging is intercepted and funneled to loguru.
+``~/.fcc/logs/server.log``). Stdlib logging is intercepted and funneled to loguru.
Context vars (request_id, node_id, chat_id) from contextualize() are
included at top level for easy grep/filter.
"""
@@ -150,7 +150,7 @@ def configure_logging(
Idempotent: skips if already configured with the same path, level, and verbosity.
On path or level change, replaces only the file sink without truncating.
On verbosity change alone, updates only the third-party logger levels.
- Use force=True to reconfigure from scratch.
+ Use force=True to rebuild handlers while preserving existing log records.
When ``verbose_third_party`` is false, managed noisy third-party loggers
are capped at WARNING unless explicitly configured otherwise.
@@ -174,8 +174,6 @@ def configure_logging(
logger.remove()
- log_path.write_text("")
-
_sink_id = _add_file_sink(log_path, level)
intercept = InterceptHandler()
diff --git a/tests/config/test_logging_config.py b/tests/config/test_logging_config.py
index 8ea8729c..e00c2321 100644
--- a/tests/config/test_logging_config.py
+++ b/tests/config/test_logging_config.py
@@ -2,6 +2,8 @@
import json
import logging
+import subprocess
+import sys
from pathlib import Path
from unittest.mock import patch
@@ -22,12 +24,49 @@ def test_log_capture_does_not_feed_back_into_interception(caplog, capsys, monkey
def test_configure_logging_creates_parent_directories(tmp_path) -> None:
- """Nested log path: parent directories are created before truncating."""
+ """Nested log path: parent directories are created before opening."""
log_file = tmp_path / "nested" / "dir" / "app.log"
configure_logging(str(log_file), force=True)
assert log_file.is_file()
+def test_logging_preserves_records_across_process_restarts(tmp_path):
+ log_file = tmp_path / "server.log"
+ script = """
+import sys
+from loguru import logger
+from free_claude_code.config.logging_config import configure_logging
+configure_logging(sys.argv[1])
+logger.info(sys.argv[2])
+logger.complete()
+logger.remove()
+"""
+ for message in ("first run", "second run"):
+ subprocess.run(
+ [sys.executable, "-c", script, str(log_file), message],
+ check=True,
+ capture_output=True,
+ text=True,
+ timeout=10,
+ )
+ records = [json.loads(line) for line in log_file.read_text().splitlines()]
+ assert [record["message"] for record in records] == ["first run", "second run"]
+
+
+def test_forced_logging_reconfiguration_preserves_records(tmp_path):
+ log_file = tmp_path / "server.log"
+ configure_logging(log_file, force=True)
+ logger.info("before reconfiguration")
+ configure_logging(log_file, force=True)
+ logger.info("after reconfiguration")
+ logger.complete()
+ records = [json.loads(line) for line in log_file.read_text().splitlines()]
+ assert [record["message"] for record in records] == [
+ "before reconfiguration",
+ "after reconfiguration",
+ ]
+
+
def test_configure_logging_writes_json_to_file(tmp_path):
"""configure_logging writes JSON lines to the specified file."""
log_file = str(tmp_path / "test.log")