@holz/json-backend 0.9.2-rc.6.sha-ff7e9bc → 0.9.2-rc.60.sha-9c2a088
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 +21 -1
- package/dist/holz-json-backend.cjs +1 -1
- package/dist/holz-json-backend.js +5 -19
- package/dist/marshal-Dq-F5gZZ.cjs +1 -0
- package/dist/marshal-nT1onzi1.js +20 -0
- package/dist/marshal.cjs +1 -0
- package/dist/marshal.d.ts +20 -0
- package/dist/marshal.js +1 -0
- package/package.json +10 -4
- package/src/json-backend.ts +3 -45
- package/src/marshal/index.ts +1 -0
- package/src/marshal/marshal.ts +84 -0
package/README.md
CHANGED
|
@@ -20,7 +20,27 @@ Logs are output in [NDJSON](https://github.com/ndjson/ndjson-spec) format. The o
|
|
|
20
20
|
|
|
21
21
|
The `stream` option specifies where the logs will be written to. You can use any writable stream, such as a file or `process.stdout`.
|
|
22
22
|
|
|
23
|
-
## Log
|
|
23
|
+
## Changes to `Log`
|
|
24
|
+
|
|
25
|
+
This plugin converts a few fields to make the JSON representation useful and readable:
|
|
26
|
+
|
|
27
|
+
- `log.level`: Replaced with a string mirroring its key in [level](https://github.com/PsychoLlama/holz/tree/main/packages/holz-core#log-levels).
|
|
28
|
+
- `log.timestamp`: Converted to an ISO 8601 date string.
|
|
29
|
+
- `log.context.error`: Serialized as plain values, with recursive handling for nested [cause](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) and [errors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError/errors).
|
|
30
|
+
|
|
31
|
+
Conversion tools are exported separately and can be used in other environments outside NodeJS.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { toJSON, errorToJSON } from '@holz/json-backend/marshal';
|
|
35
|
+
|
|
36
|
+
// Convert a log to a JSON-suitable object.
|
|
37
|
+
JSON.stringify(toJSON(log));
|
|
38
|
+
|
|
39
|
+
// Convert an error on its own, for errors outside `context.error`.
|
|
40
|
+
errorToJSON(new Error('Boom', { cause: new Error('Root cause') }));
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Rotation and Compression
|
|
24
44
|
|
|
25
45
|
Long-lived processes or noisy services can fill up the disk, given enough time.
|
|
26
46
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./marshal-Dq-F5gZZ.cjs");var t=({stream:t})=>n=>{t.write(`${JSON.stringify(e.n(n))}\n`)};exports.createJsonBackend=t;
|
|
@@ -1,20 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
var t = ({ stream:
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
time: new Date(t.timestamp).toISOString(),
|
|
7
|
-
msg: t.message,
|
|
8
|
-
origin: t.origin,
|
|
9
|
-
ctx: Object.keys(t.context).length > 0 ? t.context : void 0
|
|
10
|
-
}, n);
|
|
11
|
-
e.write(`${i}\n`);
|
|
12
|
-
}, n = (e, t) => t instanceof Error ? {
|
|
13
|
-
...t,
|
|
14
|
-
name: t.name,
|
|
15
|
-
message: t.message,
|
|
16
|
-
cause: t.cause,
|
|
17
|
-
...t instanceof AggregateError && { errors: t.errors }
|
|
18
|
-
} : t, r = Object.fromEntries(Object.entries(e).map(([e, t]) => [t, e]));
|
|
19
|
-
//#endregion
|
|
1
|
+
import { n as e } from "./marshal-nT1onzi1.js";
|
|
2
|
+
// src/json-backend.ts
|
|
3
|
+
var t = ({ stream: t }) => (n) => {
|
|
4
|
+
t.write(`${JSON.stringify(e(n))}\n`);
|
|
5
|
+
};
|
|
20
6
|
export { t as createJsonBackend };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
let e=require("@holz/core");var t=e=>({level:a[e.level],timestamp:new Date(e.timestamp).toISOString(),message:e.message,origin:e.origin,context:i(e.context)}),n=e=>({...e,name:e.name,message:e.message,cause:r(e.cause),errors:r(e.errors),stack:e.stack}),r=e=>e instanceof Error?n(e):Array.isArray(e)?e.map(r):e,i=e=>({...e,error:r(e.error)}),a=Object.fromEntries(Object.entries(e.level).map(([e,t])=>[t,e]));Object.defineProperty(exports,"n",{enumerable:!0,get:function(){return t}}),Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return n}});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { level as e } from "@holz/core";
|
|
2
|
+
// src/marshal/marshal.ts
|
|
3
|
+
var t = (e) => ({
|
|
4
|
+
level: a[e.level],
|
|
5
|
+
timestamp: new Date(e.timestamp).toISOString(),
|
|
6
|
+
message: e.message,
|
|
7
|
+
origin: e.origin,
|
|
8
|
+
context: i(e.context)
|
|
9
|
+
}), n = (e) => ({
|
|
10
|
+
...e,
|
|
11
|
+
name: e.name,
|
|
12
|
+
message: e.message,
|
|
13
|
+
cause: r(e.cause),
|
|
14
|
+
errors: r(e.errors),
|
|
15
|
+
stack: e.stack
|
|
16
|
+
}), r = (e) => e instanceof Error ? n(e) : Array.isArray(e) ? e.map(r) : e, i = (e) => ({
|
|
17
|
+
...e,
|
|
18
|
+
error: r(e.error)
|
|
19
|
+
}), a = Object.fromEntries(Object.entries(e).map(([e, t]) => [t, e]));
|
|
20
|
+
export { t as n, n as t };
|
package/dist/marshal.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./marshal-Dq-F5gZZ.cjs");exports.errorToJSON=e.t,exports.toJSON=e.n;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Log } from '@holz/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Best-effort attempt at converting `Error` instances to serializable JSON.
|
|
5
|
+
*
|
|
6
|
+
* These are among the most important logs, but the default behavior produces
|
|
7
|
+
* an empty object.
|
|
8
|
+
*
|
|
9
|
+
* This is NOT a lossless conversion. Values are too dynamic, especially
|
|
10
|
+
* considering custom errors. But it should handle the vast majority.
|
|
11
|
+
*/
|
|
12
|
+
export declare const errorToJSON: (error: Error) => Record<string, unknown>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Converts a log to a JSON-serializable object.
|
|
16
|
+
* Keys ordered to make visually skimming a log file easier.
|
|
17
|
+
*/
|
|
18
|
+
export declare const toJSON: (log: Log) => Record<string, unknown>;
|
|
19
|
+
|
|
20
|
+
export { }
|
package/dist/marshal.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as errorToJSON, n as toJSON } from "./marshal-nT1onzi1.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@holz/json-backend",
|
|
3
|
-
"version": "0.9.2-rc.
|
|
3
|
+
"version": "0.9.2-rc.60.sha-9c2a088",
|
|
4
4
|
"description": "Print logs as newline-delimited JSON.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -14,6 +14,12 @@
|
|
|
14
14
|
"types": "./dist/holz-json-backend.d.ts",
|
|
15
15
|
"require": "./dist/holz-json-backend.cjs",
|
|
16
16
|
"import": "./dist/holz-json-backend.js"
|
|
17
|
+
},
|
|
18
|
+
"./marshal": {
|
|
19
|
+
"source": "./src/marshal/index.ts",
|
|
20
|
+
"types": "./dist/marshal.d.ts",
|
|
21
|
+
"require": "./dist/marshal.cjs",
|
|
22
|
+
"import": "./dist/marshal.js"
|
|
17
23
|
}
|
|
18
24
|
},
|
|
19
25
|
"publishConfig": {
|
|
@@ -43,9 +49,9 @@
|
|
|
43
49
|
"@holz/core": "^0.9.0"
|
|
44
50
|
},
|
|
45
51
|
"devDependencies": {
|
|
46
|
-
"@holz/core": "^0.9.2-rc.
|
|
52
|
+
"@holz/core": "^0.9.2-rc.60.sha-9c2a088",
|
|
47
53
|
"@microsoft/api-extractor": "^7.58.8",
|
|
48
|
-
"@types/node": "^
|
|
54
|
+
"@types/node": "^26.0.0",
|
|
49
55
|
"@vitest/coverage-v8": "^5.0.0",
|
|
50
56
|
"typescript": "^6.0.0",
|
|
51
57
|
"vite": "^8.0.0",
|
|
@@ -53,5 +59,5 @@
|
|
|
53
59
|
"vite-tsconfig-paths": "^6.0.0",
|
|
54
60
|
"vitest": "^5.0.0"
|
|
55
61
|
},
|
|
56
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "9c2a0885dda76cf5734dd4861809caa6754ef100"
|
|
57
63
|
}
|
package/src/json-backend.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Writable } from 'node:stream';
|
|
2
|
-
import
|
|
2
|
+
import type { Log, LogProcessor } from '@holz/core';
|
|
3
|
+
import { toJSON } from './marshal';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Prints structured logs to a writable stream in NDJSON form. Optimized for
|
|
@@ -14,59 +15,16 @@ import { level, type LogLevel, type Log, type LogProcessor } from '@holz/core';
|
|
|
14
15
|
*/
|
|
15
16
|
export const createJsonBackend = ({ stream }: Config): LogProcessor => {
|
|
16
17
|
return (log: Log) => {
|
|
17
|
-
// Follow the order of typical log statements. Be kind to the human
|
|
18
|
-
// reader.
|
|
19
|
-
const output = JSON.stringify(
|
|
20
|
-
{
|
|
21
|
-
level: labelForLevel[log.level],
|
|
22
|
-
time: new Date(log.timestamp).toISOString(),
|
|
23
|
-
msg: log.message,
|
|
24
|
-
origin: log.origin,
|
|
25
|
-
ctx: Object.keys(log.context).length > 0 ? log.context : undefined,
|
|
26
|
-
},
|
|
27
|
-
errorSerializer,
|
|
28
|
-
);
|
|
29
|
-
|
|
30
18
|
// NOTE: If the stream applies backpressure, we will lose logs. I believe
|
|
31
19
|
// this is the right tradeoff. We can't prevent the app from generating
|
|
32
20
|
// more logs, and if we buffered it would risk running out of memory and
|
|
33
21
|
// crashing the process.
|
|
34
22
|
//
|
|
35
23
|
// It is unlikely that a file or tty will apply backpressure in practice.
|
|
36
|
-
stream.write(`${
|
|
24
|
+
stream.write(`${JSON.stringify(toJSON(log))}\n`);
|
|
37
25
|
};
|
|
38
26
|
};
|
|
39
27
|
|
|
40
|
-
/**
|
|
41
|
-
* Errors are not JSON serializable, but errors are naturally a crucial aspect
|
|
42
|
-
* of any logging system. This unwraps errors into a JSON representation.
|
|
43
|
-
*/
|
|
44
|
-
const errorSerializer = (_key: string, value: unknown) => {
|
|
45
|
-
if (value instanceof Error) {
|
|
46
|
-
return {
|
|
47
|
-
...value, // Some custom errors have important custom properties.
|
|
48
|
-
name: value.name,
|
|
49
|
-
message: value.message,
|
|
50
|
-
cause: value.cause,
|
|
51
|
-
// `AggregateError` collects its constituent failures in a
|
|
52
|
-
// non-enumerable `errors` array that the spread above misses. Pull it
|
|
53
|
-
// explicitly so those underlying errors survive serialization; the
|
|
54
|
-
// replacer recurses into each one.
|
|
55
|
-
...(value instanceof AggregateError && { errors: value.errors }),
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return value;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Slightly heavier than logging the number, but easier to process
|
|
64
|
-
* programmatically which is somewhat implied by a JSON backend.
|
|
65
|
-
*/
|
|
66
|
-
const labelForLevel = Object.fromEntries(
|
|
67
|
-
Object.entries(level).map(([key, value]) => [value, key]),
|
|
68
|
-
) as Record<LogLevel, string>;
|
|
69
|
-
|
|
70
28
|
interface Config {
|
|
71
29
|
/** Where to print logs. */
|
|
72
30
|
stream: Writable;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { toJSON, errorToJSON } from './marshal';
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { level, type LogLevel, type Log, type LogContext } from '@holz/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts a log to a JSON-serializable object.
|
|
5
|
+
* Keys ordered to make visually skimming a log file easier.
|
|
6
|
+
*/
|
|
7
|
+
export const toJSON = (log: Log): Record<string, unknown> => {
|
|
8
|
+
return {
|
|
9
|
+
// Convert the numeric level to a label. Lossless, as keys map back to
|
|
10
|
+
// `level`. Improves readability and use with tools like `jq`.
|
|
11
|
+
level: labelForLevel[log.level],
|
|
12
|
+
|
|
13
|
+
// Converted purely for human readability. `jq` doesn't have millis-level
|
|
14
|
+
// date precision, so optimizing for tools is somewhat moot.
|
|
15
|
+
timestamp: new Date(log.timestamp).toISOString(),
|
|
16
|
+
|
|
17
|
+
// No changes needed here.
|
|
18
|
+
message: log.message,
|
|
19
|
+
origin: log.origin,
|
|
20
|
+
|
|
21
|
+
// Everything in context is meant to be JSON-serializable with the
|
|
22
|
+
// exception (heh) of errors. Attempt a naive conversion.
|
|
23
|
+
context: contextToJSON(log.context),
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Best-effort attempt at converting `Error` instances to serializable JSON.
|
|
29
|
+
*
|
|
30
|
+
* These are among the most important logs, but the default behavior produces
|
|
31
|
+
* an empty object.
|
|
32
|
+
*
|
|
33
|
+
* This is NOT a lossless conversion. Values are too dynamic, especially
|
|
34
|
+
* considering custom errors. But it should handle the vast majority.
|
|
35
|
+
*/
|
|
36
|
+
export const errorToJSON = (error: Error): Record<string, unknown> => ({
|
|
37
|
+
...error, // Some custom errors have important custom properties.
|
|
38
|
+
|
|
39
|
+
name: error.name,
|
|
40
|
+
message: error.message,
|
|
41
|
+
cause: errorFieldToJSON(error.cause),
|
|
42
|
+
|
|
43
|
+
// Special case for `AggregateError` which carries a list of errors. Well,
|
|
44
|
+
// we hope they're errors. It's not like JS enforces it.
|
|
45
|
+
errors: errorFieldToJSON((error as AggregateError).errors),
|
|
46
|
+
|
|
47
|
+
// Invaluable when needed, but costly in terms of noise.
|
|
48
|
+
stack: error.stack,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const errorFieldToJSON = (value: unknown): unknown => {
|
|
52
|
+
// Example: `Error(...).cause`
|
|
53
|
+
if (value instanceof Error) {
|
|
54
|
+
return errorToJSON(value);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Example: `AggregateError(...).errors`
|
|
58
|
+
if (Array.isArray(value)) {
|
|
59
|
+
return value.map(errorFieldToJSON);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Pass through unremarked.
|
|
63
|
+
return value;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Types require `context.error` to be an `Error` instance. Everything else is
|
|
68
|
+
* restricted to JSON values.
|
|
69
|
+
*
|
|
70
|
+
* `errorToJSON` is exported as an escape hatch. Internally, we only handle
|
|
71
|
+
* `context.error`. Consumers with more advanced cases have to eject.
|
|
72
|
+
*/
|
|
73
|
+
const contextToJSON = (context: LogContext) => ({
|
|
74
|
+
...context,
|
|
75
|
+
error: errorFieldToJSON(context.error),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Slightly heavier than logging the number, but easier to process
|
|
80
|
+
* programmatically which is somewhat implied by a JSON backend.
|
|
81
|
+
*/
|
|
82
|
+
const labelForLevel = Object.fromEntries(
|
|
83
|
+
Object.entries(level).map(([key, value]) => [value, key]),
|
|
84
|
+
) as Record<LogLevel, string>;
|