@holz/json-backend 0.4.0 → 0.5.0

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.
@@ -1 +1 @@
1
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const o=require("node:os");class t{constructor(e){this.stream=e.stream}processLog(e){const s=JSON.stringify({level:e.level,time:new Date().toISOString(),msg:e.message,ctx:Object.keys(e.context).length>0?e.context:void 0});this.stream.write(`${s}${o.EOL}`)}}exports.JsonBackend=t;exports.default=t;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("node:os");function o({stream:t}){return e=>{const n=JSON.stringify({level:e.level,time:new Date().toISOString(),msg:e.message,ctx:Object.keys(e.context).length>0?e.context:void 0});t.write(`${n}${c.EOL}`)}}exports.createJsonBackend=o;
@@ -1,19 +1,15 @@
1
- import { EOL as s } from "node:os";
2
- class c {
3
- constructor(t) {
4
- this.stream = t.stream;
5
- }
6
- processLog(t) {
7
- const e = JSON.stringify({
1
+ import { EOL as r } from "node:os";
2
+ function i({ stream: e }) {
3
+ return (t) => {
4
+ const n = JSON.stringify({
8
5
  level: t.level,
9
6
  time: new Date().toISOString(),
10
7
  msg: t.message,
11
8
  ctx: Object.keys(t.context).length > 0 ? t.context : void 0
12
9
  });
13
- this.stream.write(`${e}${s}`);
14
- }
10
+ e.write(`${n}${r}`);
11
+ };
15
12
  }
16
13
  export {
17
- c as JsonBackend,
18
- c as default
14
+ i as createJsonBackend
19
15
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@holz/json-backend",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Print logs as newline-delimited JSON.",
5
5
  "type": "module",
6
6
  "main": "./dist/holz-json-backend.cjs",
@@ -39,10 +39,10 @@
39
39
  "test:types": "tsc"
40
40
  },
41
41
  "devDependencies": {
42
- "@holz/core": "0.2.0",
42
+ "@holz/core": "^0.5.0",
43
43
  "@types/node": "^18.14.0",
44
- "@vitest/coverage-c8": "0.28.5",
45
- "typescript": "4.9.5",
44
+ "@vitest/coverage-c8": "^0.28.5",
45
+ "typescript": "^4.9.5",
46
46
  "vite": "^4.0.0",
47
47
  "vitest": "^0.28.5"
48
48
  }
@@ -1,6 +1,6 @@
1
1
  import { Writable } from 'node:stream';
2
2
  import { createLogger } from '@holz/core';
3
- import JsonBackend from '../json-backend';
3
+ import { createJsonBackend } from '../json-backend';
4
4
 
5
5
  const CURRENT_TIME = new Date('2020-06-15T12:00:00.000Z');
6
6
 
@@ -32,7 +32,7 @@ describe('JSON backend', () => {
32
32
 
33
33
  it('prints the logs to the writable stream', () => {
34
34
  const { stream, getOutput } = createStream();
35
- const backend = new JsonBackend({ stream });
35
+ const backend = createJsonBackend({ stream });
36
36
 
37
37
  const logger = createLogger(backend);
38
38
  logger.debug('shout');
@@ -45,7 +45,7 @@ describe('JSON backend', () => {
45
45
 
46
46
  it('escapes newlines and carriage returns', () => {
47
47
  const { stream, getOutput } = createStream();
48
- const backend = new JsonBackend({ stream });
48
+ const backend = createJsonBackend({ stream });
49
49
  const logger = createLogger(backend);
50
50
 
51
51
  logger.debug('sneaky log\nwith newlines\rand carriage returns\r\n');
@@ -57,7 +57,7 @@ describe('JSON backend', () => {
57
57
 
58
58
  it('includes log context', () => {
59
59
  const { stream, getOutput } = createStream();
60
- const backend = new JsonBackend({ stream });
60
+ const backend = createJsonBackend({ stream });
61
61
  const logger = createLogger(backend);
62
62
 
63
63
  logger.debug('hello', { reqId: 'abc' });
package/src/index.ts CHANGED
@@ -1 +1 @@
1
- export { default, default as JsonBackend } from './json-backend';
1
+ export { createJsonBackend } from './json-backend';
@@ -7,20 +7,14 @@ import type { Log, LogProcessor } from '@holz/core';
7
7
  * log files.
8
8
  *
9
9
  * @example
10
- * new JsonBackend({
10
+ * createJsonBackend({
11
11
  * stream: fs.createWriteStream('my-app.log', { flags: 'a' }),
12
12
  * })
13
13
  *
14
14
  * @see http://ndjson.org
15
15
  */
16
- export default class JsonBackend implements LogProcessor {
17
- private stream: Writable;
18
-
19
- constructor(options: Config) {
20
- this.stream = options.stream;
21
- }
22
-
23
- processLog(log: Log) {
16
+ export function createJsonBackend({ stream }: Config): LogProcessor {
17
+ return (log: Log) => {
24
18
  // Follow the order of typical log statements. Be kind to the human
25
19
  // reader.
26
20
  const output = JSON.stringify({
@@ -36,8 +30,8 @@ export default class JsonBackend implements LogProcessor {
36
30
  // crashing the process.
37
31
  //
38
32
  // It is unlikely that a file or tty will apply backpressure in practice.
39
- this.stream.write(`${output}${EOL}`);
40
- }
33
+ stream.write(`${output}${EOL}`);
34
+ };
41
35
  }
42
36
 
43
37
  interface Config {