@holz/json-backend 0.5.0 → 0.7.0-rc.1
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
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# `@holz/json-backend`
|
|
2
|
+
|
|
3
|
+
Prints structured logs to a writable stream in NDJSON form.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { createJsonBackend } from '@holz/json-backend';
|
|
9
|
+
import { createLogger } from '@holz/core';
|
|
10
|
+
import * as fs from 'node:fs';
|
|
11
|
+
|
|
12
|
+
const logger = createLogger(
|
|
13
|
+
createJsonBackend({
|
|
14
|
+
stream: fs.createWriteStream('my-app.log', { flags: 'a' }),
|
|
15
|
+
})
|
|
16
|
+
);
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Logs are output in [NDJSON](http://ndjson.org/) format. The output is optimized for log files, following the order of typical log statements. The output includes the log level, timestamp, message, and context, if provided.
|
|
20
|
+
|
|
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`.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("node:os");function
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("node:os");function r({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=r;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { LogProcessor } from '@holz/core';
|
|
2
|
+
import { Writable } from 'node:stream';
|
|
3
|
+
|
|
4
|
+
declare interface Config {
|
|
5
|
+
/** Where to print logs. */
|
|
6
|
+
stream: Writable;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Prints structured logs to a writable stream in NDJSON form. Optimized for
|
|
11
|
+
* log files.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* createJsonBackend({
|
|
15
|
+
* stream: fs.createWriteStream('my-app.log', { flags: 'a' }),
|
|
16
|
+
* })
|
|
17
|
+
*
|
|
18
|
+
* @see http://ndjson.org
|
|
19
|
+
*/
|
|
20
|
+
export declare function createJsonBackend({ stream }: Config): LogProcessor;
|
|
21
|
+
|
|
22
|
+
export { }
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { EOL as r } from "node:os";
|
|
2
|
-
function i({ stream:
|
|
3
|
-
return (
|
|
2
|
+
function i({ stream: t }) {
|
|
3
|
+
return (e) => {
|
|
4
4
|
const n = JSON.stringify({
|
|
5
|
-
level:
|
|
6
|
-
time: new Date().toISOString(),
|
|
7
|
-
msg:
|
|
8
|
-
ctx: Object.keys(
|
|
5
|
+
level: e.level,
|
|
6
|
+
time: (/* @__PURE__ */ new Date()).toISOString(),
|
|
7
|
+
msg: e.message,
|
|
8
|
+
ctx: Object.keys(e.context).length > 0 ? e.context : void 0
|
|
9
9
|
});
|
|
10
|
-
|
|
10
|
+
t.write(`${n}${r}`);
|
|
11
11
|
};
|
|
12
12
|
}
|
|
13
13
|
export {
|
package/package.json
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@holz/json-backend",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0-rc.1",
|
|
4
4
|
"description": "Print logs as newline-delimited JSON.",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./dist/holz-json-backend.cjs",
|
|
7
|
-
"module": "./dist/holz-json-backend.js",
|
|
8
|
-
"types": "./src/index.ts",
|
|
9
6
|
"repository": {
|
|
10
7
|
"type": "git",
|
|
11
8
|
"url": "https://github.com/PsychoLlama/holz",
|
|
@@ -13,6 +10,7 @@
|
|
|
13
10
|
},
|
|
14
11
|
"exports": {
|
|
15
12
|
".": {
|
|
13
|
+
"types": "./dist/holz-json-backend.d.ts",
|
|
16
14
|
"require": "./dist/holz-json-backend.cjs",
|
|
17
15
|
"import": "./dist/holz-json-backend.js"
|
|
18
16
|
}
|
|
@@ -39,11 +37,14 @@
|
|
|
39
37
|
"test:types": "tsc"
|
|
40
38
|
},
|
|
41
39
|
"devDependencies": {
|
|
42
|
-
"@holz/core": "^0.
|
|
43
|
-
"@types/node": "^
|
|
44
|
-
"@vitest/coverage-
|
|
45
|
-
"typescript": "^
|
|
46
|
-
"vite": "^
|
|
47
|
-
"
|
|
48
|
-
|
|
40
|
+
"@holz/core": "^0.7.0-rc.1",
|
|
41
|
+
"@types/node": "^22.0.0",
|
|
42
|
+
"@vitest/coverage-v8": "^3.0.8",
|
|
43
|
+
"typescript": "^5.0.0",
|
|
44
|
+
"vite": "^6.0.0",
|
|
45
|
+
"vite-plugin-dts": "^4.5.3",
|
|
46
|
+
"vite-tsconfig-paths": "^5.1.4",
|
|
47
|
+
"vitest": "^3.0.8"
|
|
48
|
+
},
|
|
49
|
+
"stableVersion": "0.6.0"
|
|
49
50
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
// Vitest Snapshot v1
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
2
|
|
|
3
3
|
exports[`JSON backend > prints the logs to the writable stream 1`] = `
|
|
4
|
-
"{
|
|
5
|
-
{
|
|
6
|
-
{
|
|
7
|
-
{
|
|
4
|
+
"{"level":"debug","time":"2020-06-15T12:00:00.000Z","msg":"shout"}
|
|
5
|
+
{"level":"info","time":"2020-06-15T12:00:00.000Z","msg":"normal"}
|
|
6
|
+
{"level":"warn","time":"2020-06-15T12:00:00.000Z","msg":"hmmmm"}
|
|
7
|
+
{"level":"error","time":"2020-06-15T12:00:00.000Z","msg":"oh no"}
|
|
8
8
|
"
|
|
9
9
|
`;
|
|
@@ -51,7 +51,7 @@ describe('JSON backend', () => {
|
|
|
51
51
|
logger.debug('sneaky log\nwith newlines\rand carriage returns\r\n');
|
|
52
52
|
|
|
53
53
|
expect(getOutput()).toContain(
|
|
54
|
-
'sneaky log\\nwith newlines\\rand carriage returns\\r\\n'
|
|
54
|
+
'sneaky log\\nwith newlines\\rand carriage returns\\r\\n',
|
|
55
55
|
);
|
|
56
56
|
});
|
|
57
57
|
|