Product case study · November 18, 2026
Ask Claude Code why one log message prints twice
Reproduce duplicate Python logs, ask Claude Code for a scoped repair, and verify repeated setup without removing another component's handler.

Fix the handler setup before deduplicating log text. Two identical lines can be one event sent twice, or two real events you need to keep.
Your script prints “job started” twice. Before asking an agent to remove repeated lines, check whether the job actually ran twice. A logging helper that installs another handler every time it runs can make one event look like several.
This lab gives Claude Code a small repair with a useful constraint: stop the extra output without deleting someone else’s handler. Deduplicating message text would hide the symptom and could erase real events. Fixing setup gives you a much better explanation of what happened.
Use Bash on Linux, macOS or WSL, Python 3, and an installed, signed-in Claude Code. All messages below are fictional. The Python baseline and reference repair were tested locally; the agent prompt is an authored exercise, not a recorded Claude session. Official sources were checked November 18, 2026.
Reproduce the extra handler
Create an empty practice folder:
log_lab=$(mktemp -d /tmp/claude-log-handlers.XXXXXX)
cd "$log_lab"
cat > log_setup.py <<'PY'
import logging
def configure(stream):
logger = logging.getLogger("superbash_log_lab")
logger.setLevel(logging.INFO)
logger.propagate = False
handler = logging.StreamHandler(stream)
handler.setFormatter(logging.Formatter("%(message)s"))
logger.addHandler(handler)
return logger
PY
The logger has a fixed name. Calling configure() again gets the same logger, then adds a new handler to it. Python’s logging cookbook explains that logger identity persists across calls and modules within the same interpreter process.
We set propagate = False deliberately: this exercise uses a dedicated logger with its own destination. It isolates repeated setup from the separate problem of a record reaching handlers on both a child and its ancestors.
Save this as test_log_setup.py in the same folder:
import io
import logging
import unittest
from log_setup import configure
class LoggingSetupTests(unittest.TestCase):
def setUp(self):
self.logger = logging.getLogger("superbash_log_lab")
self.clear_handlers()
self.logger.disabled = False
def tearDown(self):
self.clear_handlers()
def clear_handlers(self):
for handler in self.logger.handlers[:]:
self.logger.removeHandler(handler)
handler.close()
def test_repeated_setup_emits_once(self):
output = io.StringIO()
for _ in range(3):
logger = configure(output)
logger.info("job started")
self.assertEqual(output.getvalue(), "job started\n")
def test_latest_stream_receives_future_events(self):
old, new = io.StringIO(), io.StringIO()
configure(old).info("before")
configure(new).info("after")
self.assertEqual(old.getvalue(), "before\n")
self.assertEqual(new.getvalue(), "after\n")
def test_external_handler_survives(self):
external, output = io.StringIO(), io.StringIO()
handler = logging.StreamHandler(external)
self.logger.addHandler(handler)
configure(output).info("job started")
self.assertIn(handler, self.logger.handlers)
self.assertEqual(external.getvalue(), "job started\n")
self.assertEqual(output.getvalue(), "job started\n")
def test_two_real_events_remain_two_lines(self):
output = io.StringIO()
logger = configure(output)
logger.info("heartbeat")
logger.info("heartbeat")
self.assertEqual(output.getvalue(), "heartbeat\nheartbeat\n")
if __name__ == "__main__":
unittest.main()
Run:
python3 -m unittest -v test_log_setup.py
The baseline fails two of four tests and exits with status 1. Repeated setup prints three copies of one message. Changing streams leaves the old handler attached, so the old destination also receives after. The external-handler and two-real-events tests already pass; keep them to protect behavior the repair must preserve.
The test cleanup removes all handlers only from this disposable lab logger. That convenience is not the required behavior of the application helper.
Give Claude a contract it cannot solve by hiding output
Start claude in the practice folder and confirm the working directory and active tool permissions. Paste:
Read log_setup.py and test_log_setup.py. Run:
python3 -m unittest -v test_log_setup.py
Explain the failing tests, then edit only log_setup.py.
configure(stream) must keep exactly one handler owned by this helper.
Repeated sequential calls must not multiply output. A later call changes
that owned handler's stream, so future events go only to the latest stream.
Preserve handlers installed by other components, including their identity.
Keep INFO level, message-only formatting, and propagate=False on this
specific dedicated logger. Preserve two real events with identical text.
Do not change tests, alter the root logger, install packages, access external
services, commit, or delegate. This exercise does not require concurrent setup.
Run the same tests again and report failures and the actual exit status.
If you cannot run commands, state that clearly.
Anthropic’s debugging workflow recommends giving Claude the reproduction command and describing when the failure occurs. Here, “on repeated setup” matters more than the wording of the log message. The prompt describes the task boundary; it does not replace the session’s permission controls.
Review a repair that owns only its handler
One reference implementation is:
import logging
_HANDLER_NAME = "superbash_log_lab.console"
def configure(stream):
logger = logging.getLogger("superbash_log_lab")
logger.setLevel(logging.INFO)
logger.propagate = False
handler = next(
(item for item in logger.handlers
if item.get_name() == _HANDLER_NAME),
None,
)
if handler is None:
handler = logging.StreamHandler(stream)
handler.set_name(_HANDLER_NAME)
logger.addHandler(handler)
else:
handler.setStream(stream)
handler.setFormatter(logging.Formatter("%(message)s"))
return logger
The name is this application’s ownership convention: reserve it for this helper. First setup installs the handler; subsequent calls reuse it and update its stream. An unrelated handler stays attached. This reference passed all four tests locally with exit status 0.
Exit Claude, inspect the file, and rerun the tests yourself. A green result is evidence for these sequential cases, not proof of thread-safe configuration or a successful production rollout. Configure logging during startup; if multiple threads can call this helper simultaneously, the find-or-create sequence needs a separate concurrency design and test.
This repair assumes a fresh process starts with the corrected helper. It does not identify unnamed handlers left behind by the broken version in a live interpreter. Restart the disposable lab process before checking it. For a long-running application, inspect who owns existing handlers before removing any.
Spot the tempting wrong fixes
Clearing every handler makes the duplicate disappear but removes the external destination too. The third test catches that shortcut. Checking whether any handler exists also fails that test: another component’s handler is not a substitute for this helper’s output.
Filtering identical messages breaks the heartbeat test. Two calls are two events even when their text matches. I would keep that regression test in a real repair; it prevents a quiet log from being mistaken for a correct log.
Changing propagation everywhere answers a different question. If duplicates remain in an existing application, inspect handlers on the emitting logger and its ancestors. Python’s logging reference describes how propagation can deliver a record to multiple handlers. Many applications intentionally configure the root logger and let module loggers propagate to it. Do not copy this lab’s dedicated-logger policy into every module.
If Python cannot import log_setup, run the test from the folder containing both files. If the tests pass but your real application still duplicates messages, add a reproduction of its startup calls and logger hierarchy before accepting another edit. The missing evidence is which handler delivered each line, not whether the final terminal output looks tidy.
Sources
Checked November 18, 2026:
- Python: Logging reference — logger identity, handler management, names, and propagation.
- Python: Logging cookbook — shared named loggers across modules and application configuration patterns.
- Python: StreamHandler — changing a handler’s stream.
- Anthropic: Common workflows — reproducible debugging and checking changes.
Put this to work
How to separate repeated events from repeated delivery of one log record.
Try
Repair one logging helper in a disposable folder with Claude Code.
Prove it worked
Run four fixed tests before and after the edit, then inspect handler ownership.
Keep in view
- Reproduce handlers accumulating on a named Python logger.
- Give Claude Code an explicit rule for repeated setup and stream changes.
- Preserve real repeated events and another component's output.