-
Notifications
You must be signed in to change notification settings - Fork 31
feat(service-logs): format JSON object messages #3012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ce43db2
feat(service-logs): format JSON object messages
RemiBonnet 3ba648e
fix(service-logs): preserve structured log content
RemiBonnet 585a826
feat(service-logs): accent structured log keys
RemiBonnet 7ec1797
perf(service-logs): scan highlight ranges linearly
RemiBonnet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...logs/feature/src/lib/list-service-logs/row-service-logs/format-object-log-message.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { findHighlightRanges, formatObjectLogMessage } from './format-object-log-message' | ||
|
|
||
| describe('formatObjectLogMessage', () => { | ||
| it('formats a JSON object with two-space indentation', () => { | ||
| expect(formatObjectLogMessage('{"message":"Started","context":{"attempt":2},"ports":[80,443]}').message).toBe(`{ | ||
| "message": "Started", | ||
| "context": { | ||
| "attempt": 2 | ||
| }, | ||
| "ports": [ | ||
| 80, | ||
| 443 | ||
| ] | ||
| }`) | ||
| }) | ||
|
|
||
| it('preserves numeric literals and duplicate keys', () => { | ||
| expect(formatObjectLogMessage('{"id":9007199254740993,"decimal":42.0,"exponent":1e3,"key":1,"key":2}').message) | ||
| .toBe(`{ | ||
| "id": 9007199254740993, | ||
| "decimal": 42.0, | ||
| "exponent": 1e3, | ||
| "key": 1, | ||
| "key": 2 | ||
| }`) | ||
| }) | ||
|
|
||
| it('preserves whitespace and escaped characters inside strings', () => { | ||
| expect(formatObjectLogMessage('{"message":"keep spaces, braces { } and \\"quotes\\""}').message).toBe(`{ | ||
| "message": "keep spaces, braces { } and \\"quotes\\"" | ||
| }`) | ||
| }) | ||
|
|
||
| it('returns the formatted ranges for object keys', () => { | ||
| const formattedLogMessage = formatObjectLogMessage('{"message":"Started","context":{"attempt":2}}') | ||
|
|
||
| expect( | ||
| formattedLogMessage.keyRanges?.map(({ start, end }) => formattedLogMessage.message.slice(start, end)) | ||
| ).toEqual(['"message"', '"context"', '"attempt"']) | ||
| }) | ||
|
|
||
| it.each([ | ||
| ['invalid JSON', '{"message":"Started"'], | ||
| ['an array', '["Started"]'], | ||
| ['a string', '"Started"'], | ||
| ['a number', '42'], | ||
| ['a boolean', 'true'], | ||
| ['null', 'null'], | ||
| ['plain text', 'Started successfully'], | ||
| ])('keeps %s unchanged', (_description, message) => { | ||
| expect(formatObjectLogMessage(message)).toEqual({ message }) | ||
| }) | ||
|
|
||
| it('maps a raw compact match to its formatted range', () => { | ||
| const rawMessage = '{"message":"Started","attempt":2}' | ||
| const formattedLogMessage = formatObjectLogMessage(rawMessage) | ||
| const ranges = findHighlightRanges(rawMessage, formattedLogMessage, '"message":"Started"') | ||
|
|
||
| expect(ranges).toEqual([{ start: 4, end: 24 }]) | ||
| expect(formattedLogMessage.message.slice(ranges[0]?.start, ranges[0]?.end)).toBe('"message": "Started"') | ||
| }) | ||
|
|
||
| it('finds case-insensitive matches in plain-text messages', () => { | ||
| const rawMessage = 'Started successfully' | ||
|
|
||
| expect(findHighlightRanges(rawMessage, formatObjectLogMessage(rawMessage), 'started')).toEqual([ | ||
| { start: 0, end: 7 }, | ||
| ]) | ||
| }) | ||
| }) |
147 changes: 147 additions & 0 deletions
147
...vice-logs/feature/src/lib/list-service-logs/row-service-logs/format-object-log-message.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| export interface HighlightRange { | ||
| start: number | ||
| end: number | ||
| } | ||
|
|
||
| export interface FormattedLogMessage { | ||
| message: string | ||
| sourceMap?: number[] | ||
| keyRanges?: HighlightRange[] | ||
| } | ||
|
|
||
| function nextNonWhitespaceCharacter(message: string, start: number) { | ||
| for (let index = start; index < message.length; index++) { | ||
| if (!/\s/.test(message[index] ?? '')) return message[index] | ||
| } | ||
|
|
||
| return undefined | ||
| } | ||
|
|
||
| function previousNonWhitespaceCharacter(message: string, start: number) { | ||
| for (let index = start; index >= 0; index--) { | ||
| if (!/\s/.test(message[index] ?? '')) return message[index] | ||
| } | ||
|
|
||
| return undefined | ||
| } | ||
|
|
||
| export function formatObjectLogMessage(message: string): FormattedLogMessage { | ||
| try { | ||
| const parsedMessage: unknown = JSON.parse(message) | ||
|
|
||
| if (typeof parsedMessage !== 'object' || parsedMessage === null || Array.isArray(parsedMessage)) { | ||
| return { message } | ||
| } | ||
| } catch { | ||
| return { message } | ||
| } | ||
|
|
||
| let formattedMessage = '' | ||
| let indentation = 0 | ||
| let isInsideString = false | ||
| let isEscaped = false | ||
| let currentStringStart = 0 | ||
| const sourceMap = Array.from<number>({ length: message.length }).fill(-1) | ||
| const keyRanges: HighlightRange[] = [] | ||
|
|
||
| const appendSourceCharacter = (character: string, sourceIndex: number) => { | ||
| sourceMap[sourceIndex] = formattedMessage.length | ||
| formattedMessage += character | ||
| } | ||
|
|
||
| const appendNewLine = () => { | ||
| formattedMessage += `\n${' '.repeat(indentation)}` | ||
| } | ||
|
|
||
| for (let index = 0; index < message.length; index++) { | ||
| const character = message[index] ?? '' | ||
|
|
||
| if (isInsideString) { | ||
| appendSourceCharacter(character, index) | ||
|
|
||
| if (isEscaped) { | ||
| isEscaped = false | ||
| } else if (character === '\\') { | ||
| isEscaped = true | ||
| } else if (character === '"') { | ||
| isInsideString = false | ||
| if (nextNonWhitespaceCharacter(message, index + 1) === ':') { | ||
| keyRanges.push({ start: currentStringStart, end: formattedMessage.length }) | ||
| } | ||
| } | ||
|
|
||
| continue | ||
| } | ||
|
|
||
| if (/\s/.test(character)) continue | ||
|
|
||
| if (character === '"') { | ||
| isInsideString = true | ||
| currentStringStart = formattedMessage.length | ||
| appendSourceCharacter(character, index) | ||
| continue | ||
| } | ||
|
|
||
| if (character === '{' || character === '[') { | ||
| appendSourceCharacter(character, index) | ||
|
|
||
| const closingCharacter = character === '{' ? '}' : ']' | ||
| if (nextNonWhitespaceCharacter(message, index + 1) !== closingCharacter) { | ||
| indentation++ | ||
| appendNewLine() | ||
| } | ||
| continue | ||
| } | ||
|
|
||
| if (character === '}' || character === ']') { | ||
| const openingCharacter = character === '}' ? '{' : '[' | ||
| if (previousNonWhitespaceCharacter(message, index - 1) !== openingCharacter) { | ||
| indentation-- | ||
| appendNewLine() | ||
| } | ||
| appendSourceCharacter(character, index) | ||
| continue | ||
| } | ||
|
|
||
| appendSourceCharacter(character, index) | ||
|
|
||
| if (character === ',') { | ||
| appendNewLine() | ||
| } else if (character === ':') { | ||
| formattedMessage += ' ' | ||
| } | ||
| } | ||
|
|
||
| return { message: formattedMessage, sourceMap, keyRanges } | ||
| } | ||
|
|
||
| export function findHighlightRanges( | ||
| rawMessage: string, | ||
| { message, sourceMap }: FormattedLogMessage, | ||
| searchTerm: string | null | undefined | ||
| ): HighlightRange[] { | ||
| if (!searchTerm) return [] | ||
|
|
||
| const ranges: HighlightRange[] = [] | ||
| const normalizedSearchTerm = searchTerm.toLowerCase() | ||
| const searchableMessage = sourceMap ? rawMessage : message | ||
| const normalizedMessage = searchableMessage.toLowerCase() | ||
| let matchIndex = normalizedMessage.indexOf(normalizedSearchTerm) | ||
|
|
||
| while (matchIndex >= 0) { | ||
| if (!sourceMap) { | ||
| ranges.push({ start: matchIndex, end: matchIndex + searchTerm.length }) | ||
| } else { | ||
| const matchEnd = matchIndex + searchTerm.length | ||
| const mappedOffsets = sourceMap.slice(matchIndex, matchEnd).filter((offset) => offset >= 0) | ||
|
|
||
| if (mappedOffsets.length > 0) { | ||
| ranges.push({ start: mappedOffsets[0] ?? 0, end: (mappedOffsets[mappedOffsets.length - 1] ?? 0) + 1 }) | ||
| } | ||
| } | ||
|
|
||
| matchIndex = normalizedMessage.indexOf(normalizedSearchTerm, matchIndex + searchTerm.length) | ||
| } | ||
|
|
||
| return ranges | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Unicode case folding can make
matchIndexno longer indexrawMessage, so searches after characters such asİhighlight the wrong rendered text. Use an index-preserving case-insensitive matcher or retain a mapping from normalized offsets back to original offsets.Prompt for AI agents