Skip to content

Commit fa93ef7

Browse files
authored
Merge pull request #25 from JaniAnttonen/feat/size-optimization
Prepare for 6.0.0 release
2 parents fae79bd + fbbd59f commit fa93ef7

21 files changed

Lines changed: 4816 additions & 4761 deletions

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
test
2+
examples

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- "10"
4+
- "12"
55
branches:
66
only:
77
- master

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# winston-loki
22

33
[![npm version](https://badge.fury.io/js/winston-loki.svg)](https://badge.fury.io/js/winston-loki)
4+
[![install size](https://packagephobia.now.sh/badge?p=winston-loki)](https://packagephobia.now.sh/result?p=winston-loki)
45
[![Build Status](https://travis-ci.com/JaniAnttonen/winston-loki.svg?branch=master)](https://travis-ci.com/JaniAnttonen/winston-loki)
56
[![Coverage Status](https://coveralls.io/repos/github/JaniAnttonen/winston-loki/badge.svg?branch=master)](https://coveralls.io/github/JaniAnttonen/winston-loki?branch=master)
67
[![Maintainability](https://api.codeclimate.com/v1/badges/17a55cce14d581c308bc/maintainability)](https://codeclimate.com/github/JaniAnttonen/winston-loki/maintainability)
7-
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
88

99

1010
A Grafana Loki transport for the nodejs logging library Winston.

examples/basicAuth.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const winston = require('winston')
2+
const LokiTransport = require('../index')
3+
const logger = winston.createLogger()
4+
5+
logger.add(new winston.transports.Console({
6+
format: winston.format.json(),
7+
level: 'debug'
8+
}))
9+
10+
logger.add(new LokiTransport({
11+
host: 'http://127.0.0.1:3100',
12+
json: true,
13+
basicAuth: 'username:password',
14+
labels: { job: 'winston-loki-example' }
15+
}))
16+
17+
const wait = (duration) => new Promise(resolve => {
18+
setTimeout(resolve, duration)
19+
})
20+
21+
const run = async () => {
22+
while (true) {
23+
logger.debug('I am a debug log')
24+
logger.info('This is a test, no need to panic')
25+
logger.error('Testing for errors')
26+
await wait(1000)
27+
}
28+
}
29+
30+
run()

examples/docker-compose.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: "3"
2+
3+
networks:
4+
loki:
5+
6+
services:
7+
loki:
8+
image: grafana/loki:latest
9+
ports:
10+
- "3100:3100"
11+
command: -config.file=/etc/loki/local-config.yaml
12+
networks:
13+
- loki
14+
15+
promtail:
16+
image: grafana/promtail:latest
17+
volumes:
18+
- /var/log:/var/log
19+
command: -config.file=/etc/promtail/docker-config.yaml
20+
networks:
21+
- loki
22+
23+
grafana:
24+
image: grafana/grafana:master
25+
ports:
26+
- "3000:3000"
27+
networks:
28+
- loki

examples/json.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const winston = require('winston')
2+
const LokiTransport = require('../index')
3+
const logger = winston.createLogger()
4+
5+
logger.add(new winston.transports.Console({
6+
format: winston.format.json(),
7+
level: 'debug'
8+
}))
9+
10+
logger.add(new LokiTransport({
11+
host: 'http://127.0.0.1:3100',
12+
json: true,
13+
labels: { job: 'winston-loki-example' }
14+
}))
15+
16+
const wait = (duration) => new Promise(resolve => {
17+
setTimeout(resolve, duration)
18+
})
19+
20+
const run = async () => {
21+
while (true) {
22+
logger.debug('I am a debug log')
23+
logger.info('This is a test, no need to panic')
24+
logger.error('Testing for errors')
25+
await wait(1000)
26+
}
27+
}
28+
29+
run()

examples/proto.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const winston = require('winston')
2+
const LokiTransport = require('../index')
3+
const logger = winston.createLogger()
4+
5+
logger.add(new winston.transports.Console({
6+
format: winston.format.json(),
7+
level: 'debug'
8+
}))
9+
10+
logger.add(new LokiTransport({
11+
host: 'http://127.0.0.1:3100',
12+
labels: { job: 'winston-loki-example' }
13+
}))
14+
15+
const wait = (duration) => new Promise(resolve => {
16+
setTimeout(resolve, duration)
17+
})
18+
19+
const run = async () => {
20+
while (true) {
21+
logger.debug('I am a debug log')
22+
logger.info('This is a test, no need to panic')
23+
logger.error('Testing for errors')
24+
await wait(1000)
25+
}
26+
}
27+
28+
run()

index.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class LokiTransport extends Transport {
2020
// Pass all the given options to batcher
2121
this.batcher = new Batcher({
2222
host: options.host,
23+
basicAuth: options.basicAuth,
24+
headers: options.headers || {},
2325
interval: options.interval,
2426
json: options.json,
2527
batching: options.batching !== false,
@@ -52,20 +54,12 @@ class LokiTransport extends Transport {
5254
const { label, labels, timestamp, level, message, ...rest } = info
5355

5456
// build custom labels if provided
55-
let lokiLabels
57+
let lokiLabels = { level: level }
58+
5659
if (this.labels) {
57-
lokiLabels = `{level="${level}"`
58-
for (let key in this.labels) {
59-
lokiLabels += `,${key}="${this.labels[key]}"`
60-
}
61-
if (labels) {
62-
for (let key in labels) {
63-
lokiLabels += `,${key}="${labels[key]}"`
64-
}
65-
}
66-
lokiLabels += '}'
60+
lokiLabels = Object.assign(lokiLabels, this.labels)
6761
} else {
68-
lokiLabels = `{job="${label}", level="${level}"}`
62+
lokiLabels['job'] = label
6963
}
7064

7165
// follow the format provided
@@ -78,7 +72,7 @@ class LokiTransport extends Transport {
7872
labels: lokiLabels,
7973
entries: [
8074
{
81-
ts: timestamp || Date.now(),
75+
ts: timestamp || Date.now().valueOf(),
8276
line
8377
}
8478
]

0 commit comments

Comments
 (0)