All files / winston-loki index.js

91.67% Statements 11/12
75% Branches 3/4
66.67% Functions 2/3
91.67% Lines 11/12
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 381x 1x   1x   4x 4x       4x       3x       3x 3x                         3x   3x      
const Transport = require('winston-transport')
const Batcher = require('./src/batcher')
 
module.exports = class LokiTransport extends Transport {
  constructor (options) {
    super(options)
    this.batcher = new Batcher({
      host: options.host,
      interval: options.interval
    })
    this.batcher.run()
  }
 
  log (info, callback) {
    setImmediate(() => {
      this.emit('logged', info)
    })
 
    const { label, timestamp, level, message, ...rest } = info
    const logEntry = {
      labels: `{job="${label}", level="${level}"}`,
      entries: [
        {
          ts: timestamp,
          line: `${message} ${(rest &&
            Object.keys(rest) &&
            JSON.stringify(rest)) ||
            ''}`
        }
      ]
    }
 
    this.batcher.pushLogEntry(logEntry)
 
    callback()
  }
}