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