-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathwinston.ts
More file actions
177 lines (162 loc) · 4.79 KB
/
winston.ts
File metadata and controls
177 lines (162 loc) · 4.79 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* eslint-disable @typescript-eslint/ban-ts-comment */
import type { LogSeverityLevel } from '@sentry/core';
import { captureLog } from '../logs/capture';
const DEFAULT_CAPTURED_LEVELS: Array<LogSeverityLevel> = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];
// See: https://github.com/winstonjs/triple-beam
const LEVEL_SYMBOL = Symbol.for('level');
const MESSAGE_SYMBOL = Symbol.for('message');
const SPLAT_SYMBOL = Symbol.for('splat');
/**
* Options for the Sentry Winston transport.
*/
interface WinstonTransportOptions {
/**
* Use this option to filter which levels should be captured. By default, all levels are captured.
*
* @example
* ```ts
* const SentryWinstonTransport = Sentry.createSentryWinstonTransport(Transport, {
* // Only capture error and warn logs
* levels: ['error', 'warn'],
* });
* ```
*/
levels?: Array<LogSeverityLevel>;
/**
* Use this option to map custom levels to Sentry log severity levels.
*
* @example
* ```ts
* const SentryWinstonTransport = Sentry.createSentryWinstonTransport(Transport, {
* customLevelMap: {
* myCustomLevel: 'info',
* customError: 'error',
* },
* });
* ```
*/
customLevelMap?: Record<string, LogSeverityLevel>;
}
/**
* Creates a new Sentry Winston transport that fowards logs to Sentry. Requires the `enableLogs` option to be enabled.
*
* Supports Winston 3.x.x.
*
* @param TransportClass - The Winston transport class to extend.
* @returns The extended transport class.
*
* @example
* ```ts
* const winston = require('winston');
* const Transport = require('winston-transport');
*
* const SentryWinstonTransport = Sentry.createSentryWinstonTransport(Transport);
*
* const logger = winston.createLogger({
* transports: [new SentryWinstonTransport()],
* });
* ```
*/
export function createSentryWinstonTransport<TransportStreamInstance extends object>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
TransportClass: new (options?: any) => TransportStreamInstance,
sentryWinstonOptions?: WinstonTransportOptions,
): typeof TransportClass {
// @ts-ignore - We know this is safe because SentryWinstonTransport extends TransportClass
class SentryWinstonTransport extends TransportClass {
private _levels: Set<LogSeverityLevel>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public constructor(options?: any) {
super(options);
this._levels = new Set(sentryWinstonOptions?.levels ?? DEFAULT_CAPTURED_LEVELS);
}
/**
* Forwards a winston log to the Sentry SDK.
*/
public log(info: unknown, callback: () => void): void {
try {
setImmediate(() => {
// @ts-ignore - We know this is safe because SentryWinstonTransport extends TransportClass
this.emit('logged', info);
});
if (!isObject(info)) {
return;
}
const levelFromSymbol = info[LEVEL_SYMBOL];
// See: https://github.com/winstonjs/winston?tab=readme-ov-file#streams-objectmode-and-info-objects
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { level, message, timestamp, ...attributes } = info;
// Remove all symbols from the remaining attributes
attributes[LEVEL_SYMBOL] = undefined;
attributes[MESSAGE_SYMBOL] = undefined;
attributes[SPLAT_SYMBOL] = undefined;
const customLevel = sentryWinstonOptions?.customLevelMap?.[levelFromSymbol as string];
const logSeverityLevel =
customLevel ?? WINSTON_LEVEL_TO_LOG_SEVERITY_LEVEL_MAP[levelFromSymbol as string] ?? 'info';
if (this._levels.has(logSeverityLevel)) {
captureLog(logSeverityLevel, message as string, {
...attributes,
'sentry.origin': 'auto.log.winston',
});
}
} catch {
// do nothing
}
if (callback) {
callback();
}
}
}
return SentryWinstonTransport as typeof TransportClass;
}
function isObject(anything: unknown): anything is Record<string | symbol, unknown> {
return typeof anything === 'object' && anything != null;
}
// npm
// {
// error: 0,
// warn: 1,
// info: 2,
// http: 3,
// verbose: 4,
// debug: 5,
// silly: 6
// }
//
// syslog
// {
// emerg: 0,
// alert: 1,
// crit: 2,
// error: 3,
// warning: 4,
// notice: 5,
// info: 6,
// debug: 7,
// }
const WINSTON_LEVEL_TO_LOG_SEVERITY_LEVEL_MAP: Record<string, LogSeverityLevel> = {
// npm
silly: 'trace',
// npm and syslog
debug: 'debug',
// npm
verbose: 'debug',
// npm
http: 'debug',
// npm and syslog
info: 'info',
// syslog
notice: 'info',
// npm
warn: 'warn',
// syslog
warning: 'warn',
// npm and syslog
error: 'error',
// syslog
emerg: 'fatal',
// syslog
alert: 'fatal',
// syslog
crit: 'fatal',
};