@miketako3/cloki 0.1.13 → 0.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # cloki - Simple and Zero Dependency Logging Library from Cloudflare Workers to Grafana Cloud's Loki
1
+ # cloki - Zero Dependency and Simple Logging Library from Cloudflare Workers to Grafana Cloud's Loki
2
2
 
3
3
  [![NPM Version](https://img.shields.io/npm/v/%40miketako3%2Fcloki)](https://www.npmjs.com/package/@miketako3/cloki)
4
4
  [![NPM License](https://img.shields.io/npm/l/%40miketako3%2Fcloki)](https://github.com/miketako3/cloki/blob/main/LICENSE)
@@ -10,14 +10,14 @@
10
10
 
11
11
  ## Introduction
12
12
 
13
- Welcome to **cloki**, an open-source logger designed to bridge Cloudflare Workers with Grafana Cloud's Loki seamlessly and efficiently. Targeted at individual developers, cloki aims to reduce maintenance costs while offering a straightforward logging solution. With minimal configuration and the sole use of the fetch API, cloki is an easy-to-implement tool for effective logging.
13
+ Welcome to **cloki**, an open-source logger designed to bridge [Cloudflare Workers](https://www.cloudflare.com/developer-platform/workers/) with [Grafana Cloud's Loki](https://grafana.com/products/cloud/logs/) seamlessly and efficiently. Targeted at individual developers, cloki aims to reduce maintenance costs while offering a straightforward logging solution. With minimal configuration and the sole use of the fetch API, cloki is an easy-to-implement tool for effective logging.
14
14
 
15
15
  ## Features
16
16
 
17
17
  - **Easy Integration**: Connect Cloudflare Workers with Grafana Cloud's Loki effortlessly.
18
18
  - **Minimal Configuration**: Get started with just a few simple settings.
19
- - **Cost-Effective**: Designed for developers looking to minimize overhead costs.
20
- - **Pure Fetch API Usage**: No complex dependencies or setups, just the fetch API.
19
+ - **Zero Dependencies**: cloki has zero dependencies, making it easy to maintain.
20
+ - **Fetch API Usage**: cloki uses the Fetch API, so it can be used in any environment like edge runtime.
21
21
 
22
22
  ## Installation
23
23
 
@@ -25,20 +25,30 @@ Welcome to **cloki**, an open-source logger designed to bridge Cloudflare Worker
25
25
  $ npm i @miketako3/cloki
26
26
  ```
27
27
 
28
+ ## Grafana Cloud Setup
29
+
30
+ 1. Create a Grafana Cloud account from [here](https://grafana.com/).
31
+ 2. Access https://grafana.com/orgs/${YOUR_ORG_NAME}
32
+ 3. Click Detail. ![](static/image1.png)
33
+ 4. You got the required information. ![](static/image2.png)
34
+
28
35
  ## Usage
29
36
 
30
37
  ```typescript
31
38
  import {Cloki} from '@miketako3/cloki'
32
39
 
33
40
  const logger = getLokiLogger({
34
- lokiHost: "${LOKI_HOST}",
35
- lokiUser: "${LOKI_USER}",
36
- lokiToken: "${LOKI_TOKEN}"
41
+ lokiHost: "Host URL (e.g. logs-xxx-yyy.grafana.net)",
42
+ lokiUser: "User (e.g. 123456)",
43
+ lokiToken: "Generated API Token"
37
44
  });
38
45
 
39
46
  await logger.info({message: "Hello World!"});
40
47
 
41
48
  await logger.error({message: "Hello World!", error: error});
49
+
50
+ // with addional labels
51
+ await logger.info({message: "Hello World!"}, {foo: "bar"});
42
52
  ```
43
53
 
44
54
  ## Contributing
package/dist/logger.d.ts CHANGED
@@ -6,6 +6,12 @@ type LokiConfig = {
6
6
  lokiToken: string;
7
7
  lokiUser: string;
8
8
  };
9
+ /**
10
+ * Loki labels
11
+ */
12
+ type LokiLabels = {
13
+ [key: string]: string;
14
+ };
9
15
  /**
10
16
  * Create a Loki logger
11
17
  * logger has some async logging methods like info, error, warn, etc.
@@ -13,9 +19,9 @@ type LokiConfig = {
13
19
  * @param config
14
20
  */
15
21
  export declare const getLokiLogger: (config: LokiConfig) => {
16
- info: (message: object) => Promise<void>;
17
- warn: (message: object) => Promise<void>;
18
- error: (message: object) => Promise<void>;
19
- debug: (message: object) => Promise<void>;
22
+ info: (message: object, labels?: LokiLabels) => Promise<void>;
23
+ warn: (message: object, labels?: LokiLabels) => Promise<void>;
24
+ error: (message: object, labels?: LokiLabels) => Promise<void>;
25
+ debug: (message: object, labels?: LokiLabels) => Promise<void>;
20
26
  };
21
27
  export {};
@@ -49,6 +49,25 @@ describe('Loki Logger', () => {
49
49
  // Reset the spy
50
50
  consoleSpy.mockRestore();
51
51
  }));
52
+ it.each(['info', 'warn', 'error', 'debug'])('should call log for %s method with some labels', (method) => __awaiter(void 0, void 0, void 0, function* () {
53
+ const logger = (0, logger_1.getLokiLogger)(mockConfig);
54
+ const mockMessage = { test: 'message' };
55
+ const mockLabels = { hoge: 'huga' };
56
+ const consoleSpy = jest.spyOn(console, 'log').mockImplementation();
57
+ yield logger[method](mockMessage, mockLabels);
58
+ expect(consoleSpy).toHaveBeenCalledWith(JSON.stringify(mockMessage));
59
+ expect(fetch).toHaveBeenCalledTimes(1);
60
+ expect(fetch).toHaveBeenCalledWith('https://testhost/loki/api/v1/push', {
61
+ method: "POST",
62
+ headers: {
63
+ "Content-Type": "application/json",
64
+ Authorization: `Basic ${btoa("user:token123")}`
65
+ },
66
+ body: `{\"streams\":[{\"stream\":{\"level\":\"${method}\",\"hoge\":\"huga\"},\"values\":[[\"1482363367071000000\",\"{\\\"test\\\":\\\"message\\\"}\"]]}]}`
67
+ });
68
+ // Reset the spy
69
+ consoleSpy.mockRestore();
70
+ }));
52
71
  });
