feat(logger): add opt-in multi-line log alignment#94
Conversation
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
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThe PR adds an 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/utils.ts (1)
6-6: 🧹 Nitpick | 🔵 Trivial | ⚡ Quick winPrefer standard escape sequence notation.
Consider using
\x1binstead ofString.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
⛔ Files ignored due to path filters (1)
tests/__snapshots__/logger.test.ts.snapis excluded by!**/*.snap
📒 Files selected for processing (5)
preview/index.tssrc/createLogger.tssrc/types.tssrc/utils.tstests/logger.test.ts
chenjiahan
left a comment
There was a problem hiding this comment.
Sorry I missed this, could you resolve the conflicts?
Resolved |
|
Thank you! |
feat(logger): add opt-in multi-line log alignment
Add
alignMultilineoption tocreateLogger(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
alignMultiline?: booleantoOptions.stripAnsi; use it inisErrorStackMessageso the stack regex still matches ANSI-grayed lines.
LABEL_WIDTHconstant; aftertext 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.
snapshots are untouched.
Expected output
Without prefix:
With prefix
[web]:Error stacks stay top-aligned:
Closes #51