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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import pino, { LoggerOptions as PinoLoggerOptions } from 'pino'
import pinoms, { Streams } from 'pino-multi-stream'
import { createWriteStream } from 'pino-sentry'
import { NodeOptions } from '@sentry/node'
export const logLevels = [
'trace',
'debug',
'info',
'warn',
'error',
'fatal',
] as const
export type LogLevel = (typeof logLevels)[number]
export interface LoggerOptions {
name: string
level?: LogLevel
sentryOptions?: NodeOptions
streams?: Streams
}
/**
* Temporary wrapper class to maintain earlier module interface.
*/
export class Logger {
options: LoggerOptions
inner: pino.Logger
constructor(options: LoggerOptions) {
this.options = options
const loggerOptions: PinoLoggerOptions = {
name: options.name,
level: options.level || 'debug',
// Remove pid and hostname considering production runs inside docker
base: null,
}
let loggerStreams: Streams = [{ stream: process.stdout }]
if (options.sentryOptions) {
loggerStreams.push({
level: 'error',
stream: createWriteStream({
...options.sentryOptions,
stackAttributeKey: 'err',
}),
})
}
if (options.streams) {
loggerStreams = loggerStreams.concat(options.streams)
}
this.inner = pino(loggerOptions, pinoms.multistream(loggerStreams))
}
child(bindings: pino.Bindings): Logger {
const inner = this.inner.child(bindings)
const logger = new Logger(this.options)
logger.inner = inner
return logger
}
trace(msg: string, o?: object, ...args: any[]): void {
if (o) {
this.inner.trace(o, msg, ...args)
} else {
this.inner.trace(msg, ...args)
}
}
debug(msg: string, o?: object, ...args: any[]): void {
if (o) {
this.inner.debug(o, msg, ...args)
} else {
this.inner.debug(msg, ...args)
}
}
info(msg: string, o?: object, ...args: any[]): void {
if (o) {
this.inner.info(o, msg, ...args)
} else {
this.inner.info(msg, ...args)
}
}
warn(msg: string, o?: object, ...args: any[]): void {
if (o) {
this.inner.warn(o, msg, ...args)
} else {
this.inner.warn(msg, ...args)
}
}
warning(msg: string, o?: object, ...args: any[]): void {
if (o) {
this.inner.warn(o, msg, ...args)
} else {
this.inner.warn(msg, ...args)
}
}
error(msg: string, o?: object, ...args: any[]): void {
if (o) {
// Formatting error log for Sentry
const context = {
extra: { ...o },
}
this.inner.error(context, msg, ...args)
} else {
this.inner.error(msg, ...args)
}
}
fatal(msg: string, o?: object, ...args: any[]): void {
if (o) {
const context = {
extra: { ...o },
}
this.inner.fatal(context, msg, ...args)
} else {
this.inner.fatal(msg, ...args)
}
}
}