-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
73 lines (54 loc) · 1.48 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const winston = require('winston')
const util = require('util')
const _ = require('lodash')
const DEFAULT_REGION = 'us'
const Logger = require('r7insight_node')
function transformLevel(level) {
const codes = {
error: 'err',
warn: 'warning',
info: 'info',
verbose: 'debug',
debug: 'debug',
silly: 'debug'
}
return codes[level]
}
let InsightOpsTransport = module.exports = function (options) {
options = options || {}
winston.Transport.call(this, options)
if (!options.token) {
throw new Error('A token must be set for Insight Transport')
}
this.silent = options.silent || false
this.logger = new Logger({
token: options.token,
region: options.region || DEFAULT_REGION
})
}
util.inherits(InsightOpsTransport, winston.Transport)
winston.transports.InsightOpsTransport = InsightOpsTransport
InsightOpsTransport.prototype.log = function (level, message, meta, callback) {
if (this.silent) {
return callback(null, true);
}
const _level = transformLevel(level)
if (!_.isEmpty(message)) {
this.logger.log(_level, message, meta)
}
callback(null, true);
}
InsightOpsTransport.prototype.logException = function (msg, meta, callback, err) {
let self = this;
function onLogged() {
self.removeListener('error', onError);
callback();
}
function onError() {
self.removeListener('logged', onLogged);
callback();
}
this.once('logged', onLogged);
this.once('error', onError);
this.logger.log('err', err)
};