UNPKG

3.91 kBMarkdownView Raw
1# winston-loki
2
3[![npm version](https://badge.fury.io/js/winston-loki.svg)](https://badge.fury.io/js/winston-loki)
4[![Build Status](https://travis-ci.com/JaniAnttonen/winston-loki.svg?branch=master)](https://travis-ci.com/JaniAnttonen/winston-loki)
5[![Coverage Status](https://coveralls.io/repos/github/JaniAnttonen/winston-loki/badge.svg?branch=master)](https://coveralls.io/github/JaniAnttonen/winston-loki?branch=master)
6[![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)
8
9
10A Grafana Loki transport for the nodejs logging library Winston.
11
12## Usage
13This Winston transport is used similarly to other Winston transports. Require winston and define a new LokiTransport() inside its options when creating it.
14
15### Options
16LokiTransport() takes a Javascript object as an input. These are the options that are available, __required in bold__:
17
18| **Parameter** | **Description** | **Example** | **Default** |
19| ------------------ | --------------------------------------------------------- | -----------------------| ------------- |
20| __`host`__ | URL for Grafana Loki | http://localhost:3100 | null |
21| `interval` | The interval at which batched logs are sent in seconds | 30 | 5 |
22| `json` | Use JSON instead of Protobuf for transport | true | false |
23| `batching` | If batching is not used, the logs are sent as they come | true | true |
24| `clearOnError` | Discard any logs that result in an error during transport | true | false |
25| `replaceTimestamp` | Replace any log timestamps with Date.now() | true | false |
26| `labels` | custom labels, key-value pairs | { module: 'http' } | null |
27| `format` | winston format (https://github.com/winstonjs/winston#formats) | simple() | null |
28| `gracefulShutdown` | Enable/disable graceful shutdown (wait for any unsent batches) | false | true |
29
30### Example
31With default formatting:
32```js
33const { createLogger, transports } = require("winston");
34const LokiTransport = require("winston-loki");
35const options = {
36 ...,
37 transports: [
38 new LokiTransport({
39 host: "http://localhost:3100"
40 })
41 ]
42 ...
43};
44const logger = createLogger(options);
45```
46
47You can set custom labels in every log as well like this:
48```js
49logger.debug({ message: 'test', labels: { 'key': 'value' } })
50```
51
52TODO: Add custom formatting example
53
54## Developing
55### Requirements
56Running a local Loki for testing is probably required, and the easiest way to do that is to follow this guide: https://github.com/grafana/loki/tree/master/production#run-locally-using-docker. After that, Grafana Loki instance is available at `http://localhost:3100`, with a Grafana instance running at `http://localhost:3000`. Username `admin`, password `admin`. Add the Loki source with the URL `http://loki:3100`, and the explorer should work.
57
58Refer to https://github.com/grafana/loki/blob/master/docs/api.md for documentation about the available endpoints, data formats etc.
59
60### Example
61```sh
62npm install
63npm link
64cd ~/your_project
65npm link winston-loki
66npm install
67```
68And you should have a working, requirable winston-loki package under your project's node_modules.
69After the link has been established, any changes to winston-loki should show on rerun of the software that uses it.
70
71### Run tests
72```sh
73npm test
74```
75
76Write new ones under `/test`