-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
91 lines (73 loc) · 2.36 KB
/
index.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
import { isFunction, isError, defaultsDeep } from 'lodash'
import * as Transport from 'winston-transport'
import * as Sentry from '@sentry/node'
import * as Integrations from '@sentry/integrations'
interface WinstonSentryOptions {
debug?: boolean
level?: string
init?: boolean
sentry?: any
sentryOpts: Sentry.NodeOptions
sentryScope? (scope: Sentry.Scope): void
}
export class SentryTransport extends Transport.default {
Sentry: any
debug: boolean
level: string
constructor (opts: WinstonSentryOptions) {
super(opts)
opts = defaultsDeep(opts, {
level: 'error',
init: true,
debug: false,
sentryOpts: {
attachStacktrace: true,
sendDefaultPii: true,
integrations: [
new Sentry.Integrations.Modules(),
new Integrations.Transaction()
]
}
})
this.name = opts.name ? opts.name : 'winston-sentry-logger'
this.debug = opts.debug!
this.level = opts.level!
if (isFunction(opts.sentryScope)) Sentry.configureScope(opts.sentryScope)
// Define internal sentry to use
this.Sentry = Sentry
if (opts.sentry) this.Sentry = opts.sentry
if (opts.init === true) this.Sentry.init(opts.sentryOpts)
}
log (info: any, callback: Function) {
setImmediate(() => this.emit('logged', info))
// Some attribute is not in Sentry type
let self = this as any
// No need to log if level is above transport level
if (!(self.levels[info.level] <= self.levels[self.level!])) {
return callback()
}
try {
let error: Error | undefined = undefined
if (isError(info)) error = info
else if (info.error && isError(info.error)) error = info.error
if (typeof error === 'undefined') {
if (self.debug) console.log('Capture message: ', info)
self.Sentry.captureMessage(info)
return callback()
}
self.Sentry.withScope((scope: Sentry.Scope) => {
if (self.debug) console.log('Capture exception: ', error)
scope.setExtra('info', info)
if (error !== undefined) {
if (error.stack) scope.setExtra('stack', error.stack)
if (error.message) scope.setExtra('message', error.message)
}
self.Sentry.captureException(error)
})
} catch (error) {
if (self.debug) console.log(error)
self.emit('error', error)
}
callback()
}
}