Skip to content

Preserve context in normalized JSON logs#12

Merged
AbaSheger merged 1 commit into
AbaSheger:mainfrom
Aayush10016:codex/preserve-json-log-context
Jul 5, 2026
Merged

Preserve context in normalized JSON logs#12
AbaSheger merged 1 commit into
AbaSheger:mainfrom
Aayush10016:codex/preserve-json-log-context

Conversation

@Aayush10016

@Aayush10016 Aayush10016 commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Summary

  • preserve timestamp and level when structured JSON logs are normalized
  • keep message and stack_trace in the normalized output
  • return the original JSON line when none of those useful fields are present
  • add focused tests for complete, partial, and unrecognized structured logs

Root cause

LogProcessor.JsonLogStructure only mapped message and stack_trace, so Jackson discarded timestamp and severity context before classification. The normalizer also returned an empty string when a valid JSON object contained none of the mapped fields.

Technical solution

The JSON record now maps timestamp and level. A shared field appender emits each nonblank field on its own line, preserving all available context without introducing empty lines. If no useful fields are extracted, the original input is returned unchanged.

Tests

  • mvn "-Dmaven.repo.local=<workspace>/.cache/maven" test
  • Result: 91 tests passed, 0 failures, 0 errors, 0 skipped

Closes #11

Summary by CodeRabbit

  • Bug Fixes

    • Improved JSON log text conversion to include more useful fields like timestamp and level, while skipping empty values.
    • Added a fallback so the original log entry is preserved when no meaningful fields are available.
  • Tests

    • Added coverage for plain-text conversion, including mixed field presence and the fallback behavior.

@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1ccd0a53-cd0f-4e4d-9964-cf26888160f4

📥 Commits

Reviewing files that changed from the base of the PR and between 38716fb and 3e4aafb.

📒 Files selected for processing (2)
  • src/main/java/com/stacklens/JSONlogs/LogProcessor.java
  • src/test/java/com/stacklens/JSONlogs/LogProcessorTest.java

📝 Walkthrough

Walkthrough

LogProcessor.parseToPlainText was updated to append timestamp, level, message, and stack_trace fields via a new appendField helper, falling back to the original log line when no fields are appended. JsonLogStructure record was expanded with timestamp and level fields. New unit tests validate the behavior.

Changes

JSON Log Normalization

Layer / File(s) Summary
JsonLogStructure schema and append helper
src/main/java/com/stacklens/JSONlogs/LogProcessor.java
Record gains timestamp and level components with @JsonProperty mappings; a new appendField helper skips null/blank values and manages newline separators.
parseToPlainText field appending and fallback
src/main/java/com/stacklens/JSONlogs/LogProcessor.java
parseToPlainText appends timestamp, level, message, and stack trace via the helper, returning the original logLine when no fields were appended instead of an empty string.
Test coverage
src/test/java/com/stacklens/JSONlogs/LogProcessorTest.java
New tests assert structured field preservation, skipping of blank/missing fields, and passthrough for JSON with no useful fields.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant LogProcessor
  participant appendField
  Caller->>LogProcessor: parseToPlainText(logLine)
  LogProcessor->>appendField: append timestamp
  LogProcessor->>appendField: append level
  LogProcessor->>appendField: append message
  LogProcessor->>appendField: append stackTrace
  alt builder empty
    LogProcessor-->>Caller: return original logLine
  else builder non-empty
    LogProcessor-->>Caller: return built plain text
  end
Loading

Possibly related PRs

  • AbaSheger/stacklens#10: Introduced the original LogProcessor.parseToPlainText and JsonLogStructure that this PR directly extends.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: preserving context in normalized JSON logs.
Description check ✅ Passed The description covers summary, root cause, solution, and testing, with only non-critical template sections omitted.
Linked Issues check ✅ Passed The changes match issue #11 by preserving timestamp and level, keeping message and stack trace, falling back to the original line, and adding tests.
Out of Scope Changes check ✅ Passed The diff appears focused on JSON log normalization and tests, with no clear out-of-scope code changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Aayush10016 Aayush10016 changed the title [codex] Preserve context in normalized JSON logs Preserve context in normalized JSON logs Jul 4, 2026
@Aayush10016 Aayush10016 marked this pull request as ready for review July 5, 2026 08:33
@AbaSheger

Copy link
Copy Markdown
Owner

Reviewed and approved for merge.

All requirements from #11 are fully addressed:

  • timestamp and level are now preserved in normalized JSON log output
  • message and stack_trace continue to work as before
  • Fallback to original JSON line when no useful fields are present
  • 3 focused unit tests covering all scenarios

Clean implementation with the appendField helper eliminating the repeated null checks. 91 tests passing, no regressions. Good work!

@AbaSheger AbaSheger merged commit 289b33e into AbaSheger:main Jul 5, 2026
3 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.

Preserve timestamp and level when normalizing JSON logs

2 participants