-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
json_layout.ts
60 lines (52 loc) · 1.58 KB
/
json_layout.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import moment from 'moment-timezone';
import { merge } from '@kbn/std';
import { schema } from '@kbn/config-schema';
import { LogRecord, Layout } from '@kbn/logging';
const { literal, object } = schema;
const jsonLayoutSchema = object({
type: literal('json'),
});
/** @internal */
export interface JsonLayoutConfigType {
type: 'json';
}
/**
* Layout that just converts `LogRecord` into JSON string.
* @internal
*/
export class JsonLayout implements Layout {
public static configSchema = jsonLayoutSchema;
private static errorToSerializableObject(error: Error | undefined) {
if (error === undefined) {
return error;
}
return {
message: error.message,
type: error.name,
stack_trace: error.stack,
};
}
public format(record: LogRecord): string {
const log = {
'@timestamp': moment(record.timestamp).format('YYYY-MM-DDTHH:mm:ss.SSSZ'),
message: record.message,
error: JsonLayout.errorToSerializableObject(record.error),
log: {
level: record.level.id.toUpperCase(),
logger: record.context,
},
process: {
pid: record.pid,
},
};
const output = record.meta ? merge(log, record.meta) : log;
return JSON.stringify(output);
}
}