-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentry_util.ts
103 lines (92 loc) · 2.4 KB
/
sentry_util.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
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
import * as Sentry from '@sentry/node';
import * as Integrations from '@sentry/integrations';
import _ from 'lodash';
import util, { inspect } from 'util';
// @ts-ignore
import winston from 'winston';
import { Severity } from '@sentry/node';
const isProd = process.env.NODE_ENV === 'production';
const commitHash = process.env.COMMIT_HASH;
export const initialize = (opts = {}) => {
const initOpts = _.defaults(opts, {
debug: !isProd,
enabled: isProd,
release: commitHash,
integrations: [new Integrations.ExtraErrorData()],
});
Sentry.init(initOpts);
};
export const captureException = (err: any, locale: string, clientIp?: string, user?: any) => {
try {
Sentry.configureScope((scope: any) => {
scope.setTag('locale', locale);
if (user || clientIp) {
scope.setUser({
..._.pick(user, ['id', 'email']),
ip_address: clientIp,
});
}
});
Sentry.captureException(err);
} catch (e) {
console.error(e.stack);
}
};
export const captureMessage = (msg: string, level: Severity, clientIp?: string, user?: any) => {
try {
Sentry.configureScope((scope: any) => {
if (user || clientIp) {
scope.setUser({
..._.pick(user, ['id', 'email']),
ip_address: clientIp,
});
}
});
Sentry.captureMessage(msg, level)
} catch (e) {
console.error(e.stack);
}
};
// @ts-ignore
export const BreadcrumbTransport = (winston.transports.BreadcrumbTransport = function(options: any) {
//
// Name this logger
//
this.name = 'sentryBreadcrumbTransport';
//
// Set the level from your options
//
this.level = options.level || 'info';
//
// Configure your storage backing as you see fit
//
});
//
// Inherit from `winston.Transport` so you can take advantage
// of the base functionality and `.handleExceptions()`.
//
util.inherits(BreadcrumbTransport, winston.Transport);
BreadcrumbTransport.prototype.log = (
level: Sentry.Severity,
msg: string,
meta: any,
callback: (arg1: any, arg2: boolean) => void
) => {
Sentry.addBreadcrumb({
level,
message: msg,
data: { meta: inspect(meta) },
});
//
// Store this message and metadata, maybe use some custom logic
// then callback indicating success.
//
callback(null, true);
};
export const CrashReportUtil = {
initialize,
captureException,
BreadcrumbTransport,
captureMessage,
};
export default CrashReportUtil;