Skip to content

Commit

Permalink
Print error.stack for pattern layout and error.message, `error.st…
Browse files Browse the repository at this point in the history
…ack` and `error.name` for json layout. (#13309)
  • Loading branch information
azasypkin authored Aug 3, 2017
1 parent cb222d7 commit 432e6c1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 20 deletions.
26 changes: 19 additions & 7 deletions platform/logging/layouts/JsonLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
}
4 changes: 3 additions & 1 deletion platform/logging/layouts/PatternLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion platform/logging/layouts/__tests__/JsonLayout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
{
Expand Down
9 changes: 6 additions & 3 deletions platform/logging/layouts/__tests__/PatternLayout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
{
Expand Down Expand Up @@ -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();
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -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\\"}"`;
Original file line number Diff line number Diff line change
@@ -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"`;

Expand All @@ -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][[31mFATAL[39m][[35mcontext-1[39m] message-1"`;
exports[`\`format()\` correctly formats record with highlighting. 1`] = `"[2012-01-31T23:00:00.000Z][[31mFATAL[39m][[35mcontext-1[39m] Some error stack"`;

exports[`\`format()\` correctly formats record with highlighting. 2`] = `"[2012-01-31T23:00:00.000Z][ERROR][context-2] message-2"`;

Expand Down

0 comments on commit 432e6c1

Please sign in to comment.