-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
68 lines (60 loc) · 1.55 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
'use strict'
const Transport = require('winston-transport')
const {createLogger} = require('@logdna/logger')
const pkg = require('./package.json')
/*
* Support for Winston Transport
*/
module.exports = class LogDNATransport extends Transport {
constructor(options) {
const {levels, maxLevel, key, ...opts} = options
super({
...opts
, levels
, level: maxLevel
})
let custom_levels = levels
if (!custom_levels) {
// Per the winston docs, their 'npm' levels will be used, and those must be
// set up as custom levels in LogDNA.
// @see https://github.com/winstonjs/winston#logging-levels
custom_levels = {
error: 0
, warn: 1
, info: 2
, http: 3
, verbose: 4
, debug: 5
, silly: 6
}
}
// Create an instance of @logdna/logger
this.logger = createLogger(key, {
...opts
, levels: Object.keys(custom_levels)
, UserAgent: `${pkg.name}/${pkg.version}`
})
}
log(info, callback) {
const level = info.level
if (info.error instanceof Error) {
info.error = info.error.stack || info.error.toString()
}
if (!info.message) {
// Send the incoming object payload as the message
this.logger.log(info, level)
callback(null, true)
return
}
// eslint-disable-next-line no-unused-vars
const {level: _, message, indexMeta, timestamp, ...meta} = info
const opts = {
level
, indexMeta
, timestamp
, meta
}
this.logger.log(message, opts)
callback(null, true)
}
}