1 | const Transport = require('winston-transport')
|
2 | const Batcher = require('./src/batcher')
|
3 |
|
4 | module.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
|
12 | })
|
13 | options.batching && this.batcher.run()
|
14 | }
|
15 |
|
16 | log (info, callback) {
|
17 | setImmediate(() => {
|
18 | this.emit('logged', info)
|
19 | })
|
20 |
|
21 | const { label, timestamp, level, message, ...rest } = info
|
22 | const logEntry = {
|
23 | labels: `{job="${label}", level="${level}"}`,
|
24 | entries: [
|
25 | {
|
26 | ts: timestamp,
|
27 | line: `${message} ${
|
28 | rest && Object.keys(rest).length > 0 ? JSON.stringify(rest) : ''
|
29 | }`
|
30 | }
|
31 | ]
|
32 | }
|
33 |
|
34 | this.batcher.pushLogEntry(logEntry)
|
35 |
|
36 | callback()
|
37 | }
|
38 | }
|