Skip to content

Commit

Permalink
Merge pull request #25 from JaniAnttonen/feat/size-optimization
Browse files Browse the repository at this point in the history
Prepare for 6.0.0 release
  • Loading branch information
JaniAnttonen authored Apr 27, 2020
2 parents fae79bd + fbbd59f commit fa93ef7
Show file tree
Hide file tree
Showing 21 changed files with 4,816 additions and 4,761 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test
examples
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sudo: false
language: node_js
node_js:
- "10"
- "12"
branches:
only:
- master
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# winston-loki

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


A Grafana Loki transport for the nodejs logging library Winston.
Expand Down
30 changes: 30 additions & 0 deletions examples/basicAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const winston = require('winston')
const LokiTransport = require('../index')
const logger = winston.createLogger()

logger.add(new winston.transports.Console({
format: winston.format.json(),
level: 'debug'
}))

logger.add(new LokiTransport({
host: 'http://127.0.0.1:3100',
json: true,
basicAuth: 'username:password',
labels: { job: 'winston-loki-example' }
}))

const wait = (duration) => new Promise(resolve => {
setTimeout(resolve, duration)
})

const run = async () => {
while (true) {
logger.debug('I am a debug log')
logger.info('This is a test, no need to panic')
logger.error('Testing for errors')
await wait(1000)
}
}

run()
28 changes: 28 additions & 0 deletions examples/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
version: "3"

networks:
loki:

services:
loki:
image: grafana/loki:latest
ports:
- "3100:3100"
command: -config.file=/etc/loki/local-config.yaml
networks:
- loki

promtail:
image: grafana/promtail:latest
volumes:
- /var/log:/var/log
command: -config.file=/etc/promtail/docker-config.yaml
networks:
- loki

grafana:
image: grafana/grafana:master
ports:
- "3000:3000"
networks:
- loki
29 changes: 29 additions & 0 deletions examples/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const winston = require('winston')
const LokiTransport = require('../index')
const logger = winston.createLogger()

logger.add(new winston.transports.Console({
format: winston.format.json(),
level: 'debug'
}))

logger.add(new LokiTransport({
host: 'http://127.0.0.1:3100',
json: true,
labels: { job: 'winston-loki-example' }
}))

const wait = (duration) => new Promise(resolve => {
setTimeout(resolve, duration)
})

const run = async () => {
while (true) {
logger.debug('I am a debug log')
logger.info('This is a test, no need to panic')
logger.error('Testing for errors')
await wait(1000)
}
}

run()
28 changes: 28 additions & 0 deletions examples/proto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const winston = require('winston')
const LokiTransport = require('../index')
const logger = winston.createLogger()

logger.add(new winston.transports.Console({
format: winston.format.json(),
level: 'debug'
}))

logger.add(new LokiTransport({
host: 'http://127.0.0.1:3100',
labels: { job: 'winston-loki-example' }
}))

const wait = (duration) => new Promise(resolve => {
setTimeout(resolve, duration)
})

const run = async () => {
while (true) {
logger.debug('I am a debug log')
logger.info('This is a test, no need to panic')
logger.error('Testing for errors')
await wait(1000)
}
}

run()
20 changes: 7 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class LokiTransport extends Transport {
// Pass all the given options to batcher
this.batcher = new Batcher({
host: options.host,
basicAuth: options.basicAuth,
headers: options.headers || {},
interval: options.interval,
json: options.json,
batching: options.batching !== false,
Expand Down Expand Up @@ -52,20 +54,12 @@ class LokiTransport extends Transport {
const { label, labels, timestamp, level, message, ...rest } = info

// build custom labels if provided
let lokiLabels
let lokiLabels = { level: level }

if (this.labels) {
lokiLabels = `{level="${level}"`
for (let key in this.labels) {
lokiLabels += `,${key}="${this.labels[key]}"`
}
if (labels) {
for (let key in labels) {
lokiLabels += `,${key}="${labels[key]}"`
}
}
lokiLabels += '}'
lokiLabels = Object.assign(lokiLabels, this.labels)
} else {
lokiLabels = `{job="${label}", level="${level}"}`
lokiLabels['job'] = label
}

// follow the format provided
Expand All @@ -78,7 +72,7 @@ class LokiTransport extends Transport {
labels: lokiLabels,
entries: [
{
ts: timestamp || Date.now(),
ts: timestamp || Date.now().valueOf(),
line
}
]
Expand Down
Loading

0 comments on commit fa93ef7

Please sign in to comment.