UNPKG

3.78 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
29### Example
30With default formatting:
31```js
32const { createLogger, transports } = require("winston");
33const LokiTransport = require("winston-loki");
34const options = {
35 ...,
36 transports: [
37 new LokiTransport({
38 host: "http://localhost:3100"
39 })
40 ]
41 ...
42};
43const logger = createLogger(options);
44```
45
46You can set custom labels in every log as well like this:
47```js
48logger.debug({ message: 'test', labels: { 'key': 'value' } })
49```
50
51TODO: Add custom formatting example
52
53## Developing
54### Requirements
55Running 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.
56
57Refer to https://github.com/grafana/loki/blob/master/docs/api.md for documentation about the available endpoints, data formats etc.
58
59### Example
60```sh
61npm install
62npm link
63cd ~/your_project
64npm link winston-loki
65npm install
66```
67And you should have a working, requirable winston-loki package under your project's node_modules.
68After the link has been established, any changes to winston-loki should show on rerun of the software that uses it.
69
70### Run tests
71```sh
72npm test
73```
74
75Write new ones under `/test`