-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (46 loc) · 1.48 KB
/
app.js
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
'use strict';
const Sentry = require('@sentry/node');
const util = require('./app/util');
const assert = require('assert');
const SentryLoggerTransport = require('./app/util/sentryLoggerTransport');
class AppBootHook {
constructor(app) {
this.app = app;
}
async configWillLoad() {
const { app } = this;
// 加载插件中间件
app.config.coreMiddleware.unshift('requestTranstionMiddleware');
}
async didLoad() {
const { app } = this;
// 所有的配置已经加载完毕
const { formatDate } = util;
const { weSentry = {} } = app.config;
const { config = {} } = weSentry;
const { enable = true } = config;
if (enable) {
assert(config.dsn, '[egg-we-wentry] dsn must be set in weSentry config!');
Sentry.init({
...config,
release: config.release || `${config.servername}-${formatDate(Date.now())}`,
tracesSampleRate: config.tracesSampleRate || 1,
});
app.Sentry = Sentry;
for (const [ name, logger ] of app.loggers.entries()) {
const transport = new SentryLoggerTransport({
level: logger.options.level,
loggerEntry: name + '-sentry',
app,
});
logger.set('sentry', transport);
}
app.on('error', (err, ctx) => {
ctx = ctx || app.createAnonymousContext();
ctx.service.sentryService.errorCapture(err);
});
console.log('[egg-we-sentry] egg-we-sentry plugin is ready.');
}
}
}
module.exports = AppBootHook;