1 | const Transport = require('winston-transport')
|
2 | const Batcher = require('./src/batcher')
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | class LokiTransport extends Transport {
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 | constructor (options) {
|
17 | super(options)
|
18 |
|
19 |
|
20 | this.batcher = new Batcher({
|
21 | host: options.host,
|
22 | interval: options.interval,
|
23 | json: options.json,
|
24 | batching: options.batching !== undefined ? options.batching : true,
|
25 | clearOnError: options.clearOnError,
|
26 | replaceOnError: options.replaceOnError,
|
27 | replaceTimestamp: options.replaceTimestamp
|
28 | })
|
29 | }
|
30 |
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | log (info, callback) {
|
41 |
|
42 | setImmediate(() => {
|
43 | this.emit('logged', info)
|
44 | })
|
45 |
|
46 |
|
47 | const { label, timestamp, level, message, ...rest } = info
|
48 |
|
49 |
|
50 | const logEntry = {
|
51 | labels: `{job="${label}", level="${level}"}`,
|
52 | entries: [
|
53 | {
|
54 | ts: timestamp || Date.now(),
|
55 | line: `${message} ${
|
56 | rest && Object.keys(rest).length > 0 ? JSON.stringify(rest) : ''
|
57 | }`
|
58 | }
|
59 | ]
|
60 | }
|
61 |
|
62 |
|
63 | this.batcher.pushLogEntry(logEntry)
|
64 |
|
65 |
|
66 | callback()
|
67 | }
|
68 | }
|
69 |
|
70 | module.exports = LokiTransport
|