Skip to content

[Ported] feat(sensor): add an opt-in persistent runtime log - #144

Merged
barisozbas merged 5 commits into
mainfrom
port/runtime-log
Sep 29, 2026
Merged

barisozbas merged 5 commits into
mainfrom
port/runtime-log

Conversation

@v314b0i

@v314b0i v314b0i commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Related issue: N/A

What changed? (stacked on L4, port/logger-parsers)

  1. feat: JsonRecordFormatter writes one JSON object per record:
    • timestamp, level, component, function, phase, sensor_version
    • exception_type (also taken from an exception passed as an argument)
    • message, and stack when a traceback is attached
  2. feat: enable_runtime_log(dir) / disable_runtime_log() attach rotating sensor_runtime_errors.jsonl (WARNING and above) and sensor_runtime_debug.jsonl (DEBUG/INFO, whatever the console level) files, each 1 MiB × 2.
    • If the files cannot be opened, one warning is printed and warnings and errors keep going to stderr.
    • A failing write is reported once instead of once per record.
  3. feat: --log-file enables it under --output-dir, next to diagnostics.jsonl. --log-file-content-free drops message and stack. The files are closed when the run ends.
  4. feat: opt-in --log-identity adds username/hostname. Both options require --log-file.
  5. docs: a README "Runtime log files" section with a privacy note, and a CONTRIBUTING reminder.

Why?
diagnostics.jsonl records how healthy a run was, using counts and fixed codes. When a source fails, there is currently no persisted record of what failed or where. The runtime log keeps that, off by default, with options to strip content or add identity when logs are collected centrally.

How did you test it?
New tests cover:

  • formatter fields, stack capture, content-free mode, and identity (including an unknown user)
  • the level split, rotation, and replacing earlier files on re-enable
  • the unwritable-directory fallback and write failure reported once
  • the CLI: files are written only with --log-file, content-free output, identity, and option validation

Every commit passes pytest tests/ -q and ruff check adr_sensor/.

Potential risks

  • With --log-file, message/stack can contain local paths and error text; this is documented, and --log-file-content-free removes them. Call sites never log prompts or tool content.
  • Off by default, so there is no change for existing users.

@v314b0i
v314b0i added this pull request to stack #141 September 27, 2026 22:24
barisozbas added a commit that referenced this pull request Sep 29, 2026
Combine the remaining logging stack from PRs #142, #143, and #144. Reconcile main's squashed resource-log rotation ancestry while preserving the complete runtime-log branch tree.
@barisozbas
barisozbas removed this pull request from stack #141 September 29, 2026 11:13
@v314b0i
v314b0i added this pull request to stack #148 September 29, 2026 12:26
Base automatically changed from port/logger-parsers to main September 29, 2026 13:38
Add sensor_log.JsonRecordFormatter, which renders a record as one JSON
object: timestamp, level, component (from the logger name), function,
phase, sensor_version and exception_type, plus the rendered message and
the stack of an attached exception. exception_type also covers the common
logger.warning("... %s", exc) form where the exception is an argument.

include_details=False drops message and stack so records carry no paths
or error text. Nothing uses the formatter yet.
…ogger

Add sensor_log.enable_runtime_log(directory) and disable_runtime_log().
When enabled, every sensor log record is also written as JSON lines:
WARNING and above to sensor_runtime_errors.jsonl, DEBUG and INFO to
sensor_runtime_debug.jsonl, independent of the console level. Each file
rotates at 1 MiB with two backups, like diagnostics.jsonl.

A runtime log never stops or floods a run:
- if the files cannot be opened, one warning is printed and warnings and
  errors keep going to stderr through the console handlers
- a failing write is reported once on stderr instead of once per record
- enabling again replaces the previous files
--log-file writes the run's log records to sensor_runtime_errors.jsonl
and sensor_runtime_debug.jsonl in the output directory (the same place as
diagnostics.jsonl), rotating at 1 MiB with two backups. It is off by
default. Records include the message and any traceback, which can contain
local file paths and error text; --log-file-content-free omits both and
keeps only level, component, function, phase, version and exception type.

The files are closed when the run finishes, so repeated in-process calls
do not keep writing to an earlier directory.
--log-identity adds the local username and hostname to each --log-file
record, so logs collected from many machines can be told apart without
relying on file location. It is off by default and requires --log-file.
Identity is looked up once per run; an unknown user or host is recorded
as null.
…cy options

Add a "Runtime log files" README section: file names, level split,
rotation, the unwritable-directory fallback, the record fields with an
example, and a privacy note covering --log-file-content-free and
--log-identity. Remind contributors in CONTRIBUTING.md that log messages
can be persisted.
@barisozbas
barisozbas merged commit 1c4fbe7 into main Sep 29, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants