-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce logging
JsonLayout
. (#13180)
- Loading branch information
Showing
9 changed files
with
204 additions
and
51 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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,32 @@ | ||
import { Schema, typeOfSchema } from '../../types'; | ||
import { LogRecord } from '../LogRecord'; | ||
import { Layout } from './Layouts'; | ||
|
||
const createSchema = ({ literal, object }: Schema) => { | ||
return object({ | ||
kind: literal('json') | ||
}); | ||
}; | ||
|
||
const schemaType = typeOfSchema(createSchema); | ||
/** @internal */ | ||
export type JsonLayoutConfigType = typeof schemaType; | ||
|
||
/** | ||
* Layout that just converts `LogRecord` into JSON string. | ||
* @internal | ||
*/ | ||
export class JsonLayout implements Layout { | ||
static createConfigSchema = createSchema; | ||
|
||
format({ timestamp, level, context, message, error, meta }: LogRecord): string { | ||
return JSON.stringify({ | ||
'@timestamp': timestamp.toISOString(), | ||
level: level.id.toUpperCase(), | ||
context, | ||
message, | ||
error: error && error.message, | ||
meta | ||
}); | ||
} | ||
} |
This file contains 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
This file contains 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,59 @@ | ||
import * as mockSchema from '../../../lib/schema'; | ||
|
||
import { LogLevel } from '../../LogLevel'; | ||
import { LogRecord } from '../../LogRecord'; | ||
import { JsonLayout } from '../JsonLayout'; | ||
|
||
const records: LogRecord[] = [ | ||
{ | ||
timestamp: new Date(2012, 1, 1), | ||
message: 'message-1', | ||
context: 'context-1', | ||
error: new Error('Some error message'), | ||
level: LogLevel.Fatal | ||
}, | ||
{ | ||
timestamp: new Date(2012, 1, 1), | ||
message: 'message-2', | ||
context: 'context-2', | ||
level: LogLevel.Error | ||
}, | ||
{ | ||
timestamp: new Date(2012, 1, 1), | ||
message: 'message-3', | ||
context: 'context-3', | ||
level: LogLevel.Warn | ||
}, | ||
{ | ||
timestamp: new Date(2012, 1, 1), | ||
message: 'message-4', | ||
context: 'context-4', | ||
level: LogLevel.Debug | ||
}, | ||
{ | ||
timestamp: new Date(2012, 1, 1), | ||
message: 'message-5', | ||
context: 'context-5', | ||
level: LogLevel.Info | ||
}, | ||
{ | ||
timestamp: new Date(2012, 1, 1), | ||
message: 'message-6', | ||
context: 'context-6', | ||
level: LogLevel.Trace | ||
} | ||
]; | ||
|
||
test('`createConfigSchema()` creates correct schema.', () => { | ||
const layoutSchema = JsonLayout.createConfigSchema(mockSchema); | ||
|
||
expect(layoutSchema.validate({ kind: 'json' })).toEqual({ kind: 'json' }); | ||
}); | ||
|
||
test('`format()` correctly formats record.', () => { | ||
const layout = new JsonLayout(); | ||
|
||
for (const record of records) { | ||
expect(layout.format(record)).toMatchSnapshot(); | ||
} | ||
}); |
This file contains 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
This file contains 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
13 changes: 13 additions & 0 deletions
13
platform/logging/layouts/__tests__/__snapshots__/JsonLayout.test.ts.snap
This file contains 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,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. 2`] = `"{\\"timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"message\\":\\"message-2\\",\\"context\\":\\"context-2\\",\\"level\\":{\\"id\\":\\"error\\",\\"value\\":3}}"`; | ||
|
||
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. 4`] = `"{\\"timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"message\\":\\"message-4\\",\\"context\\":\\"context-4\\",\\"level\\":{\\"id\\":\\"debug\\",\\"value\\":6}}"`; | ||
|
||
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. 6`] = `"{\\"timestamp\\":\\"2012-01-31T23:00:00.000Z\\",\\"message\\":\\"message-6\\",\\"context\\":\\"context-6\\",\\"level\\":{\\"id\\":\\"trace\\",\\"value\\":7}}"`; |
Oops, something went wrong.