UNPKG

811 BJavaScriptView Raw
1const Transport = require('winston-transport')
2const Batcher = require('./src/batcher')
3
4module.exports = class LokiTransport extends Transport {
5 constructor (options) {
6 super(options)
7 this.batcher = new Batcher({
8 host: options.host,
9 interval: options.interval
10 })
11 this.batcher.run()
12 }
13
14 log (info, callback) {
15 setImmediate(() => {
16 this.emit('logged', info)
17 })
18
19 const { label, timestamp, level, message, ...rest } = info
20 const logEntry = {
21 labels: `{job="${label}", level="${level}"}`,
22 entries: [
23 {
24 ts: timestamp,
25 line: `${message} ${(rest &&
26 Object.keys(rest) &&
27 JSON.stringify(rest)) ||
28 ''}`
29 }
30 ]
31 }
32
33 this.batcher.pushLogEntry(logEntry)
34
35 callback()
36 }
37}