@vllnt/logger 0.1.3-canary.4a30380 → 0.1.3-canary.7d9ec44
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/CHANGELOG.md +31 -2
- package/README.md +133 -121
- package/dist/{chunk-ZXUJ3U24.js → chunk-OROSOSC5.js} +14 -8
- package/dist/convex.d.ts +1 -1
- package/dist/convex.js +1 -1
- package/dist/{core-he_ASnDU.d.ts → core-CzlOE0nJ.d.ts} +2 -2
- package/dist/index.d.ts +8 -3
- package/dist/index.js +1 -1
- package/docs/API.md +20 -5
- package/llms.txt +26 -8
- package/package.json +24 -8
- package/src/backend.ts +6 -1
- package/src/convex.ts +1 -1
- package/src/core.ts +26 -10
- package/src/index.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -4,7 +4,7 @@ All notable changes to this project are documented here. The format follows
|
|
|
4
4
|
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses
|
|
5
5
|
[Semantic Versioning](https://semver.org/).
|
|
6
6
|
|
|
7
|
-
## [0.1.3] -
|
|
7
|
+
## [0.1.3] - 2026-11-27
|
|
8
8
|
|
|
9
9
|
### Added
|
|
10
10
|
|
|
@@ -12,7 +12,7 @@ All notable changes to this project are documented here. The format follows
|
|
|
12
12
|
`CHANGELOG.md` and the TypeScript sources in `src/`, so coding agents can read docs that match
|
|
13
13
|
the installed version from `node_modules/@vllnt/logger/`. `llms.txt` links the shipped files
|
|
14
14
|
with relative paths.
|
|
15
|
-
- README notes the bundled docs for coding agents
|
|
15
|
+
- README notes the bundled docs for coding agents.
|
|
16
16
|
- `package.json` sets `homepage` and `keywords`. Docs-only changes on `main` now publish a canary.
|
|
17
17
|
|
|
18
18
|
### Fixed
|
|
@@ -28,6 +28,14 @@ All notable changes to this project are documented here. The format follows
|
|
|
28
28
|
- `createPostHogOutput` swallows rejected promises returned by `capture` instead of leaving an
|
|
29
29
|
unhandled rejection. `PostHogCapture` now accepts functions returning any value.
|
|
30
30
|
- `withTiming` logs a placeholder when a thrown value cannot be converted to a string.
|
|
31
|
+
- `withTiming` and `withTimingSync` no longer throw when `extraData` has a throwing getter: they
|
|
32
|
+
return the result or rethrow the original error, and drop only the entries that cannot be built.
|
|
33
|
+
- `docs/API.md`: the posthog-node wrapper now type-checks under `exactOptionalPropertyTypes`
|
|
34
|
+
(`(event, properties = {}) => client.capture({ distinctId, event, properties })`), and the
|
|
35
|
+
placeholder note names every placeholder. Tests check the entry-point table against the real
|
|
36
|
+
exports.
|
|
37
|
+
- The publish workflow builds before it runs the tests, so the check of the built package files
|
|
38
|
+
runs before every publish instead of being skipped.
|
|
31
39
|
|
|
32
40
|
### Changed
|
|
33
41
|
|
|
@@ -37,6 +45,27 @@ All notable changes to this project are documented here. The format follows
|
|
|
37
45
|
pinned to commit SHAs, the workflow token is read-only by default, and pnpm is pinned to 10.34.8
|
|
38
46
|
with a seven-day minimum release age.
|
|
39
47
|
- README documents the release flow, serialization behaviour and that there is no built-in redaction.
|
|
48
|
+
- README follows the section layout shared by @vllnt packages, for coding agents: when to use,
|
|
49
|
+
quick start, an API table of every export, common tasks, errors and fixes, limits and gotchas,
|
|
50
|
+
and compatibility. Every TypeScript example is complete and is type-checked against the built
|
|
51
|
+
package in tests; examples without third-party imports also run, and the output they show in
|
|
52
|
+
`// console.` comments is compared with what they print. `llms.txt` carries the same summary
|
|
53
|
+
and links to the package page.
|
|
54
|
+
- README adds a `## Links` section (package page, release feed, package index and search, source,
|
|
55
|
+
issues, npm, community, author and sponsor links). Its `## Related packages` section states that
|
|
56
|
+
the package has no runtime or peer dependencies and that no other @vllnt package shares code, a
|
|
57
|
+
test or an example with it. `llms.txt` ends with the shared ecosystem block, which adds the
|
|
58
|
+
release feed and package search links.
|
|
59
|
+
- `docs/API.md` examples (the code blocks that import a package) are type-checked against the
|
|
60
|
+
built package, with posthog-node 6 for the posthog-node wrapper; examples that show output in
|
|
61
|
+
`// console.` comments also run and are compared with it.
|
|
62
|
+
- `package.json`: new `description` and `keywords`, `author` as an object, and new `funding` and
|
|
63
|
+
`bugs` fields. `createBackendLogger` TSDoc links the package page.
|
|
64
|
+
- Built with tsup 9 and TypeScript 6. The `.d.ts` files of the subpaths and the shared core use
|
|
65
|
+
`export type { … }`, and the subpath declarations import shared types from `./index.js`. The
|
|
66
|
+
toolchain change does not change the types consumers see.
|
|
67
|
+
- Development only: `@vllnt/typescript` 3 (stricter compiler flags; published output unchanged),
|
|
68
|
+
`vitest` 5.1.1, and `pnpm test:coverage`.
|
|
40
69
|
|
|
41
70
|
## [0.1.2] - 2026-03-10
|
|
42
71
|
|
package/README.md
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
# @vllnt/logger
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Zero-dependency structured JSON logger for TypeScript with scoped events, log levels, timing, and console, PostHog and test outputs for Node.js and Convex.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## For coding agents
|
|
6
|
+
|
|
7
|
+
`node_modules/@vllnt/logger/docs/API.md` documents every export with its signature, and the TypeScript
|
|
8
|
+
sources ship in `node_modules/@vllnt/logger/src/`. Both match the installed version.
|
|
9
|
+
|
|
10
|
+
## When to use
|
|
11
|
+
|
|
12
|
+
- Use it when you want one JSON line per event, with a scoped event name (`auth.login`), from TypeScript.
|
|
13
|
+
- Use it when the same logger API must run in Node.js and in Convex queries, mutations and actions.
|
|
14
|
+
- Use it when log entries should go to several outputs, such as the console and PostHog.
|
|
15
|
+
- Use it when tests should assert on log entries instead of console output.
|
|
16
|
+
- Do not use it when you need log transports, files, rotation, redaction or async batching: use [pino](https://github.com/pinojs/pino) or [winston](https://github.com/winstonjs/winston).
|
|
17
|
+
- Do not use it when entries must be stored and queried later: use [`@vllnt/convex-events`](https://oss.vllnt.com/packages/convex-events).
|
|
18
|
+
- Do not use it for product analytics with consent: use [`@vllnt/analytics`](https://oss.vllnt.com/packages/analytics).
|
|
6
19
|
|
|
7
20
|
## Install
|
|
8
21
|
|
|
@@ -10,188 +23,187 @@ Zero runtime dependencies. Tree-shakeable. Works in Node.js, Convex, browser, an
|
|
|
10
23
|
pnpm add @vllnt/logger
|
|
11
24
|
```
|
|
12
25
|
|
|
13
|
-
##
|
|
14
|
-
|
|
15
|
-
The docs are bundled in the package: `node_modules/@vllnt/logger/docs/API.md` (every export,
|
|
16
|
-
its signature and an example), `llms.txt`, `CHANGELOG.md` and the TypeScript sources in
|
|
17
|
-
`node_modules/@vllnt/logger/src/`. They match the installed version. Read them before writing code.
|
|
18
|
-
|
|
19
|
-
## Quick Start
|
|
26
|
+
## Quick start
|
|
20
27
|
|
|
21
28
|
```ts
|
|
22
29
|
import { createBackendLogger } from "@vllnt/logger";
|
|
23
30
|
|
|
24
31
|
const logger = createBackendLogger("auth");
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
// → {"event":"auth.login","level":"info","timestamp":"...","userId":"123"}
|
|
28
|
-
|
|
29
|
-
logger.warn("rate-limited", { ip: "1.2.3.4" });
|
|
32
|
+
logger.warn("login.failed", { userId: "123" });
|
|
33
|
+
// console.warn: {"userId":"123","event":"auth.login.failed","level":"warn","timestamp":"2026-11-27T12:00:00.000Z"}
|
|
30
34
|
```
|
|
31
35
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
## Subpath Exports
|
|
35
|
-
|
|
36
|
-
| Import | Use Case |
|
|
37
|
-
| ----------------------- | ------------------------------------- |
|
|
38
|
-
| `@vllnt/logger` | Core API + Node.js backend preset |
|
|
39
|
-
| `@vllnt/logger/convex` | Convex-safe logger (no `process.env`) |
|
|
40
|
-
| `@vllnt/logger/posthog` | PostHog output adapter |
|
|
41
|
-
| `@vllnt/logger/testing` | In-memory test output for assertions |
|
|
36
|
+
`createBackendLogger` writes `warn` and `error` by default. Set `LOG_LEVEL=info` or `LOG_LEVEL=debug` to see more.
|
|
42
37
|
|
|
43
38
|
## API
|
|
44
39
|
|
|
45
|
-
Full reference: [docs/API.md](docs/API.md).
|
|
40
|
+
Full reference with signatures and examples: [docs/API.md](docs/API.md).
|
|
41
|
+
|
|
42
|
+
| Export | Import from | What it does |
|
|
43
|
+
| --- | --- | --- |
|
|
44
|
+
| [`createBackendLogger`](docs/API.md#createbackendloggerscope-string-extendedlogger) | `@vllnt/logger` | Node.js logger: level from `LOG_LEVEL` (default `warn`), writes with `consoleOutput` |
|
|
45
|
+
| [`createLogger`](docs/API.md#createloggerscope-string-config-loggerconfig-logger) | `@vllnt/logger` | Logger with your own `getLogLevel` and `output` |
|
|
46
|
+
| [`createExtendedLogger`](docs/API.md#createextendedloggerscope-string-config-loggerconfig-extendedlogger) | `@vllnt/logger` | `createLogger` plus `withTiming` and `withTimingSync` |
|
|
47
|
+
| [`consoleOutput`](docs/API.md#consoleoutputentry-logentry-void) | `@vllnt/logger` | Output that writes one JSON line to `console.debug/info/warn/error` |
|
|
48
|
+
| [`composeOutputs`](docs/API.md#composeoutputsoutputs-logoutput-logoutput) | `@vllnt/logger` | Combines outputs; a throwing output does not stop the others |
|
|
49
|
+
| [`formatLogEntry`](docs/API.md#formatlogentrylevel-loglevel-event-string-data-logdata-logentry) | `@vllnt/logger` | Builds a `LogEntry` without a scope prefix |
|
|
50
|
+
| [`parseLogLevel`](docs/API.md#parseloglevelvalue-string--undefined-defaultlevel-loglevel-loglevel) | `@vllnt/logger` | Parses a string to a `LogLevel`, with a default |
|
|
51
|
+
| [`LogLevel`](docs/API.md#types) | `@vllnt/logger` | Type: `"debug" \| "info" \| "warn" \| "error"` |
|
|
52
|
+
| [`LogData`](docs/API.md#types) | `@vllnt/logger` | Type: `Record<string, unknown>` passed to a log call |
|
|
53
|
+
| [`LogEntry`](docs/API.md#types) | `@vllnt/logger` | Type: `{ event, level, timestamp }` plus your data |
|
|
54
|
+
| [`LogOutput`](docs/API.md#types) | `@vllnt/logger` | Type: `(entry: LogEntry) => void` |
|
|
55
|
+
| [`Logger`](docs/API.md#types) | `@vllnt/logger` | Type: `debug`, `info`, `warn`, `error` methods |
|
|
56
|
+
| [`ExtendedLogger`](docs/API.md#types) | `@vllnt/logger` | Type: `Logger` plus `withTiming`, `withTimingSync` |
|
|
57
|
+
| [`LoggerConfig`](docs/API.md#types) | `@vllnt/logger` | Type: `{ getLogLevel, output }` |
|
|
58
|
+
| [`createConvexLogger`](docs/API.md#createconvexloggerscope-string-level-loglevel-extendedlogger) | `@vllnt/logger/convex` | Convex logger with a fixed level (default `info`); no `process.env` |
|
|
59
|
+
| [`createPostHogOutput`](docs/API.md#createposthogoutputcapture-posthogcapture-logoutput) | `@vllnt/logger/posthog` | Output that calls your PostHog `capture` function |
|
|
60
|
+
| [`PostHogCapture`](docs/API.md#posthogcapture) | `@vllnt/logger/posthog` | Type: `(event, properties?) => unknown` |
|
|
61
|
+
| [`createTestOutput`](docs/API.md#createtestoutput-testoutput) | `@vllnt/logger/testing` | Output that stores entries in memory for assertions |
|
|
62
|
+
| [`TestOutput`](docs/API.md#testoutput) | `@vllnt/logger/testing` | Type: `{ entries, output }` |
|
|
63
|
+
|
|
64
|
+
## Common tasks
|
|
65
|
+
|
|
66
|
+
Set the level of `createBackendLogger` at runtime (read on every log call):
|
|
46
67
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
Pre-configured for Node.js. Reads `LOG_LEVEL` from `process.env` lazily on each log call.
|
|
50
|
-
|
|
51
|
-
```ts
|
|
52
|
-
const logger = createBackendLogger("payments");
|
|
53
|
-
logger.info("charge", { amount: 99, currency: "usd" });
|
|
68
|
+
```bash
|
|
69
|
+
LOG_LEVEL=debug node app.js
|
|
54
70
|
```
|
|
55
71
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
Core factory. Bring your own level strategy and output.
|
|
72
|
+
Use your own level and output:
|
|
59
73
|
|
|
60
74
|
```ts
|
|
61
|
-
import { createLogger,
|
|
75
|
+
import { consoleOutput, createLogger, parseLogLevel } from "@vllnt/logger";
|
|
62
76
|
|
|
63
77
|
const logger = createLogger("api", {
|
|
64
|
-
getLogLevel: () => "
|
|
78
|
+
getLogLevel: () => parseLogLevel(process.env.API_LOG_LEVEL, "info"),
|
|
65
79
|
output: consoleOutput,
|
|
66
80
|
});
|
|
81
|
+
logger.info("request", { method: "GET", path: "/health" });
|
|
67
82
|
```
|
|
68
83
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
Same as `createLogger` but adds `withTiming` and `withTimingSync` helpers.
|
|
84
|
+
Time an async call. It logs `<scope>.<label>.start`, then `.complete` with `durationMs`, or `.error` and rethrows:
|
|
72
85
|
|
|
73
86
|
```ts
|
|
74
|
-
|
|
75
|
-
return db.query("SELECT ...");
|
|
76
|
-
});
|
|
77
|
-
// Logs: scope.db.query.start → scope.db.query.complete (with durationMs)
|
|
78
|
-
// On error: scope.db.query.error (with durationMs + error message)
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
### `composeOutputs(...outputs): LogOutput`
|
|
82
|
-
|
|
83
|
-
Fan-out to multiple outputs. Each output is error-isolated.
|
|
87
|
+
import { consoleOutput, createExtendedLogger } from "@vllnt/logger";
|
|
84
88
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const output = composeOutputs(
|
|
90
|
-
consoleOutput,
|
|
91
|
-
createPostHogOutput(posthog.capture.bind(posthog)),
|
|
92
|
-
);
|
|
89
|
+
const logger = createExtendedLogger("db", { getLogLevel: () => "info", output: consoleOutput });
|
|
90
|
+
const rows = await logger.withTiming("query", async () => [{ id: 1 }], { table: "users" });
|
|
91
|
+
logger.info("rows", { count: rows.length });
|
|
93
92
|
```
|
|
94
93
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
Safe for Convex queries, mutations, and actions. No `process.env` access.
|
|
94
|
+
Log from a Convex query, mutation or action:
|
|
98
95
|
|
|
99
96
|
```ts
|
|
100
97
|
import { createConvexLogger } from "@vllnt/logger/convex";
|
|
101
98
|
|
|
102
|
-
const logger = createConvexLogger("
|
|
103
|
-
logger.info("
|
|
99
|
+
const logger = createConvexLogger("orders", "debug");
|
|
100
|
+
logger.info("created", { orderId: "o_1" });
|
|
104
101
|
```
|
|
105
102
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
Wraps a PostHog-compatible `capture` function as a `LogOutput`. Swallows thrown errors and rejected promises.
|
|
103
|
+
Write to the console and to PostHog (posthog-node):
|
|
109
104
|
|
|
110
105
|
```ts
|
|
106
|
+
import { composeOutputs, consoleOutput, createLogger } from "@vllnt/logger";
|
|
111
107
|
import { createPostHogOutput } from "@vllnt/logger/posthog";
|
|
112
|
-
|
|
113
|
-
|
|
108
|
+
import { PostHog } from "posthog-node";
|
|
109
|
+
|
|
110
|
+
const client = new PostHog("phc_project_api_key");
|
|
111
|
+
const logger = createLogger("billing", {
|
|
112
|
+
getLogLevel: () => "info",
|
|
113
|
+
output: composeOutputs(
|
|
114
|
+
consoleOutput,
|
|
115
|
+
createPostHogOutput((event, properties = {}) =>
|
|
116
|
+
client.capture({ distinctId: "server", event, properties }),
|
|
117
|
+
),
|
|
118
|
+
),
|
|
119
|
+
});
|
|
120
|
+
logger.info("invoice.paid", { invoiceId: "in_1" });
|
|
121
|
+
await client.shutdown();
|
|
114
122
|
```
|
|
115
123
|
|
|
116
|
-
|
|
124
|
+
## Errors and fixes
|
|
117
125
|
|
|
118
|
-
|
|
126
|
+
Log calls never throw. These are the error messages and markers you can see, and their fixes:
|
|
119
127
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
128
|
+
| Error or message | Cause | Fix |
|
|
129
|
+
| --- | --- | --- |
|
|
130
|
+
| `[logger:serialize-error] event=<event> level=<level>` written instead of JSON | `consoleOutput` could not serialize the entry: a value has a throwing `toJSON`, or a nested object has a throwing getter | Log plain data; convert class instances before logging |
|
|
131
|
+
| `"[Circular]"` as a field value | The data has a circular reference | None needed; log a plain copy to keep the full value |
|
|
132
|
+
| `"error": "[unprintable error]"` in a `<label>.error` entry | `withTiming` caught a value whose conversion to a string throws | Throw `Error` instances |
|
|
133
|
+
| The error from your function, after a `<label>.error` entry | `withTiming` and `withTimingSync` rethrow the original error | Catch it around the `withTiming` call |
|
|
134
|
+
| `ERR_PACKAGE_PATH_NOT_EXPORTED` from `require("@vllnt/logger")` | The package is ESM only and has no `require` export. A CommonJS project built with `moduleResolution: nodenext` type-checks, but its output calls `require` and fails here at runtime | Use `import`; in CommonJS use `await import("@vllnt/logger")`, a `.mts` file, or `"type": "module"` |
|
|
135
|
+
| `TS2307: Cannot find module '@vllnt/logger'` (TypeScript 6 reports `TS5107`, `node10` is deprecated, first) | TypeScript `moduleResolution` is `node10` (`node`), which ignores `exports` | Set `moduleResolution` to `nodenext`, `node16` or `bundler` |
|
|
136
|
+
| `TS1479: The current file is a CommonJS module whose imports will produce 'require' calls` | A CommonJS file under `node16` imports this ESM package | Add `"type": "module"`, use a `.mts` file, or use `await import()` |
|
|
123
137
|
|
|
124
|
-
|
|
125
|
-
const logger = createLogger("test", {
|
|
126
|
-
getLogLevel: () => "debug",
|
|
127
|
-
output,
|
|
128
|
-
});
|
|
138
|
+
## Limits and gotchas
|
|
129
139
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
140
|
+
- `createBackendLogger` defaults to `warn`: `debug` and `info` calls write nothing until `LOG_LEVEL` is set. `withTiming` logs `.start` and `.complete` at `info`, so only `.error` shows at the default.
|
|
141
|
+
- `createConvexLogger` uses its fixed `level` (default `info`) and never reads environment variables.
|
|
142
|
+
- An invalid `LOG_LEVEL` falls back to `warn`. A custom `getLogLevel` that returns an invalid value or throws falls back to `info`.
|
|
143
|
+
- Data keys `event`, `level` and `timestamp` are silently replaced by the logger's values.
|
|
144
|
+
- `formatLogEntry` does not add the scope prefix; only logger methods do.
|
|
145
|
+
- A throwing output is swallowed without a trace. `createPostHogOutput` also swallows rejected promises. Outputs are synchronous; returned promises are not awaited.
|
|
146
|
+
- A throwing getter at the top level of log-call data, or of `withTiming`/`withTimingSync` `extraData`, drops that entry silently: it runs while the entry is built. The timing helpers still return the result or rethrow the original error. A throwing getter deeper down gives the `[logger:serialize-error]` line.
|
|
147
|
+
- `consoleOutput` writes `BigInt` as a string and `Error` as `{ name, message, stack }`. Like `JSON.stringify`, it writes `Map` and `Set` as `{}` and `NaN` as `null`; it drops `undefined`, functions and symbols from objects and writes them as `null` in arrays.
|
|
148
|
+
- `withTiming` logs `error.message` verbatim, without the stack. There is no redaction.
|
|
149
|
+
- `createTestOutput` keeps every entry in memory; create a new one per test.
|
|
133
150
|
|
|
134
|
-
|
|
151
|
+
## Compatibility
|
|
135
152
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
153
|
+
- Runtime dependencies: none. Peer dependencies: none.
|
|
154
|
+
- ESM only, with typings for every entry point. TypeScript `moduleResolution` must be `nodenext`, `node16` or `bundler`, and the importing file must be ESM: in a CommonJS project, `nodenext` type-checks but the emitted `require` fails at runtime.
|
|
155
|
+
- Node.js: no `engines` field. The build targets ES2024; CI tests on Node.js 24.
|
|
156
|
+
- Convex: `@vllnt/logger/convex` does not read `process.env`. Browsers and edge runtimes: the core uses no Node.js APIs and `createBackendLogger` checks for `process` first, but tests run only on Node.js.
|
|
157
|
+
- PostHog: with posthog-js, pass `(event, properties) => posthog.capture(event, properties)` so `capture` keeps its `this`. posthog-node needs the wrapper shown above; tests check it against posthog-node 6.
|
|
141
158
|
|
|
142
|
-
##
|
|
159
|
+
## Testing
|
|
160
|
+
|
|
161
|
+
Capture entries with `createTestOutput` and assert on them:
|
|
143
162
|
|
|
144
163
|
```ts
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
debug;
|
|
154
|
-
info;
|
|
155
|
-
warn;
|
|
156
|
-
error: (event: string, data?: LogData) => void;
|
|
157
|
-
};
|
|
158
|
-
type ExtendedLogger = Logger & { withTiming; withTimingSync };
|
|
159
|
-
type LoggerConfig = { getLogLevel: () => LogLevel; output: LogOutput };
|
|
164
|
+
import assert from "node:assert/strict";
|
|
165
|
+
import { createLogger } from "@vllnt/logger";
|
|
166
|
+
import { createTestOutput } from "@vllnt/logger/testing";
|
|
167
|
+
|
|
168
|
+
const { entries, output } = createTestOutput();
|
|
169
|
+
const logger = createLogger("checkout", { getLogLevel: () => "debug", output });
|
|
170
|
+
logger.info("paid", { orderId: "o_1" });
|
|
171
|
+
assert.equal(entries[0]?.event, "checkout.paid");
|
|
160
172
|
```
|
|
161
173
|
|
|
162
|
-
##
|
|
174
|
+
## Security
|
|
163
175
|
|
|
164
|
-
-
|
|
165
|
-
|
|
166
|
-
- **Log calls never throw**: a failing `getLogLevel`, output, or data getter is swallowed; an invalid level falls back to `info`
|
|
167
|
-
- **Crash-safe serialization**: `consoleOutput` writes `BigInt` as a string, `Error` as `{ name, message, stack }`, and circular refs as `"[Circular]"`; a throwing `toJSON` degrades to a minimal `event`/`level` line
|
|
168
|
-
- **No built-in redaction**: pass only data you are willing to log. `withTiming` logs `error.message` verbatim, so sanitize sensitive errors before throwing them
|
|
169
|
-
- **Synchronous outputs**: `LogOutput` is synchronous by design — async adapters manage their own promises
|
|
170
|
-
- **Lazy level evaluation**: `getLogLevel()` is called per-emit, so env changes are picked up without restart
|
|
171
|
-
- **Zero side effects**: Core module has zero module-level side effects (`sideEffects: false`)
|
|
176
|
+
There is no built-in redaction: pass only data you are willing to log. Report vulnerabilities as described in
|
|
177
|
+
[SECURITY.md](https://github.com/vllnt/logger/blob/main/SECURITY.md).
|
|
172
178
|
|
|
173
179
|
## Related packages
|
|
174
180
|
|
|
175
|
-
|
|
176
|
-
management. Use it for user behaviour; use this logger for operational logs.
|
|
177
|
-
- [`@vllnt/convex-events`](https://oss.vllnt.com/packages/convex-events): an append-only activity
|
|
178
|
-
feed stored in Convex. Use it when entries must be stored and queried later; this logger only
|
|
179
|
-
writes to outputs such as the console.
|
|
181
|
+
None. This package has no runtime or peer dependencies, and no other @vllnt package shares code, a test or an example with it.
|
|
180
182
|
|
|
181
|
-
##
|
|
183
|
+
## Contributing
|
|
182
184
|
|
|
183
|
-
|
|
184
|
-
nothing is published from a
|
|
185
|
+
See [CONTRIBUTING.md](https://github.com/vllnt/logger/blob/main/CONTRIBUTING.md). Every publish runs in
|
|
186
|
+
`.github/workflows/publish.yml` with npm trusted publishing and provenance; nothing is published from a
|
|
187
|
+
local machine. Maintainers only.
|
|
185
188
|
|
|
186
189
|
- Canary (`@canary`): every push to `main` that touches the package publishes one
|
|
187
190
|
(`pnpm add @vllnt/logger@canary`).
|
|
188
191
|
- Stable (`@latest`): merge a PR that sets `package.json` `version` and adds a matching
|
|
189
192
|
`## [X.Y.Z]` entry to `CHANGELOG.md`, then push the tag `vX.Y.Z` on that `main` commit.
|
|
190
193
|
|
|
194
|
+
## Links
|
|
195
|
+
|
|
196
|
+
- Docs and changelog: [oss.vllnt.com/packages/logger](https://oss.vllnt.com/packages/logger) · [release feed](https://oss.vllnt.com/packages/logger/rss.xml)
|
|
197
|
+
- All @vllnt packages: [oss.vllnt.com](https://oss.vllnt.com) · [search packages and docs on index.vllnt.com](https://index.vllnt.com)
|
|
198
|
+
- Source and issues: [GitHub](https://github.com/vllnt/logger) · [issues](https://github.com/vllnt/logger/issues) · [npm](https://www.npmjs.com/package/@vllnt/logger)
|
|
199
|
+
- Questions and updates: [Discord](https://bntvllnt.com/discord) · [X @bntvllnt](https://x.com/bntvllnt)
|
|
200
|
+
- Company and author: [vllnt.com](https://vllnt.com) · [bntvllnt.com](https://bntvllnt.com) · [GitHub @bntvllnt](https://github.com/bntvllnt) · [LinkedIn](https://linkedin.com/in/bntvllnt)
|
|
201
|
+
- Sponsor: [GitHub Sponsors](https://github.com/sponsors/bntvllnt)
|
|
202
|
+
|
|
191
203
|
## License
|
|
192
204
|
|
|
193
205
|
[MIT](LICENSE)
|
|
194
206
|
|
|
195
207
|
---
|
|
196
208
|
|
|
197
|
-
Part of vllnt oss
|
|
209
|
+
Part of [vllnt open source](https://oss.vllnt.com) · [Package page](https://oss.vllnt.com/packages/logger) · [vllnt.com](https://vllnt.com) · Built by [bntvllnt](https://bntvllnt.com)
|
|
@@ -61,23 +61,29 @@ function formatError(error) {
|
|
|
61
61
|
return "[unprintable error]";
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
|
+
function logBuilt(log, event, build) {
|
|
65
|
+
try {
|
|
66
|
+
log(event, build());
|
|
67
|
+
} catch {
|
|
68
|
+
}
|
|
69
|
+
}
|
|
64
70
|
function createWithTiming(base) {
|
|
65
71
|
return async (label, execute, extraData) => {
|
|
66
72
|
const start = Date.now();
|
|
67
73
|
base.info(`${label}.start`, extraData);
|
|
68
74
|
try {
|
|
69
75
|
const result = await execute();
|
|
70
|
-
base.info
|
|
76
|
+
logBuilt(base.info, `${label}.complete`, () => ({
|
|
71
77
|
...extraData,
|
|
72
78
|
durationMs: Date.now() - start
|
|
73
|
-
});
|
|
79
|
+
}));
|
|
74
80
|
return result;
|
|
75
81
|
} catch (error) {
|
|
76
|
-
base.error
|
|
82
|
+
logBuilt(base.error, `${label}.error`, () => ({
|
|
77
83
|
...extraData,
|
|
78
84
|
durationMs: Date.now() - start,
|
|
79
85
|
error: formatError(error)
|
|
80
|
-
});
|
|
86
|
+
}));
|
|
81
87
|
throw error;
|
|
82
88
|
}
|
|
83
89
|
};
|
|
@@ -88,17 +94,17 @@ function createWithTimingSync(base) {
|
|
|
88
94
|
base.info(`${label}.start`, extraData);
|
|
89
95
|
try {
|
|
90
96
|
const result = execute();
|
|
91
|
-
base.info
|
|
97
|
+
logBuilt(base.info, `${label}.complete`, () => ({
|
|
92
98
|
...extraData,
|
|
93
99
|
durationMs: Date.now() - start
|
|
94
|
-
});
|
|
100
|
+
}));
|
|
95
101
|
return result;
|
|
96
102
|
} catch (error) {
|
|
97
|
-
base.error
|
|
103
|
+
logBuilt(base.error, `${label}.error`, () => ({
|
|
98
104
|
...extraData,
|
|
99
105
|
durationMs: Date.now() - start,
|
|
100
106
|
error: formatError(error)
|
|
101
|
-
});
|
|
107
|
+
}));
|
|
102
108
|
throw error;
|
|
103
109
|
}
|
|
104
110
|
};
|
package/dist/convex.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { LogLevel, ExtendedLogger } from './index.js';
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Create a logger for Convex functions.
|
|
12
|
-
* Pass level explicitly
|
|
12
|
+
* Pass the level explicitly; this logger never reads `process.env`.
|
|
13
13
|
* Defaults to "info".
|
|
14
14
|
*/
|
|
15
15
|
declare function createConvexLogger(scope: string, level?: LogLevel): ExtendedLogger;
|
package/dist/convex.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Pure functions, ZERO module-level side effects.
|
|
5
5
|
* Safe to import in any runtime (Node, Convex, browser, edge).
|
|
6
6
|
*/
|
|
7
|
-
/**
|
|
7
|
+
/** The four log levels. Severity order: debug < info < warn < error. */
|
|
8
8
|
type LogLevel = "debug" | "error" | "info" | "warn";
|
|
9
9
|
/** Structured log data payload */
|
|
10
10
|
type LogData = Record<string, unknown>;
|
|
@@ -30,7 +30,7 @@ type ExtendedLogger = Logger & {
|
|
|
30
30
|
type LogOutput = (entry: LogEntry) => void;
|
|
31
31
|
/** Configuration for creating a logger */
|
|
32
32
|
type LoggerConfig = {
|
|
33
|
-
/** Returns the current log level. Called on every log
|
|
33
|
+
/** Returns the current log level. Called on every log call, including calls its level filters out — keep it cheap. */
|
|
34
34
|
getLogLevel: () => LogLevel;
|
|
35
35
|
output: LogOutput;
|
|
36
36
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { E as ExtendedLogger } from './core-
|
|
2
|
-
export { a as LogData, b as LogEntry, L as LogLevel, c as LogOutput, d as Logger, e as LoggerConfig, f as composeOutputs, g as consoleOutput, h as createExtendedLogger, i as createLogger, j as formatLogEntry, p as parseLogLevel } from './core-
|
|
1
|
+
import { E as ExtendedLogger } from './core-CzlOE0nJ.js';
|
|
2
|
+
export { a as LogData, b as LogEntry, L as LogLevel, c as LogOutput, d as Logger, e as LoggerConfig, f as composeOutputs, g as consoleOutput, h as createExtendedLogger, i as createLogger, j as formatLogEntry, p as parseLogLevel } from './core-CzlOE0nJ.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @vllnt/logger — Backend preset.
|
|
@@ -8,7 +8,12 @@ export { a as LogData, b as LogEntry, L as LogLevel, c as LogOutput, d as Logger
|
|
|
8
8
|
* Reads LOG_LEVEL from process.env on each call (no module-level side effects).
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* Create a scoped Node.js logger that writes with `consoleOutput`. Reads
|
|
13
|
+
* `LOG_LEVEL` from `process.env` on each call; the default is `warn`.
|
|
14
|
+
*
|
|
15
|
+
* @see https://oss.vllnt.com/packages/logger
|
|
16
|
+
*/
|
|
12
17
|
declare function createBackendLogger(scope: string): ExtendedLogger;
|
|
13
18
|
|
|
14
19
|
export { ExtendedLogger, createBackendLogger };
|
package/dist/index.js
CHANGED
package/docs/API.md
CHANGED
|
@@ -18,7 +18,8 @@ its own type declarations.
|
|
|
18
18
|
|
|
19
19
|
Shared types (`LogEntry`, `LogLevel`, …) are exported only from `@vllnt/logger`.
|
|
20
20
|
|
|
21
|
-
Examples use placeholders (`posthog
|
|
21
|
+
Examples use placeholders (`posthog` for a posthog-js instance, `apiKey`, `distinctId`,
|
|
22
|
+
`fetchRows`, `orderId`) and Vitest's `expect`.
|
|
22
23
|
|
|
23
24
|
## Behaviour shared by every logger
|
|
24
25
|
|
|
@@ -46,7 +47,7 @@ import { createBackendLogger } from "@vllnt/logger";
|
|
|
46
47
|
|
|
47
48
|
const logger = createBackendLogger("payments");
|
|
48
49
|
logger.warn("charge.declined", { amount: 99 });
|
|
49
|
-
// console.warn: {"amount":99,"event":"payments.charge.declined","level":"warn","timestamp":"
|
|
50
|
+
// console.warn: {"amount":99,"event":"payments.charge.declined","level":"warn","timestamp":"2026-11-27T12:00:00.000Z"}
|
|
50
51
|
```
|
|
51
52
|
|
|
52
53
|
### `createLogger(scope: string, config: LoggerConfig): Logger`
|
|
@@ -74,6 +75,8 @@ Both log `info` `<scope>.<label>.start` with `extraData`, then `info` `<scope>.<
|
|
|
74
75
|
with `extraData` and `durationMs`. If `execute` throws, they log `error` `<scope>.<label>.error`
|
|
75
76
|
with `extraData`, `durationMs` and `error` (the `Error` message, otherwise `String(value)`, or
|
|
76
77
|
`"[unprintable error]"`), then rethrow the original error. The message is logged verbatim.
|
|
78
|
+
An entry whose `extraData` cannot be read (for example a throwing getter) is dropped; the helpers
|
|
79
|
+
still return the result or rethrow the original error.
|
|
77
80
|
|
|
78
81
|
```ts
|
|
79
82
|
import { consoleOutput, createExtendedLogger } from "@vllnt/logger";
|
|
@@ -119,7 +122,7 @@ Builds an entry: `{ ...data, event, level, timestamp }`. It does not add a scope
|
|
|
119
122
|
import { formatLogEntry } from "@vllnt/logger";
|
|
120
123
|
|
|
121
124
|
formatLogEntry("info", "cache.hit", { key: "user:1" });
|
|
122
|
-
// { key: "user:1", event: "cache.hit", level: "info", timestamp: "2026
|
|
125
|
+
// { key: "user:1", event: "cache.hit", level: "info", timestamp: "2026-11-27T12:00:00.000Z" }
|
|
123
126
|
```
|
|
124
127
|
|
|
125
128
|
### `parseLogLevel(value: string | undefined, defaultLevel?: LogLevel): LogLevel`
|
|
@@ -182,8 +185,7 @@ logger.info("created", { orderId });
|
|
|
182
185
|
Calls `capture(entry.event, properties)`, where `properties` is the entry without `event`
|
|
183
186
|
(`level`, `timestamp` and your data). Errors thrown by `capture`, and rejections of a promise it
|
|
184
187
|
returns, are swallowed. No PostHog SDK dependency: pass your own function. posthog-js
|
|
185
|
-
`capture(event, properties)`
|
|
186
|
-
`(event, properties) => client.capture({ distinctId, event, properties })`.
|
|
188
|
+
`capture(event, properties)` takes the same parameters; the arrow wrapper keeps its `this` bound:
|
|
187
189
|
|
|
188
190
|
```ts
|
|
189
191
|
import { createLogger } from "@vllnt/logger";
|
|
@@ -195,6 +197,19 @@ const logger = createLogger("billing", {
|
|
|
195
197
|
});
|
|
196
198
|
```
|
|
197
199
|
|
|
200
|
+
posthog-node `capture` takes one object, so wrap it. The `= {}` default keeps the wrapper valid
|
|
201
|
+
under `exactOptionalPropertyTypes`, where posthog-node's `properties` does not accept `undefined`:
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
import { createPostHogOutput } from "@vllnt/logger/posthog";
|
|
205
|
+
import { PostHog } from "posthog-node";
|
|
206
|
+
|
|
207
|
+
const client = new PostHog(apiKey);
|
|
208
|
+
const output = createPostHogOutput((event, properties = {}) =>
|
|
209
|
+
client.capture({ distinctId, event, properties }),
|
|
210
|
+
);
|
|
211
|
+
```
|
|
212
|
+
|
|
198
213
|
### `PostHogCapture`
|
|
199
214
|
|
|
200
215
|
```ts
|
package/llms.txt
CHANGED
|
@@ -1,33 +1,51 @@
|
|
|
1
1
|
# @vllnt/logger
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> Zero-dependency structured JSON logger for TypeScript with scoped events, log levels, timing, and console, PostHog and test outputs for Node.js and Convex.
|
|
4
4
|
|
|
5
|
-
Scoped events (`auth.login`), levels `debug`, `info`, `warn`, `error`, and pluggable synchronous outputs. The Node.js
|
|
5
|
+
Scoped events (`auth.login`), levels `debug`, `info`, `warn`, `error`, and pluggable synchronous outputs. Log calls never throw. The Node.js preset `createBackendLogger` reads `LOG_LEVEL` from `process.env` on each call and defaults to `warn`. There is no built-in redaction. ESM only; TypeScript `moduleResolution` must be `nodenext`, `node16` or `bundler`.
|
|
6
6
|
|
|
7
7
|
## For coding agents
|
|
8
8
|
|
|
9
9
|
The docs are bundled in the package at `node_modules/@vllnt/logger/docs/`, with the TypeScript sources in `node_modules/@vllnt/logger/src/`. They match the installed version. Read them before writing code.
|
|
10
10
|
|
|
11
|
+
## When to use
|
|
12
|
+
|
|
13
|
+
- Use it when you want one JSON line per event, with a scoped event name, from TypeScript.
|
|
14
|
+
- Use it when the same logger API must run in Node.js and in Convex queries, mutations and actions.
|
|
15
|
+
- Use it when log entries should go to several outputs, such as the console and PostHog.
|
|
16
|
+
- Use it when tests should assert on log entries instead of console output.
|
|
17
|
+
- Do not use it when you need log transports, files, rotation, redaction or async batching: use pino or winston.
|
|
18
|
+
- Do not use it when entries must be stored and queried later: use [`@vllnt/convex-events`](https://oss.vllnt.com/packages/convex-events).
|
|
19
|
+
- Do not use it for product analytics with consent: use [`@vllnt/analytics`](https://oss.vllnt.com/packages/analytics).
|
|
20
|
+
|
|
11
21
|
## Docs
|
|
12
22
|
|
|
13
23
|
- [API Reference](./docs/API.md): Every public export, its signature and an example, per entry point
|
|
14
|
-
- [
|
|
24
|
+
- [Errors and fixes](./README.md#errors-and-fixes): Every error message and marker, its cause and the fix
|
|
25
|
+
- [Limits and gotchas](./README.md#limits-and-gotchas): Defaults and calls that silently do nothing
|
|
26
|
+
- [README](./README.md): Install, quick start, API table, common tasks and compatibility
|
|
15
27
|
- [Changelog](./CHANGELOG.md): Version history and release notes
|
|
28
|
+
- [Links](./README.md#links): Package page, release feed, source, issues, npm and community links
|
|
16
29
|
|
|
17
30
|
## Entry points
|
|
18
31
|
|
|
19
|
-
- `@vllnt/logger`: core API and Node.js
|
|
32
|
+
- `@vllnt/logger`: core API and Node.js preset (`createBackendLogger`)
|
|
20
33
|
- `@vllnt/logger/convex`: `createConvexLogger`, no `process.env` access
|
|
21
34
|
- `@vllnt/logger/posthog`: `createPostHogOutput`
|
|
22
35
|
- `@vllnt/logger/testing`: `createTestOutput`
|
|
23
36
|
|
|
24
|
-
## Ecosystem
|
|
25
|
-
|
|
26
|
-
- [vllnt oss](https://oss.vllnt.com/llms.txt): Index of all @vllnt open-source packages
|
|
27
|
-
|
|
28
37
|
## Optional
|
|
29
38
|
|
|
30
39
|
- [Contributing](https://github.com/vllnt/logger/blob/main/CONTRIBUTING.md): Development setup and release flow
|
|
31
40
|
- [AGENTS](https://github.com/vllnt/logger/blob/main/AGENTS.md): Agent instructions for working on this repository (not shipped in the package)
|
|
32
41
|
- [Security Policy](https://github.com/vllnt/logger/blob/main/SECURITY.md): Vulnerability reporting
|
|
33
42
|
- [License](./LICENSE): MIT
|
|
43
|
+
|
|
44
|
+
## Ecosystem
|
|
45
|
+
|
|
46
|
+
- [Package page](https://oss.vllnt.com/packages/logger)
|
|
47
|
+
- [Release feed](https://oss.vllnt.com/packages/logger/rss.xml)
|
|
48
|
+
- [All @vllnt packages](https://oss.vllnt.com/llms.txt)
|
|
49
|
+
- [Search vllnt packages and docs](https://index.vllnt.com)
|
|
50
|
+
- [vllnt](https://vllnt.com/llms.txt)
|
|
51
|
+
- [Author](https://bntvllnt.com)
|
package/package.json
CHANGED
|
@@ -1,24 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vllnt/logger",
|
|
3
|
-
"version": "0.1.3-canary.
|
|
3
|
+
"version": "0.1.3-canary.7d9ec44",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "Zero-dependency structured JSON logger for TypeScript with scoped events, log levels, timing, and console, PostHog and test outputs for Node.js and Convex.",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"author":
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "bntvllnt",
|
|
9
|
+
"url": "https://bntvllnt.com"
|
|
10
|
+
},
|
|
11
|
+
"funding": {
|
|
12
|
+
"type": "github",
|
|
13
|
+
"url": "https://github.com/sponsors/bntvllnt"
|
|
14
|
+
},
|
|
8
15
|
"homepage": "https://oss.vllnt.com/packages/logger",
|
|
9
16
|
"repository": {
|
|
10
17
|
"type": "git",
|
|
11
18
|
"url": "https://github.com/vllnt/logger"
|
|
12
19
|
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/vllnt/logger/issues"
|
|
22
|
+
},
|
|
13
23
|
"keywords": [
|
|
14
24
|
"logger",
|
|
15
25
|
"logging",
|
|
16
26
|
"structured-logging",
|
|
17
|
-
"json",
|
|
27
|
+
"json-logger",
|
|
28
|
+
"log-levels",
|
|
29
|
+
"typescript",
|
|
30
|
+
"nodejs",
|
|
18
31
|
"convex",
|
|
19
32
|
"posthog",
|
|
20
|
-
"
|
|
21
|
-
"
|
|
33
|
+
"zero-dependency",
|
|
34
|
+
"esm"
|
|
22
35
|
],
|
|
23
36
|
"sideEffects": false,
|
|
24
37
|
"exports": {
|
|
@@ -52,6 +65,7 @@
|
|
|
52
65
|
"lint": "eslint --cache --cache-location node_modules/.cache/eslint/ --max-warnings 0",
|
|
53
66
|
"lint:fix": "eslint --cache --cache-location node_modules/.cache/eslint/ --fix",
|
|
54
67
|
"test": "vitest run",
|
|
68
|
+
"test:coverage": "vitest run --coverage --coverage.include=src --coverage.reporter=text",
|
|
55
69
|
"test:watch": "vitest"
|
|
56
70
|
},
|
|
57
71
|
"publishConfig": {
|
|
@@ -60,13 +74,15 @@
|
|
|
60
74
|
},
|
|
61
75
|
"devDependencies": {
|
|
62
76
|
"@types/node": "^26.6.6",
|
|
77
|
+
"@vitest/coverage-v8": "^5.1.1",
|
|
63
78
|
"@vllnt/eslint-config": "^2.1.0",
|
|
64
|
-
"@vllnt/typescript": "^
|
|
79
|
+
"@vllnt/typescript": "^3.0.0",
|
|
65
80
|
"eslint": "^9.39.5",
|
|
81
|
+
"posthog-node": "^6.0.5",
|
|
66
82
|
"prettier": "^3.10.1",
|
|
67
83
|
"tsup": "^9.0.0",
|
|
68
84
|
"typescript": "^6.0.3",
|
|
69
|
-
"vitest": "^5.1.
|
|
85
|
+
"vitest": "^5.1.1"
|
|
70
86
|
},
|
|
71
87
|
"packageManager": "pnpm@10.34.8",
|
|
72
88
|
"pnpm": {
|
package/src/backend.ts
CHANGED
|
@@ -26,7 +26,12 @@ function getBackendLogLevel(): LogLevel {
|
|
|
26
26
|
return parseLogLevel(getEnvironmentVariable("LOG_LEVEL"), "warn");
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* Create a scoped Node.js logger that writes with `consoleOutput`. Reads
|
|
31
|
+
* `LOG_LEVEL` from `process.env` on each call; the default is `warn`.
|
|
32
|
+
*
|
|
33
|
+
* @see https://oss.vllnt.com/packages/logger
|
|
34
|
+
*/
|
|
30
35
|
export function createBackendLogger(scope: string): ExtendedLogger {
|
|
31
36
|
return createExtendedLogger(scope, {
|
|
32
37
|
getLogLevel: getBackendLogLevel,
|
package/src/convex.ts
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Create a logger for Convex functions.
|
|
17
|
-
* Pass level explicitly
|
|
17
|
+
* Pass the level explicitly; this logger never reads `process.env`.
|
|
18
18
|
* Defaults to "info".
|
|
19
19
|
*/
|
|
20
20
|
export function createConvexLogger(
|
package/src/core.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* Safe to import in any runtime (Node, Convex, browser, edge).
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
/**
|
|
8
|
+
/** The four log levels. Severity order: debug < info < warn < error. */
|
|
9
9
|
export type LogLevel = "debug" | "error" | "info" | "warn";
|
|
10
10
|
|
|
11
11
|
/** Structured log data payload */
|
|
@@ -45,7 +45,7 @@ export type LogOutput = (entry: LogEntry) => void;
|
|
|
45
45
|
|
|
46
46
|
/** Configuration for creating a logger */
|
|
47
47
|
export type LoggerConfig = {
|
|
48
|
-
/** Returns the current log level. Called on every log
|
|
48
|
+
/** Returns the current log level. Called on every log call, including calls its level filters out — keep it cheap. */
|
|
49
49
|
getLogLevel: () => LogLevel;
|
|
50
50
|
output: LogOutput;
|
|
51
51
|
};
|
|
@@ -145,6 +145,22 @@ function formatError(error: unknown): string {
|
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
+
/**
|
|
149
|
+
* Build the data and log it. If building throws (for example a throwing
|
|
150
|
+
* getter in `extraData`), drop the entry, as `createLogger` does.
|
|
151
|
+
*/
|
|
152
|
+
function logBuilt(
|
|
153
|
+
log: Logger["info"],
|
|
154
|
+
event: string,
|
|
155
|
+
build: () => LogData,
|
|
156
|
+
): void {
|
|
157
|
+
try {
|
|
158
|
+
log(event, build());
|
|
159
|
+
} catch {
|
|
160
|
+
// Dropped — building the data must not break the timed call
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
148
164
|
function createWithTiming(base: Logger): ExtendedLogger["withTiming"] {
|
|
149
165
|
return async <T>(
|
|
150
166
|
label: string,
|
|
@@ -155,17 +171,17 @@ function createWithTiming(base: Logger): ExtendedLogger["withTiming"] {
|
|
|
155
171
|
base.info(`${label}.start`, extraData);
|
|
156
172
|
try {
|
|
157
173
|
const result = await execute();
|
|
158
|
-
base.info
|
|
174
|
+
logBuilt(base.info, `${label}.complete`, () => ({
|
|
159
175
|
...extraData,
|
|
160
176
|
durationMs: Date.now() - start,
|
|
161
|
-
});
|
|
177
|
+
}));
|
|
162
178
|
return result;
|
|
163
179
|
} catch (error) {
|
|
164
|
-
base.error
|
|
180
|
+
logBuilt(base.error, `${label}.error`, () => ({
|
|
165
181
|
...extraData,
|
|
166
182
|
durationMs: Date.now() - start,
|
|
167
183
|
error: formatError(error),
|
|
168
|
-
});
|
|
184
|
+
}));
|
|
169
185
|
throw error;
|
|
170
186
|
}
|
|
171
187
|
};
|
|
@@ -177,17 +193,17 @@ function createWithTimingSync(base: Logger): ExtendedLogger["withTimingSync"] {
|
|
|
177
193
|
base.info(`${label}.start`, extraData);
|
|
178
194
|
try {
|
|
179
195
|
const result = execute();
|
|
180
|
-
base.info
|
|
196
|
+
logBuilt(base.info, `${label}.complete`, () => ({
|
|
181
197
|
...extraData,
|
|
182
198
|
durationMs: Date.now() - start,
|
|
183
|
-
});
|
|
199
|
+
}));
|
|
184
200
|
return result;
|
|
185
201
|
} catch (error) {
|
|
186
|
-
base.error
|
|
202
|
+
logBuilt(base.error, `${label}.error`, () => ({
|
|
187
203
|
...extraData,
|
|
188
204
|
durationMs: Date.now() - start,
|
|
189
205
|
error: formatError(error),
|
|
190
|
-
});
|
|
206
|
+
}));
|
|
191
207
|
throw error;
|
|
192
208
|
}
|
|
193
209
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @vllnt/logger —
|
|
2
|
+
* @vllnt/logger — zero-dependency structured JSON logger for TypeScript.
|
|
3
3
|
*
|
|
4
|
-
* Core exports +
|
|
4
|
+
* Core exports + Node.js preset (`createBackendLogger`).
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
export { createBackendLogger } from "./backend.js";
|