@holz/json-backend 0.9.2-rc.57.sha-68b9bd0 → 0.9.2-rc.59.sha-3abcf4b
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 +22 -0
- package/dist/holz-json-backend.cjs +1 -1
- package/dist/holz-json-backend.js +4 -17
- package/dist/serialize-68-JE9vM.cjs +1 -0
- package/dist/serialize-Bc_zRhA0.js +19 -0
- package/dist/serialize.cjs +1 -0
- package/dist/serialize.d.ts +9 -0
- package/dist/serialize.js +1 -0
- package/package.json +10 -4
- package/src/json-backend.ts +3 -45
- package/src/serialize/index.ts +1 -0
- package/src/serialize/serialize.ts +56 -0
package/README.md
CHANGED
|
@@ -25,3 +25,25 @@ The `stream` option specifies where the logs will be written to. You can use any
|
|
|
25
25
|
Long-lived processes or noisy services can fill up the disk, given enough time.
|
|
26
26
|
|
|
27
27
|
Lean on your environment's tools (`journalctl`) or use a package like [rotating-file-stream](https://github.com/iccicci/rotating-file-stream) to prevent files from growing unbounded.
|
|
28
|
+
|
|
29
|
+
## Custom Destinations
|
|
30
|
+
|
|
31
|
+
The default backend assumes a NodeJS environment. For other envs, use the JSON serializer exported from `/serialize`.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import serialize from '@holz/json-backend/serialize';
|
|
35
|
+
|
|
36
|
+
// Stream logs as NDJSON.
|
|
37
|
+
const jsonStream = new ReadableStream<Uint8Array>({
|
|
38
|
+
async start(controller) {
|
|
39
|
+
const encoder = new TextEncoder();
|
|
40
|
+
|
|
41
|
+
for await (const log of readLogStream()) {
|
|
42
|
+
const encoded = encoder.encode(`${serialize(log)}\n`);
|
|
43
|
+
controller.enqueue(encoded);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
controller.close();
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./serialize-68-JE9vM.cjs");var t=({stream:t})=>n=>{t.write(`${e.t(n)}\n`)};exports.createJsonBackend=t;
|
|
@@ -1,19 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as e } from "./serialize-Bc_zRhA0.js";
|
|
2
2
|
// src/json-backend.ts
|
|
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]));
|
|
3
|
+
var t = ({ stream: t }) => (n) => {
|
|
4
|
+
t.write(`${e(n)}\n`);
|
|
5
|
+
};
|
|
19
6
|
export { t as createJsonBackend };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
let e=require("@holz/core");var t=e=>JSON.stringify({level:a[e.level],time:new Date(e.timestamp).toISOString(),msg:e.message,origin:e.origin,ctx:Object.keys(e.context).length>0?n(e.context):void 0}),n=e=>e.error instanceof Error?{...e,error:r(e.error)}:e,r=e=>({...e,name:e.name,message:e.message,cause:i(e.cause),...e instanceof AggregateError&&{errors:e.errors.map(i)}}),i=e=>e instanceof Error?r(e):e,a=Object.fromEntries(Object.entries(e.level).map(([e,t])=>[t,e]));Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return t}});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { level as e } from "@holz/core";
|
|
2
|
+
// src/serialize/serialize.ts
|
|
3
|
+
var t = (e) => JSON.stringify({
|
|
4
|
+
level: a[e.level],
|
|
5
|
+
time: new Date(e.timestamp).toISOString(),
|
|
6
|
+
msg: e.message,
|
|
7
|
+
origin: e.origin,
|
|
8
|
+
ctx: Object.keys(e.context).length > 0 ? n(e.context) : void 0
|
|
9
|
+
}), n = (e) => e.error instanceof Error ? {
|
|
10
|
+
...e,
|
|
11
|
+
error: r(e.error)
|
|
12
|
+
} : e, r = (e) => ({
|
|
13
|
+
...e,
|
|
14
|
+
name: e.name,
|
|
15
|
+
message: e.message,
|
|
16
|
+
cause: i(e.cause),
|
|
17
|
+
...e instanceof AggregateError && { errors: e.errors.map(i) }
|
|
18
|
+
}), i = (e) => e instanceof Error ? r(e) : e, a = Object.fromEntries(Object.entries(e).map(([e, t]) => [t, e]));
|
|
19
|
+
export { t };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});const e=require("./serialize-68-JE9vM.cjs");exports.default=e.t;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as default } from "./serialize-Bc_zRhA0.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.59.sha-3abcf4b",
|
|
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
|
+
"./serialize": {
|
|
19
|
+
"source": "./src/serialize/index.ts",
|
|
20
|
+
"types": "./dist/serialize.d.ts",
|
|
21
|
+
"require": "./dist/serialize.cjs",
|
|
22
|
+
"import": "./dist/serialize.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.59.sha-3abcf4b",
|
|
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": "3abcf4bdde93448d03c4d0d8d9d937710db7de54"
|
|
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 serialize from './serialize';
|
|
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(`${serialize(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 { serialize as default } from './serialize';
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { level, type LogLevel, type Log, type LogContext } from '@holz/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Serializes a log as a single line of JSON (without the trailing newline).
|
|
5
|
+
*/
|
|
6
|
+
export const serialize = (log: Log): string => {
|
|
7
|
+
// Follow the order of typical log statements. Be kind to the human
|
|
8
|
+
// reader.
|
|
9
|
+
return JSON.stringify({
|
|
10
|
+
level: labelForLevel[log.level],
|
|
11
|
+
time: new Date(log.timestamp).toISOString(),
|
|
12
|
+
msg: log.message,
|
|
13
|
+
origin: log.origin,
|
|
14
|
+
ctx:
|
|
15
|
+
Object.keys(log.context).length > 0
|
|
16
|
+
? serializeContext(log.context)
|
|
17
|
+
: undefined,
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Errors are not JSON serializable, but errors are naturally a crucial aspect
|
|
23
|
+
* of any logging system. Only `context.error` is treated as one (per
|
|
24
|
+
* `CustomContext`); errors anywhere else are left to `JSON.stringify`.
|
|
25
|
+
*/
|
|
26
|
+
const serializeContext = (context: LogContext) => {
|
|
27
|
+
if (context.error instanceof Error) {
|
|
28
|
+
return { ...context, error: serializeError(context.error) };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return context;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** Unwraps an error, its cause chain, and any aggregated errors. */
|
|
35
|
+
const serializeError = (error: Error): Record<string, unknown> => ({
|
|
36
|
+
...error, // Some custom errors have important custom properties.
|
|
37
|
+
name: error.name,
|
|
38
|
+
message: error.message,
|
|
39
|
+
cause: unwrap(error.cause),
|
|
40
|
+
|
|
41
|
+
// `AggregateError` collects its constituent failures in a non-enumerable
|
|
42
|
+
// `errors` array that the spread above misses. Pull it explicitly so those
|
|
43
|
+
// underlying errors survive serialization.
|
|
44
|
+
...(error instanceof AggregateError && { errors: error.errors.map(unwrap) }),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const unwrap = (value: unknown) =>
|
|
48
|
+
value instanceof Error ? serializeError(value) : value;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Slightly heavier than logging the number, but easier to process
|
|
52
|
+
* programmatically which is somewhat implied by a JSON backend.
|
|
53
|
+
*/
|
|
54
|
+
const labelForLevel = Object.fromEntries(
|
|
55
|
+
Object.entries(level).map(([key, value]) => [value, key]),
|
|
56
|
+
) as Record<LogLevel, string>;
|