@miketako3/cloki 0.1.11 → 0.1.12

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,10 +1,11 @@
1
- # Cloki - Simple Logging from Cloudflare Workers to Grafana Cloud's Loki
1
+ # cloki - Simple and Zero Dependency Logging Library from Cloudflare Workers to Grafana Cloud's Loki
2
2
 
3
3
  [![npm version](https://badge.fury.io/js/@miketako3%2Fcloki.svg)](https://badge.fury.io/js/@miketako3%2Fcloki)
4
4
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
5
6
  ## Introduction
6
7
 
7
- 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.
8
+ 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.
8
9
 
9
10
  ## Features
10
11
 
@@ -22,9 +23,17 @@ $ npm i @miketako3/cloki
22
23
  ## Usage
23
24
 
24
25
  ```typescript
25
- import { Cloki } from '@miketako3/cloki'
26
+ import {Cloki} from '@miketako3/cloki'
27
+
28
+ const logger = getLokiLogger({
29
+ lokiHost: "${LOKI_HOST}",
30
+ lokiUser: "${LOKI_USER}",
31
+ lokiToken: "${LOKI_TOKEN}"
32
+ });
26
33
 
34
+ await logger.info({message: "Hello World!"});
27
35
 
36
+ await logger.error({message: "Hello World!", error: error});
28
37
  ```
29
38
 
30
39
  ## Contributing
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const logger_1 = require("./logger"); // Adjust the import path as necessary
13
+ global.fetch = jest.fn(() => Promise.resolve({
14
+ ok: true,
15
+ }));
16
+ Date.now = jest.fn(() => 1482363367071);
17
+ describe('Loki Logger', () => {
18
+ const mockConfig = {
19
+ lokiHost: 'testhost',
20
+ lokiToken: 'token123',
21
+ lokiUser: 'user',
22
+ };
23
+ beforeEach(() => {
24
+ jest.clearAllMocks();
25
+ });
26
+ it('should create a logger with the correct methods', () => {
27
+ const logger = (0, logger_1.getLokiLogger)(mockConfig);
28
+ expect(logger).toHaveProperty('info');
29
+ expect(logger).toHaveProperty('warn');
30
+ expect(logger).toHaveProperty('error');
31
+ expect(logger).toHaveProperty('debug');
32
+ });
33
+ describe('Logging Methods', () => {
34
+ it.each(['info', 'warn', 'error', 'debug'])('should call log for %s method', (method) => __awaiter(void 0, void 0, void 0, function* () {
35
+ const logger = (0, logger_1.getLokiLogger)(mockConfig);
36
+ const mockMessage = { test: 'message' };
37
+ const consoleSpy = jest.spyOn(console, 'log').mockImplementation();
38
+ yield logger[method](mockMessage);
39
+ expect(consoleSpy).toHaveBeenCalledWith(JSON.stringify(mockMessage));
40
+ expect(fetch).toHaveBeenCalledTimes(1);
41
+ expect(fetch).toHaveBeenCalledWith('https://testhost/loki/api/v1/push', {
42
+ method: "POST",
43
+ headers: {
44
+ "Content-Type": "application/json",
45
+ Authorization: `Basic ${btoa("user:token123")}`
46
+ },
47
+ body: `{\"streams\":[{\"stream\":{\"level\":\"${method}\"},\"values\":[[\"1482363367071000000\",\"{\\\"test\\\":\\\"message\\\"}\"]]}]}`
48
+ });
49
+ // Reset the spy
50
+ consoleSpy.mockRestore();
51
+ }));
52
+ });
53
+ });
54
+ //# sourceMappingURL=logger.test.js.map
@@ -0,0 +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,SAAS,EAAE,UAAU;QACrB,QAAQ,EAAE,MAAM;KACjB,CAAC;IAEF,UAAU,CAAC,GAAG,EAAE;QACd,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,iDAAiD,EAAE,GAAG,EAAE;QACzD,MAAM,MAAM,GAAG,IAAA,sBAAa,EAAC,UAAU,CAAC,CAAC;QACzC,MAAM,CAAC,MAAM,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;QACtC,MAAM,CAAC,MAAM,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;QACtC,MAAM,CAAC,MAAM,CAAC,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;QACvC,MAAM,CAAC,MAAM,CAAC,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;QAC/B,EAAE,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,+BAA+B,EAAE,CAAO,MAAM,EAAE,EAAE;YAC5F,MAAM,MAAM,GAAG,IAAA,sBAAa,EAAC,UAAU,CAAC,CAAC;YACzC,MAAM,WAAW,GAAG,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;YACxC,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,kBAAkB,EAAE,CAAC;YAEnE,MAAM,MAAM,CAAC,MAA6B,CAAC,CAAC,WAAW,CAAC,CAAC;YAEzD,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,kFAAkF;aACzI,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,7 +1,7 @@
1
1
  {
2
2
  "name": "@miketako3/cloki",
3
- "version": "0.1.11",
4
- "description": "cloki is logger library for Loki and Cloudflare Workers.",
3
+ "version": "0.1.12",
4
+ "description": "cloki is zero dependency and simple logger library for Loki and Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "files": [
7
7
  "dist"
@@ -13,7 +13,7 @@
13
13
  "build": "tsc",
14
14
  "format": "npx @biomejs/biome format . --write",
15
15
  "lint": "npx @biomejs/biome lint .",
16
- "test": "echo \"Error: no test specified\""
16
+ "test": "jest"
17
17
  },
18
18
  "repository": {
19
19
  "type": "git",
@@ -36,6 +36,9 @@
36
36
  "devDependencies": {
37
37
  "@biomejs/biome": "1.5.2",
38
38
  "@types/node": "^20.11.5",
39
- "typescript": "^5.3.3"
39
+ "typescript": "^5.3.3",
40
+ "@types/jest": "^29.5.11",
41
+ "ts-jest": "^29.1.1",
42
+ "jest": "^29.7.0"
40
43
  }
41
44
  }