forked from GasiorowskiPiotr/express-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
43 lines (36 loc) · 1.33 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
import * as appInsights from 'applicationinsights';
import * as express from 'express';
import * as uuid from 'uuid';
import { Logger } from './logger';
require('es6-shim');
export const loggers = (app: express.Application, instrumentationKey: string, disableAutoCollect: boolean = false) => {
var aiSetup = appInsights.setup(instrumentationKey);
if(disableAutoCollect) {
aiSetup = aiSetup
.setAutoCollectPerformance(false)
.setAutoCollectConsole(false)
.setAutoCollectRequests(false)
.setAutoCollectExceptions(false);
}
aiSetup.start();
const ai = (appInsights as any).defaultClient;
const logger = new Logger(ai);
app.locals.log = logger;
return {
logErrors: (err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.traceError(err, '', {
url: req.url,
requestId: res.locals.requestId
});
next(err);
},
logRequest: (req: express.Request, res: express.Response, next: express.NextFunction) => {
res.locals.log = logger;
res.locals.requestId = uuid.v4();
logger.trackRequest(req, res, {
requestId: res.locals.requestId
});
next();
},
}
};