forked from logdna/logdna-winston
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (49 loc) · 1.28 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
'use strict'
const Transport = require('winston-transport')
const {createLogger} = require('@logdna/logger')
const pkg = require('./package.json')
// Convert between Winston levels and @logdna/logger levels
const levelTranslate = new Map([
['error', 'error']
, ['warn', 'warn']
, ['info', 'info']
, ['http', 'debug']
, ['verbose', 'debug']
, ['debug', 'debug']
, ['silly', 'trace']
])
/*
* Support for Winston Transport
*/
module.exports = class LogDNATransport extends Transport {
constructor(options) {
super(options)
// Create an instance of @logdna/logger
this.logger = createLogger(options.key, {
...options
, UserAgent: `${pkg.name}/${pkg.version}`
})
}
log(info, callback) {
const level = levelTranslate.get(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)
}
}