This repository was archived by the owner on Jan 6, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathsentryWinstonTransport.ts
More file actions
190 lines (155 loc) · 4.92 KB
/
sentryWinstonTransport.ts
File metadata and controls
190 lines (155 loc) · 4.92 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
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Code initially taken from https://github.com/aandrewww/winston-transport-sentry-node
*/
import * as Sentry from '@sentry/electron/main';
import TransportStream from 'winston-transport';
import { rateLimit } from './rateLimiter.js';
// import { LEVEL } from 'triple-beam';
enum SentrySeverity {
Debug = 'debug',
Log = 'log',
Info = 'info',
Warning = 'warning',
Error = 'error',
Fatal = 'fatal',
}
interface SeverityOptions {
[key: string]: Sentry.SeverityLevel;
}
const DEFAULT_LEVELS_MAP: SeverityOptions = {
silly: SentrySeverity.Debug,
verbose: SentrySeverity.Debug,
info: SentrySeverity.Info,
debug: SentrySeverity.Debug,
warn: SentrySeverity.Warning,
error: SentrySeverity.Error,
};
export interface SentryTransportOptions
extends TransportStream.TransportStreamOptions {
sentry?: Sentry.ElectronMainOptions;
levelsMap?: SeverityOptions;
skipSentryInit?: boolean;
}
class ExtendedError extends Error {
constructor(info: any) {
super(info.message);
this.name = info.name || 'Error';
if (info.stack && typeof info.stack === 'string') {
this.stack = info.stack;
}
}
}
export default class SentryTransport extends TransportStream {
public silent = false;
private levelsMap: SeverityOptions = {};
private normalLogRateLimiter: (info: any, callback: () => void) => void;
public constructor(opts?: SentryTransportOptions) {
super(opts);
this.levelsMap = this.setLevelsMap(opts?.levelsMap);
this.silent = opts?.silent || false;
// Only rate limit normal logs, not errors
this.normalLogRateLimiter = rateLimit(10, 1000, this.processLog.bind(this));
if (!opts || !opts.skipSentryInit) {
Sentry.init(SentryTransport.withDefaults(opts?.sentry || {}));
}
}
private processLog(info: any, callback: () => void) {
setImmediate(() => {
this.emit('logged', info);
});
if (this.silent) return callback();
const { message, tags, user, ...meta } = info;
// const winstonLevel = info[LEVEL];
const winstonLevel = info.level;
const sentryLevel = this.levelsMap[winstonLevel];
Sentry.configureScope((scope) => {
scope.clear();
if (tags !== undefined && SentryTransport.isObject(tags)) {
scope.setTags(tags);
}
scope.setExtras(meta);
if (user !== undefined && SentryTransport.isObject(user)) {
scope.setUser(user);
}
// TODO: add fingerprints
// scope.setFingerprint(['{{ default }}', path]); // fingerprint should be an array
// scope.clear();
});
// TODO: add breadcrumbs
// Sentry.addBreadcrumb({
// message: 'My Breadcrumb',
// // ...
// });
// Capturing Errors / Exceptions - bypass rate limiting
if (SentryTransport.shouldLogException(sentryLevel)) {
const error =
Object.values(info).find((value) => value instanceof Error) ??
new ExtendedError(info);
Sentry.captureException(error, { tags });
callback();
return;
}
// Normal messages go through rate limiting
Sentry.captureMessage(message, sentryLevel);
callback();
}
public log(info: any, callback: () => void) {
// Errors and fatal logs bypass rate limiting
if (SentryTransport.shouldLogException(this.levelsMap[info.level])) {
this.processLog(info, callback);
return;
}
// Normal logs are rate limited
this.normalLogRateLimiter(info, callback);
}
end(...args: any[]) {
Sentry.flush().then(() => {
super.end(...args);
});
return this;
}
public get sentry() {
return Sentry;
}
private setLevelsMap = (options?: SeverityOptions): SeverityOptions => {
if (!options) {
return DEFAULT_LEVELS_MAP;
}
const customLevelsMap = Object.keys(options).reduce<SeverityOptions>(
(acc: { [key: string]: any }, winstonSeverity: string) => {
acc[winstonSeverity] = options[winstonSeverity];
return acc;
},
{},
);
return {
...DEFAULT_LEVELS_MAP,
...customLevelsMap,
};
};
private static withDefaults(options: Sentry.ElectronMainOptions) {
return {
...options,
dsn: options?.dsn || process.env.SENTRY_DSN || '',
serverName: options?.serverName || 'winston-transport-sentry-node',
environment:
options?.environment ||
process.env.SENTRY_ENVIRONMENT ||
process.env.NODE_ENV ||
'production',
debug: options?.debug || !!process.env.SENTRY_DEBUG || false,
sampleRate: options?.sampleRate || 1.0,
maxBreadcrumbs: options?.maxBreadcrumbs || 100,
};
}
// private normalizeMessage(msg: any) {
// return msg && msg.message ? msg.message : msg;
// }
private static isObject(obj: any) {
const type = typeof obj;
return type === 'function' || (type === 'object' && !!obj);
}
private static shouldLogException(level: Sentry.SeverityLevel) {
return level === SentrySeverity.Fatal || level === SentrySeverity.Error;
}
}