diff --git a/platform/logging/layouts/JsonLayout.ts b/platform/logging/layouts/JsonLayout.ts index 663adf56746ff..64675a795cb24 100644 --- a/platform/logging/layouts/JsonLayout.ts +++ b/platform/logging/layouts/JsonLayout.ts @@ -19,14 +19,26 @@ export type JsonLayoutConfigType = typeof schemaType; export class JsonLayout implements Layout { static createConfigSchema = createSchema; - format({ timestamp, level, context, message, error, meta }: LogRecord): string { + format(record: LogRecord): string { return JSON.stringify({ - '@timestamp': timestamp.toISOString(), - level: level.id.toUpperCase(), - context, - message, - error: error && error.message, - meta + '@timestamp': record.timestamp.toISOString(), + level: record.level.id.toUpperCase(), + context: record.context, + message: record.message, + error: JsonLayout.errorToSerializableObject(record.error), + meta: record.meta }); } + + private static errorToSerializableObject(error: Error | undefined) { + if (error === undefined) { + return error; + } + + return { + name: error.name, + stack: error.stack, + message: error.message + }; + } } diff --git a/platform/logging/layouts/PatternLayout.ts b/platform/logging/layouts/PatternLayout.ts index c9f7bdb005c99..e84cfefbe2cd1 100644 --- a/platform/logging/layouts/PatternLayout.ts +++ b/platform/logging/layouts/PatternLayout.ts @@ -70,11 +70,13 @@ export class PatternLayout implements Layout { * @param record Instance of `LogRecord` to format into string. */ format(record: LogRecord): string { + // Error stack is much more useful than just the message. + const message = (record.error && record.error.stack) || record.message; const formattedRecord = new Map([ [Parameters.Timestamp, record.timestamp.toISOString()], [Parameters.Level, record.level.id.toUpperCase().padEnd(5)], [Parameters.Context, record.context], - [Parameters.Message, record.message] + [Parameters.Message, message] ]); if (this.highlight) { diff --git a/platform/logging/layouts/__tests__/JsonLayout.test.ts b/platform/logging/layouts/__tests__/JsonLayout.test.ts index 4434085829268..d5e674ebfd241 100644 --- a/platform/logging/layouts/__tests__/JsonLayout.test.ts +++ b/platform/logging/layouts/__tests__/JsonLayout.test.ts @@ -9,7 +9,11 @@ const records: LogRecord[] = [ timestamp: new Date(2012, 1, 1), message: 'message-1', context: 'context-1', - error: new Error('Some error message'), + error: { + message: 'Some error message', + name: 'Some error name', + stack: 'Some error stack' + }, level: LogLevel.Fatal }, { diff --git a/platform/logging/layouts/__tests__/PatternLayout.test.ts b/platform/logging/layouts/__tests__/PatternLayout.test.ts index 0da8d888a5c0a..1011c0316838f 100644 --- a/platform/logging/layouts/__tests__/PatternLayout.test.ts +++ b/platform/logging/layouts/__tests__/PatternLayout.test.ts @@ -9,7 +9,11 @@ const records: LogRecord[] = [ timestamp: new Date(2012, 1, 1), message: 'message-1', context: 'context-1', - error: new Error('Some error message'), + error: { + message: 'Some error message', + name: 'Some error name', + stack: 'Some error stack' + }, level: LogLevel.Fatal }, { @@ -84,8 +88,7 @@ test('`format()` correctly formats record with custom pattern.', () => { const layout = new PatternLayout('mock-{message}-{context}-{message}'); for (const record of records) { - const { context, message } = record; - expect(layout.format(record)).toBe(`mock-${message}-${context}-${message}`); + expect(layout.format(record)).toMatchSnapshot(); } }); diff --git a/platform/logging/layouts/__tests__/__snapshots__/JsonLayout.test.ts.snap b/platform/logging/layouts/__tests__/__snapshots__/JsonLayout.test.ts.snap index 00f3808fa6058..2d4e0e4c0e3ce 100644 --- a/platform/logging/layouts/__tests__/__snapshots__/JsonLayout.test.ts.snap +++ b/platform/logging/layouts/__tests__/__snapshots__/JsonLayout.test.ts.snap @@ -1,13 +1,13 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`\`format()\` correctly formats record. 1`] = `"{\\"timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"message\\":\\"message-1\\",\\"context\\":\\"context-1\\",\\"error\\":\\"Some error message\\",\\"level\\":{\\"id\\":\\"fatal\\",\\"value\\":2}}"`; +exports[`\`format()\` correctly formats record. 1`] = `"{\\"@timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"level\\":\\"FATAL\\",\\"context\\":\\"context-1\\",\\"message\\":\\"message-1\\",\\"error\\":{\\"name\\":\\"Some error name\\",\\"stack\\":\\"Some error stack\\",\\"message\\":\\"Some error message\\"}}"`; -exports[`\`format()\` correctly formats record. 2`] = `"{\\"timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"message\\":\\"message-2\\",\\"context\\":\\"context-2\\",\\"level\\":{\\"id\\":\\"error\\",\\"value\\":3}}"`; +exports[`\`format()\` correctly formats record. 2`] = `"{\\"@timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"level\\":\\"ERROR\\",\\"context\\":\\"context-2\\",\\"message\\":\\"message-2\\"}"`; -exports[`\`format()\` correctly formats record. 3`] = `"{\\"timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"message\\":\\"message-3\\",\\"context\\":\\"context-3\\",\\"level\\":{\\"id\\":\\"warn\\",\\"value\\":4}}"`; +exports[`\`format()\` correctly formats record. 3`] = `"{\\"@timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"level\\":\\"WARN\\",\\"context\\":\\"context-3\\",\\"message\\":\\"message-3\\"}"`; -exports[`\`format()\` correctly formats record. 4`] = `"{\\"timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"message\\":\\"message-4\\",\\"context\\":\\"context-4\\",\\"level\\":{\\"id\\":\\"debug\\",\\"value\\":6}}"`; +exports[`\`format()\` correctly formats record. 4`] = `"{\\"@timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"level\\":\\"DEBUG\\",\\"context\\":\\"context-4\\",\\"message\\":\\"message-4\\"}"`; -exports[`\`format()\` correctly formats record. 5`] = `"{\\"timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"message\\":\\"message-5\\",\\"context\\":\\"context-5\\",\\"level\\":{\\"id\\":\\"info\\",\\"value\\":5}}"`; +exports[`\`format()\` correctly formats record. 5`] = `"{\\"@timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"level\\":\\"INFO\\",\\"context\\":\\"context-5\\",\\"message\\":\\"message-5\\"}"`; -exports[`\`format()\` correctly formats record. 6`] = `"{\\"timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"message\\":\\"message-6\\",\\"context\\":\\"context-6\\",\\"level\\":{\\"id\\":\\"trace\\",\\"value\\":7}}"`; +exports[`\`format()\` correctly formats record. 6`] = `"{\\"@timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"level\\":\\"TRACE\\",\\"context\\":\\"context-6\\",\\"message\\":\\"message-6\\"}"`; diff --git a/platform/logging/layouts/__tests__/__snapshots__/PatternLayout.test.ts.snap b/platform/logging/layouts/__tests__/__snapshots__/PatternLayout.test.ts.snap index 0f883a4977bd2..636d4640d7c4d 100644 --- a/platform/logging/layouts/__tests__/__snapshots__/PatternLayout.test.ts.snap +++ b/platform/logging/layouts/__tests__/__snapshots__/PatternLayout.test.ts.snap @@ -1,6 +1,18 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`\`format()\` correctly formats record with full pattern. 1`] = `"[2012-01-31T23:00:00.000Z][FATAL][context-1] message-1"`; +exports[`\`format()\` correctly formats record with custom pattern. 1`] = `"mock-Some error stack-context-1-Some error stack"`; + +exports[`\`format()\` correctly formats record with custom pattern. 2`] = `"mock-message-2-context-2-message-2"`; + +exports[`\`format()\` correctly formats record with custom pattern. 3`] = `"mock-message-3-context-3-message-3"`; + +exports[`\`format()\` correctly formats record with custom pattern. 4`] = `"mock-message-4-context-4-message-4"`; + +exports[`\`format()\` correctly formats record with custom pattern. 5`] = `"mock-message-5-context-5-message-5"`; + +exports[`\`format()\` correctly formats record with custom pattern. 6`] = `"mock-message-6-context-6-message-6"`; + +exports[`\`format()\` correctly formats record with full pattern. 1`] = `"[2012-01-31T23:00:00.000Z][FATAL][context-1] Some error stack"`; exports[`\`format()\` correctly formats record with full pattern. 2`] = `"[2012-01-31T23:00:00.000Z][ERROR][context-2] message-2"`; @@ -12,7 +24,7 @@ exports[`\`format()\` correctly formats record with full pattern. 5`] = `"[2012- exports[`\`format()\` correctly formats record with full pattern. 6`] = `"[2012-01-31T23:00:00.000Z][TRACE][context-6] message-6"`; -exports[`\`format()\` correctly formats record with highlighting. 1`] = `"[2012-01-31T23:00:00.000Z][FATAL][context-1] message-1"`; +exports[`\`format()\` correctly formats record with highlighting. 1`] = `"[2012-01-31T23:00:00.000Z][FATAL][context-1] Some error stack"`; exports[`\`format()\` correctly formats record with highlighting. 2`] = `"[2012-01-31T23:00:00.000Z][ERROR][context-2] message-2"`;