UNPKG

975 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 json: options.json,
11 batching: options.batching !== undefined ? options.batching : true,
12 clearOnError: options.clearOnError,
13 replaceTimestamp: options.replaceTimestamp
14 })
15 }
16
17 log (info, callback) {
18 setImmediate(() => {
19 this.emit('logged', info)
20 })
21
22 const { label, timestamp, level, message, ...rest } = info
23 const logEntry = {
24 labels: `{job="${label}", level="${level}"}`,
25 entries: [
26 {
27 ts: timestamp,
28 line: `${message} ${
29 rest && Object.keys(rest).length > 0 ? JSON.stringify(rest) : ''
30 }`
31 }
32 ]
33 }
34
35 this.batcher.pushLogEntry(logEntry)
36
37 callback()
38 }
39}