53
72
  });
54
73
  //# sourceMappingURL=logger.test.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"logger.test.js","sourceRoot":"","sources":["../src/logger.test.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,qCAAyC,CAAC,sCAAsC;AAEhF,MAAM,CAAC,KAAK,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,EAAE,CAC1B,OAAO,CAAC,OAAO,CAAC;IACd,EAAE,EAAE,IAAI;CACT,CAAC,CACU,CAAC;AAEf,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,aAAa,CAAC,CAAA;AAEvC,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IAC3B,MAAM,UAAU,GAAG;QACjB,QAAQ,EAAE,UAAU;QACpB,SAA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CAAC,CAAC;YACzC,MAAM,WAAW,GAAG,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;YACxC,MAAM,UAAU,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;YACpC,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,kBAAkB,EAAE,CAAC;YAEnE,MAAM,MAAM,CAAC,MAA6B,CAAC,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;YAErE,MAAM,CAAC,UAAU,CAAC,CAAC,oBAAoB,CAAC,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC;YACrE,MAAM,CAAC,KAAK,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;YACvC,MAAM,CAAC,KAAK,CAAC,CAAC,oBAAoB,CAAC,mCAAmC,EAAE;gBACtE,MAAM,EAAE,MAAM;gBACd,OAAO,EAAE;oBACP,cAAc,EAAE,kBAAkB;oBAClC,aAAa,EAAE,SAAS,IAAI,CAAC,eAAe,CAAC,EAAE;iBAChD;gBACD,IAAI,EAAE,0CAA0C,MAAM,oGAAoG;aAC3J,CAAC,CAAA;YAGF,gBAAgB;YAChB,UAAU,CAAC,WAAW,EAAE,CAAC;QAC3B,CAAC,CAAA,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@miketako3/cloki",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "cloki is zero dependency and simple logger library for Loki and Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "files": [