@vllnt/logger 0.1.2 → 0.1.3-canary.1b2dbd3
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 +89 -0
- package/README.md +144 -92
- package/dist/{chunk-5GDAMA7T.js → chunk-OROSOSC5.js} +69 -28
- package/dist/convex.d.ts +2 -2
- package/dist/convex.js +1 -1
- package/dist/{core-BKAGZb-m.d.ts → core-CzlOE0nJ.d.ts} +13 -6
- package/dist/index.d.ts +8 -3
- package/dist/index.js +1 -1
- package/dist/posthog.d.ts +6 -4
- package/dist/posthog.js +8 -1
- package/dist/testing.d.ts +3 -2
- package/docs/API.md +242 -0
- package/llms.txt +51 -0
- package/package.json +75 -28
- package/src/backend.ts +40 -0
- package/src/convex.ts +28 -0
- package/src/core.ts +309 -0
- package/src/index.ts +22 -0
- package/src/posthog.ts +43 -0
- package/src/testing.ts +22 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented here. The format follows
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses
|
|
5
|
+
[Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## [0.1.3] - 2026-11-27
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- The npm package ships `docs/API.md` (every public export with an example), `llms.txt`,
|
|
12
|
+
`CHANGELOG.md` and the TypeScript sources in `src/`, so coding agents can read docs that match
|
|
13
|
+
the installed version from `node_modules/@vllnt/logger/`. `llms.txt` links the shipped files
|
|
14
|
+
with relative paths.
|
|
15
|
+
- README notes the bundled docs for coding agents.
|
|
16
|
+
- `package.json` sets `homepage` and `keywords`. Docs-only changes on `main` now publish a canary.
|
|
17
|
+
|
|
18
|
+
### Fixed
|
|
19
|
+
|
|
20
|
+
- Log calls never throw. A throwing `getLogLevel`, output, or data getter is swallowed instead of
|
|
21
|
+
propagating into the host application; `withTiming` and `withTimingSync` always rethrow the
|
|
22
|
+
original error from the wrapped function.
|
|
23
|
+
- An invalid or throwing `getLogLevel` falls back to `info` instead of silently dropping every entry.
|
|
24
|
+
- `consoleOutput` keeps the entry when data holds a `BigInt` (written as a string), an `Error`
|
|
25
|
+
(written as `{ name, message, stack }`, previously `{}`) or a circular reference
|
|
26
|
+
(written as `"[Circular]"`). Only a throwing `toJSON` or getter still degrades to the minimal line.
|
|
27
|
+
- `consoleOutput` no longer throws when a `console` method throws.
|
|
28
|
+
- `createPostHogOutput` swallows rejected promises returned by `capture` instead of leaving an
|
|
29
|
+
unhandled rejection. `PostHogCapture` now accepts functions returning any value.
|
|
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.
|
|
39
|
+
|
|
40
|
+
### Changed
|
|
41
|
+
|
|
42
|
+
- `package.json` `exports` lists the `types` condition first so every resolver picks the typings.
|
|
43
|
+
- Publishing: stable releases come only from a pushed `vX.Y.Z` tag on `main` (npm trusted
|
|
44
|
+
publishing with provenance); canary builds still publish on every push to `main`. Actions are
|
|
45
|
+
pinned to commit SHAs, the workflow token is read-only by default, and pnpm is pinned to 10.34.8
|
|
46
|
+
with a seven-day minimum release age.
|
|
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, `pnpm test:coverage`, `pnpm test:package` (run before every publish) and
|
|
69
|
+
type-checked tests.
|
|
70
|
+
|
|
71
|
+
## [0.1.2] - 2026-03-10
|
|
72
|
+
|
|
73
|
+
### Changed
|
|
74
|
+
|
|
75
|
+
- Publish workflow and CI refinements.
|
|
76
|
+
|
|
77
|
+
## [0.1.1] - 2026-03-10
|
|
78
|
+
|
|
79
|
+
### Changed
|
|
80
|
+
|
|
81
|
+
- Added the publish workflow (trusted publishing, canary and release jobs) and refined CI.
|
|
82
|
+
|
|
83
|
+
## [0.1.0] - 2026-03-09
|
|
84
|
+
|
|
85
|
+
### Added
|
|
86
|
+
|
|
87
|
+
- Initial release: core logger (`createLogger`, `createExtendedLogger`, `composeOutputs`,
|
|
88
|
+
`consoleOutput`, `formatLogEntry`, `parseLogLevel`), Node.js backend preset, and the `/convex`,
|
|
89
|
+
`/posthog` and `/testing` entry points.
|
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,148 +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
|
-
## Quick
|
|
26
|
+
## Quick start
|
|
14
27
|
|
|
15
28
|
```ts
|
|
16
29
|
import { createBackendLogger } from "@vllnt/logger";
|
|
17
30
|
|
|
18
31
|
const logger = createBackendLogger("auth");
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
// → {"event":"auth.login","level":"info","timestamp":"...","userId":"123"}
|
|
22
|
-
|
|
23
|
-
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"}
|
|
24
34
|
```
|
|
25
35
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
## Subpath Exports
|
|
29
|
-
|
|
30
|
-
| Import | Use Case |
|
|
31
|
-
|--------|----------|
|
|
32
|
-
| `@vllnt/logger` | Core API + Node.js backend preset |
|
|
33
|
-
| `@vllnt/logger/convex` | Convex-safe logger (no `process.env`) |
|
|
34
|
-
| `@vllnt/logger/posthog` | PostHog output adapter |
|
|
35
|
-
| `@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.
|
|
36
37
|
|
|
37
38
|
## API
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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):
|
|
42
67
|
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
logger.info("charge", { amount: 99, currency: "usd" });
|
|
68
|
+
```bash
|
|
69
|
+
LOG_LEVEL=debug node app.js
|
|
46
70
|
```
|
|
47
71
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
Core factory. Bring your own level strategy and output.
|
|
72
|
+
Use your own level and output:
|
|
51
73
|
|
|
52
74
|
```ts
|
|
53
|
-
import { createLogger,
|
|
75
|
+
import { consoleOutput, createLogger, parseLogLevel } from "@vllnt/logger";
|
|
54
76
|
|
|
55
77
|
const logger = createLogger("api", {
|
|
56
|
-
getLogLevel: () => "
|
|
78
|
+
getLogLevel: () => parseLogLevel(process.env.API_LOG_LEVEL, "info"),
|
|
57
79
|
output: consoleOutput,
|
|
58
80
|
});
|
|
81
|
+
logger.info("request", { method: "GET", path: "/health" });
|
|
59
82
|
```
|
|
60
83
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
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:
|
|
64
85
|
|
|
65
86
|
```ts
|
|
66
|
-
|
|
67
|
-
return db.query("SELECT ...");
|
|
68
|
-
});
|
|
69
|
-
// Logs: scope.db.query.start → scope.db.query.complete (with durationMs)
|
|
70
|
-
// On error: scope.db.query.error (with durationMs + error message)
|
|
71
|
-
```
|
|
87
|
+
import { consoleOutput, createExtendedLogger } from "@vllnt/logger";
|
|
72
88
|
|
|
73
|
-
|
|
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 });
|
|
92
|
+
```
|
|
74
93
|
|
|
75
|
-
|
|
94
|
+
Log from a Convex query, mutation or action:
|
|
76
95
|
|
|
77
96
|
```ts
|
|
78
|
-
import {
|
|
79
|
-
import { createPostHogOutput } from "@vllnt/logger/posthog";
|
|
97
|
+
import { createConvexLogger } from "@vllnt/logger/convex";
|
|
80
98
|
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
createPostHogOutput(posthog.capture.bind(posthog)),
|
|
84
|
-
);
|
|
99
|
+
const logger = createConvexLogger("orders", "debug");
|
|
100
|
+
logger.info("created", { orderId: "o_1" });
|
|
85
101
|
```
|
|
86
102
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
Safe for Convex queries, mutations, and actions. No `process.env` access.
|
|
103
|
+
Write to the console and to PostHog (posthog-node):
|
|
90
104
|
|
|
91
105
|
```ts
|
|
92
|
-
import {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
106
|
+
import { composeOutputs, consoleOutput, createLogger } from "@vllnt/logger";
|
|
107
|
+
import { createPostHogOutput } from "@vllnt/logger/posthog";
|
|
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();
|
|
96
122
|
```
|
|
97
123
|
|
|
98
|
-
|
|
124
|
+
## Errors and fixes
|
|
99
125
|
|
|
100
|
-
|
|
126
|
+
Log calls never throw. These are the error messages and markers you can see, and their fixes:
|
|
101
127
|
|
|
102
|
-
|
|
103
|
-
|
|
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()` |
|
|
104
137
|
|
|
105
|
-
|
|
106
|
-
```
|
|
138
|
+
## Limits and gotchas
|
|
107
139
|
|
|
108
|
-
|
|
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.
|
|
109
150
|
|
|
110
|
-
|
|
151
|
+
## Compatibility
|
|
152
|
+
|
|
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.
|
|
158
|
+
|
|
159
|
+
## Testing
|
|
160
|
+
|
|
161
|
+
Capture entries with `createTestOutput` and assert on them:
|
|
111
162
|
|
|
112
163
|
```ts
|
|
113
|
-
import
|
|
164
|
+
import assert from "node:assert/strict";
|
|
114
165
|
import { createLogger } from "@vllnt/logger";
|
|
166
|
+
import { createTestOutput } from "@vllnt/logger/testing";
|
|
115
167
|
|
|
116
168
|
const { entries, output } = createTestOutput();
|
|
117
|
-
const logger = createLogger("
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
logger.info("hello", { a: 1 });
|
|
123
|
-
expect(entries[0].event).toBe("test.hello");
|
|
169
|
+
const logger = createLogger("checkout", { getLogLevel: () => "debug", output });
|
|
170
|
+
logger.info("paid", { orderId: "o_1" });
|
|
171
|
+
assert.equal(entries[0]?.event, "checkout.paid");
|
|
124
172
|
```
|
|
125
173
|
|
|
126
|
-
|
|
174
|
+
## Security
|
|
127
175
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
| `formatLogEntry(level, event, data?)` | Build a `LogEntry` manually |
|
|
131
|
-
| `parseLogLevel(value, default?)` | Safe `string` to `LogLevel` parsing |
|
|
132
|
-
| `consoleOutput(entry)` | JSON to `console.debug/info/warn/error` (crash-safe) |
|
|
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).
|
|
133
178
|
|
|
134
|
-
##
|
|
179
|
+
## Related packages
|
|
135
180
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
type LogEntry = { event: string; level: LogLevel; timestamp: string } & Record<string, unknown>;
|
|
140
|
-
type LogOutput = (entry: LogEntry) => void;
|
|
141
|
-
type Logger = { debug, info, warn, error: (event: string, data?: LogData) => void };
|
|
142
|
-
type ExtendedLogger = Logger & { withTiming, withTimingSync };
|
|
143
|
-
type LoggerConfig = { getLogLevel: () => LogLevel; output: LogOutput };
|
|
144
|
-
```
|
|
181
|
+
None. This package has no runtime or peer dependencies, and no other @vllnt package shares code, a test or an example with it.
|
|
182
|
+
|
|
183
|
+
## Contributing
|
|
145
184
|
|
|
146
|
-
|
|
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.
|
|
147
188
|
|
|
148
|
-
-
|
|
149
|
-
|
|
150
|
-
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
189
|
+
- Canary (`@canary`): every push to `main` that touches the package publishes one
|
|
190
|
+
(`pnpm add @vllnt/logger@canary`).
|
|
191
|
+
- Stable (`@latest`): merge a PR that sets `package.json` `version` and adds a matching
|
|
192
|
+
`## [X.Y.Z]` entry to `CHANGELOG.md`, then push the tag `vX.Y.Z` on that `main` commit.
|
|
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)
|
|
154
202
|
|
|
155
203
|
## License
|
|
156
204
|
|
|
157
205
|
[MIT](LICENSE)
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
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)
|
|
@@ -23,10 +23,20 @@ function formatLogEntry(level, event, data) {
|
|
|
23
23
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
|
+
function resolveLevel(config) {
|
|
27
|
+
try {
|
|
28
|
+
return parseLogLevel(config.getLogLevel(), "info");
|
|
29
|
+
} catch {
|
|
30
|
+
return "info";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
26
33
|
function createLogger(scope, config) {
|
|
27
34
|
const log = (level, event, data) => {
|
|
28
|
-
|
|
29
|
-
|
|
35
|
+
try {
|
|
36
|
+
if (shouldLog(level, resolveLevel(config))) {
|
|
37
|
+
config.output(formatLogEntry(level, `${scope}.${event}`, data));
|
|
38
|
+
}
|
|
39
|
+
} catch {
|
|
30
40
|
}
|
|
31
41
|
};
|
|
32
42
|
return {
|
|
@@ -45,7 +55,17 @@ function createLogger(scope, config) {
|
|
|
45
55
|
};
|
|
46
56
|
}
|
|
47
57
|
function formatError(error) {
|
|
48
|
-
|
|
58
|
+
try {
|
|
59
|
+
return error instanceof Error ? error.message : String(error);
|
|
60
|
+
} catch {
|
|
61
|
+
return "[unprintable error]";
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function logBuilt(log, event, build) {
|
|
65
|
+
try {
|
|
66
|
+
log(event, build());
|
|
67
|
+
} catch {
|
|
68
|
+
}
|
|
49
69
|
}
|
|
50
70
|
function createWithTiming(base) {
|
|
51
71
|
return async (label, execute, extraData) => {
|
|
@@ -53,17 +73,17 @@ function createWithTiming(base) {
|
|
|
53
73
|
base.info(`${label}.start`, extraData);
|
|
54
74
|
try {
|
|
55
75
|
const result = await execute();
|
|
56
|
-
base.info
|
|
76
|
+
logBuilt(base.info, `${label}.complete`, () => ({
|
|
57
77
|
...extraData,
|
|
58
78
|
durationMs: Date.now() - start
|
|
59
|
-
});
|
|
79
|
+
}));
|
|
60
80
|
return result;
|
|
61
81
|
} catch (error) {
|
|
62
|
-
base.error
|
|
82
|
+
logBuilt(base.error, `${label}.error`, () => ({
|
|
63
83
|
...extraData,
|
|
64
84
|
durationMs: Date.now() - start,
|
|
65
85
|
error: formatError(error)
|
|
66
|
-
});
|
|
86
|
+
}));
|
|
67
87
|
throw error;
|
|
68
88
|
}
|
|
69
89
|
};
|
|
@@ -74,17 +94,17 @@ function createWithTimingSync(base) {
|
|
|
74
94
|
base.info(`${label}.start`, extraData);
|
|
75
95
|
try {
|
|
76
96
|
const result = execute();
|
|
77
|
-
base.info
|
|
97
|
+
logBuilt(base.info, `${label}.complete`, () => ({
|
|
78
98
|
...extraData,
|
|
79
99
|
durationMs: Date.now() - start
|
|
80
|
-
});
|
|
100
|
+
}));
|
|
81
101
|
return result;
|
|
82
102
|
} catch (error) {
|
|
83
|
-
base.error
|
|
103
|
+
logBuilt(base.error, `${label}.error`, () => ({
|
|
84
104
|
...extraData,
|
|
85
105
|
durationMs: Date.now() - start,
|
|
86
106
|
error: formatError(error)
|
|
87
|
-
});
|
|
107
|
+
}));
|
|
88
108
|
throw error;
|
|
89
109
|
}
|
|
90
110
|
};
|
|
@@ -97,29 +117,50 @@ function createExtendedLogger(scope, config) {
|
|
|
97
117
|
withTimingSync: createWithTimingSync(base)
|
|
98
118
|
};
|
|
99
119
|
}
|
|
120
|
+
function createReplacer() {
|
|
121
|
+
const ancestors = [];
|
|
122
|
+
return function replacer(_key, value) {
|
|
123
|
+
if (typeof value === "bigint") {
|
|
124
|
+
return value.toString();
|
|
125
|
+
}
|
|
126
|
+
const resolved = value instanceof Error ? { message: value.message, name: value.name, stack: value.stack } : value;
|
|
127
|
+
if (typeof resolved !== "object" || resolved === null) {
|
|
128
|
+
return resolved;
|
|
129
|
+
}
|
|
130
|
+
ancestors.splice(ancestors.indexOf(this) + 1);
|
|
131
|
+
if (ancestors.includes(resolved)) {
|
|
132
|
+
return "[Circular]";
|
|
133
|
+
}
|
|
134
|
+
ancestors.push(resolved);
|
|
135
|
+
return resolved;
|
|
136
|
+
};
|
|
137
|
+
}
|
|
100
138
|
function consoleOutput(entry) {
|
|
101
139
|
let message;
|
|
102
140
|
try {
|
|
103
|
-
message = JSON.stringify(entry);
|
|
141
|
+
message = JSON.stringify(entry, createReplacer());
|
|
104
142
|
} catch {
|
|
105
143
|
message = `[logger:serialize-error] event=${entry.event} level=${entry.level}`;
|
|
106
144
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
145
|
+
try {
|
|
146
|
+
switch (entry.level) {
|
|
147
|
+
case "debug":
|
|
148
|
+
console.debug(message);
|
|
149
|
+
break;
|
|
150
|
+
case "error":
|
|
151
|
+
console.error(message);
|
|
152
|
+
break;
|
|
153
|
+
case "info":
|
|
154
|
+
console.info(message);
|
|
155
|
+
break;
|
|
156
|
+
case "warn":
|
|
157
|
+
console.warn(message);
|
|
158
|
+
break;
|
|
159
|
+
default:
|
|
160
|
+
console.info(message);
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
} catch {
|
|
123
164
|
}
|
|
124
165
|
}
|
|
125
166
|
function composeOutputs(...outputs) {
|
package/dist/convex.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LogLevel, ExtendedLogger } from './index.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @vllnt/logger — Convex-safe logger.
|
|
@@ -9,7 +9,7 @@ import { b as LogLevel, E as ExtendedLogger } from './core-BKAGZb-m.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
|
};
|
|
@@ -41,7 +41,12 @@ declare function parseLogLevel(value: string | undefined, defaultLevel?: LogLeve
|
|
|
41
41
|
* This prevents user data from overwriting event/level/timestamp.
|
|
42
42
|
*/
|
|
43
43
|
declare function formatLogEntry(level: LogLevel, event: string, data?: LogData): LogEntry;
|
|
44
|
-
/**
|
|
44
|
+
/**
|
|
45
|
+
* Create a scoped logger with the given configuration.
|
|
46
|
+
*
|
|
47
|
+
* Log calls never throw: a failing `getLogLevel`, `output`, or data getter is
|
|
48
|
+
* swallowed so logging cannot break the host application.
|
|
49
|
+
*/
|
|
45
50
|
declare function createLogger(scope: string, config: LoggerConfig): Logger;
|
|
46
51
|
/**
|
|
47
52
|
* Create an extended logger with timing helpers.
|
|
@@ -51,11 +56,13 @@ declare function createLogger(scope: string, config: LoggerConfig): Logger;
|
|
|
51
56
|
*/
|
|
52
57
|
declare function createExtendedLogger(scope: string, config: LoggerConfig): ExtendedLogger;
|
|
53
58
|
/**
|
|
54
|
-
* Default console output handler.
|
|
55
|
-
*
|
|
59
|
+
* Default console output handler. Never throws. It handles BigInt, Error and
|
|
60
|
+
* circular values; anything still unserializable (throwing toJSON or getters)
|
|
61
|
+
* degrades to a minimal event/level line, and console failures disappear.
|
|
56
62
|
*/
|
|
57
63
|
declare function consoleOutput(entry: LogEntry): void;
|
|
58
64
|
/** Compose outputs into a single LogOutput. Each output runs in isolation. */
|
|
59
65
|
declare function composeOutputs(...outputs: LogOutput[]): LogOutput;
|
|
60
66
|
|
|
61
|
-
export {
|
|
67
|
+
export { composeOutputs as f, consoleOutput as g, createExtendedLogger as h, createLogger as i, formatLogEntry as j, parseLogLevel as p };
|
|
68
|
+
export type { ExtendedLogger as E, LogLevel as L, LogData as a, LogEntry as b, LogOutput as c, Logger as d, LoggerConfig as e };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { E as ExtendedLogger } from './core-
|
|
2
|
-
export {
|
|
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 { L as LogData, a as LogEntry, b 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 };
|