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