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 |
|
10 | A Grafana Loki transport for the nodejs logging library Winston.
|
11 |
|
12 | ## Usage
|
13 | This 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
|
16 | LokiTransport() 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
|
31 | With default formatting:
|
32 | ```js
|
33 | const { createLogger, transports } = require("winston");
|
34 | const LokiTransport = require("winston-loki");
|
35 | const options = {
|
36 | ...,
|
37 | transports: [
|
38 | new LokiTransport({
|
39 | host: "http://localhost:3100"
|
40 | })
|
41 | ]
|
42 | ...
|
43 | };
|
44 | const logger = createLogger(options);
|
45 | ```
|
46 |
|
47 | You can set custom labels in every log as well like this:
|
48 | ```js
|
49 | logger.debug({ message: 'test', labels: { 'key': 'value' } })
|
50 | ```
|
51 |
|
52 | TODO: Add custom formatting example
|
53 |
|
54 | ## Developing
|
55 | ### Requirements
|
56 | Running 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 |
|
58 | Refer 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
|
62 | npm install
|
63 | npm link
|
64 | cd ~/your_project
|
65 | npm link winston-loki
|
66 | npm install
|
67 | ```
|
68 | And you should have a working, requirable winston-loki package under your project's node_modules.
|
69 | After 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
|
73 | npm test
|
74 | ```
|
75 |
|
76 | Write new ones under `/test`
|