Skip to content

feat(logger): add opt-in multi-line log alignment#94

Merged
chenjiahan merged 2 commits into
rstackjs:mainfrom
a145789:feat/multiline-align
Jul 15, 2026
Merged

feat(logger): add opt-in multi-line log alignment#94
chenjiahan merged 2 commits into
rstackjs:mainfrom
a145789:feat/multiline-align

Conversation

@a145789

@a145789 a145789 commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

feat(logger): add opt-in multi-line log alignment

Add alignMultiline option to createLogger (default: false,
non-breaking). When enabled, continuation lines of multi-line
messages are indented to align with the first line's content
column (label + prefix), so multi-line output reads as a
contiguous block instead of wrapping back to column 0.

Changes

  • src/types.ts: add alignMultiline?: boolean to Options.
  • src/utils.ts: add stripAnsi; use it in isErrorStackMessage
    so the stack regex still matches ANSI-grayed lines.
  • src/createLogger.ts: extract LABEL_WIDTH constant; after
    text is assembled, indent continuation lines to
    LABEL_WIDTH + 1 + (prefix length + 1). Skip the first line,
    blank lines, and stack-like lines (only at level === 'error').
    Error objects are exempt entirely.
  • tests: add new tests for the feature only; existing tests and
    snapshots are untouched.

Expected output

Without prefix:

info    User profile updated:
        - Name: John Doe
        - Email: john@example.com

With prefix [web]:

info    [web] User profile updated:
              - Name: John Doe
              - Email: john@example.com

Error stacks stay top-aligned:

error   Something failed:
        - reason A
    at /rslog/foo/bar.js:29:0
        - reason B

Closes #51

Add `alignMultiline` option to `createLogger` (default: false,
non-breaking). When enabled, continuation lines of multi-line
messages are indented to align with the first line's content
column (label + prefix), so multi-line output reads as a
contiguous block instead of wrapping back to column 0.

## Changes

- src/types.ts: add `alignMultiline?: boolean` to `Options`.
- src/utils.ts: add `stripAnsi`; use it in `isErrorStackMessage`
  so the stack regex still matches ANSI-grayed lines.
- src/createLogger.ts: extract `LABEL_WIDTH` constant; after
  text is assembled, indent continuation lines to
  `LABEL_WIDTH + 1 + (prefix length + 1)`. Skip the first line,
  blank lines, and stack-like lines (only at `level === 'error'`).
  Error objects are exempt entirely.
- tests: add new tests for the feature only; existing tests and
  snapshots are untouched.

## Expected output

Without prefix:

  info    User profile updated:
          - Name: John Doe
          - Email: john@example.com

With prefix `[web]`:

  info    [web] User profile updated:
                - Name: John Doe
                - Email: john@example.com

Error stacks stay top-aligned:

  error   Something failed:
          - reason A
      at /rslog/foo/bar.js:29:0
          - reason B

Closes rstackjs#51
@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e9fe137b-cbdd-4afa-bdd2-d81ccc5f4629

📥 Commits

Reviewing files that changed from the base of the PR and between 3df572a and a53084d.

📒 Files selected for processing (3)
  • src/createLogger.ts
  • src/utils.ts
  • tests/logger.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/logger.test.ts
  • src/createLogger.ts

📝 Walkthrough

Walkthrough

The PR adds an alignMultiline option to the logger. A new alignMultiline?: boolean property is added to the Options interface. A stripAnsi helper is introduced in src/utils.ts, and isErrorStackMessage is updated to strip ANSI codes before pattern matching. In createLogger, the options destructuring is expanded to include alignMultiline; label tracking is refactored with hasLabel and isErrorObject flags. When alignMultiline is enabled and a non-Error message contains newlines, subsequent non-empty lines are indented by LABEL_WIDTH plus the ANSI-stripped prefix length, except error-stack lines at error level which remain top-aligned. Tests and a preview demo are added to cover all alignment scenarios.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: opt-in multi-line log alignment.
Description check ✅ Passed The description matches the code changes and explains the new alignMultiline behavior accurately.
Linked Issues check ✅ Passed The PR implements #51 by adding alignMultiline, indenting continuation lines, handling error stacks, and adding tests.
Out of Scope Changes check ✅ Passed The changes stay focused on multiline alignment support and related test/util updates, with no clear unrelated scope creep.
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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/utils.ts (1)

6-6: 🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Prefer standard escape sequence notation.

Consider using \x1b instead of String.fromCharCode(27) for the ESC character—it's more idiomatic and immediately recognizable.

♻️ Suggested refactor
-const ANSI_REGEXP = new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, 'g');
+const ANSI_REGEXP = /\x1b\[[0-9;]*m/g;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/utils.ts` at line 6, The ANSI_REGEXP constant uses
String.fromCharCode(27) to represent the ESC character, which is less idiomatic
than the standard escape sequence notation. Replace the String.fromCharCode(27)
call in the ANSI_REGEXP definition with the \x1b escape sequence, which is more
immediately recognizable and follows JavaScript conventions for representing
control characters.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/utils.ts`:
- Line 6: The ANSI_REGEXP constant uses String.fromCharCode(27) to represent the
ESC character, which is less idiomatic than the standard escape sequence
notation. Replace the String.fromCharCode(27) call in the ANSI_REGEXP definition
with the \x1b escape sequence, which is more immediately recognizable and
follows JavaScript conventions for representing control characters.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 95d06d21-d4c9-4da2-aeb6-153813037fc4

📥 Commits

Reviewing files that changed from the base of the PR and between 25dc20d and 3df572a.

⛔ Files ignored due to path filters (1)
  • tests/__snapshots__/logger.test.ts.snap is excluded by !**/*.snap
📒 Files selected for processing (5)
  • preview/index.ts
  • src/createLogger.ts
  • src/types.ts
  • src/utils.ts
  • tests/logger.test.ts

@chenjiahan chenjiahan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I missed this, could you resolve the conflicts?

@a145789

a145789 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

Sorry I missed this, could you resolve the conflicts?

Resolved

@chenjiahan
chenjiahan merged commit c049767 into rstackjs:main Jul 15, 2026
1 check passed
@chenjiahan

Copy link
Copy Markdown
Member

Thank you!

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.

[Feature Request] Add multi-line log alignment support for better readability

2 participants