@ctrlback/logtau-context 0.1.0
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/LICENSE.md +21 -0
- package/README.md +41 -0
- package/dist/main.d.mts +35 -0
- package/dist/main.mjs +51 -0
- package/package.json +74 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CTRLback SCCL
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @ctrlback/logtau-context
|
|
2
|
+
|
|
3
|
+
AsyncLocalStorage-backed ambient context for
|
|
4
|
+
[`@ctrlback/logtau`](https://forge.cloudsling.dev/CTRLback/logtau). Attach
|
|
5
|
+
bindings to a scope; every log emitted inside that scope (synchronously or
|
|
6
|
+
across `await`) carries them, with no need to thread a child logger through
|
|
7
|
+
call sites.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
pnpm add @ctrlback/logtau-context
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { configure, getLogger, stdoutSink } from '@ctrlback/logtau'
|
|
19
|
+
import { createAlsContext, withContext } from '@ctrlback/logtau-context'
|
|
20
|
+
|
|
21
|
+
configure({
|
|
22
|
+
sinks: { out: stdoutSink() },
|
|
23
|
+
loggers: [{ category: [], level: 'info', sinks: ['out'] }],
|
|
24
|
+
contextFactory: createAlsContext(),
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const log = getLogger(['http'])
|
|
28
|
+
|
|
29
|
+
await withContext({ reqId: 'r_abc' }, async () => {
|
|
30
|
+
log.info('handled') // includes reqId
|
|
31
|
+
await doWork() // logs inside doWork() include reqId too
|
|
32
|
+
})
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Nested `withContext` calls stack; on key collision the inner scope wins, and a
|
|
36
|
+
per-call field on a log call still overrides everything above it.
|
|
37
|
+
|
|
38
|
+
## Requirements
|
|
39
|
+
|
|
40
|
+
- Node.js `>= 22.6`
|
|
41
|
+
- Pure ESM consumer (the package is `"type": "module"` and has no CJS export)
|
package/dist/main.d.mts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ContextFactory, LogFields } from "@ctrlback/logtau";
|
|
2
|
+
|
|
3
|
+
//#region src/als-context.d.mts
|
|
4
|
+
/**
|
|
5
|
+
* Build a {@link ContextFactory} backed by `AsyncLocalStorage`. Pass it to
|
|
6
|
+
* `configure({ contextFactory: createAlsContext() })` and use {@link withContext}
|
|
7
|
+
* to enter an ambient scope.
|
|
8
|
+
*
|
|
9
|
+
* The active serializer is module-level state owned by this package; calling
|
|
10
|
+
* `configure` again with a different `createAlsContext()` factory replaces
|
|
11
|
+
* it, matching the "configure is a one-shot replace" semantics of the core.
|
|
12
|
+
*/
|
|
13
|
+
declare function createAlsContext(): ContextFactory;
|
|
14
|
+
/**
|
|
15
|
+
* Run `fn` with `bindings` added to the ambient context. Every log emitted
|
|
16
|
+
* (synchronously or across `await` boundaries) by a logger built from a
|
|
17
|
+
* configuration that wired in {@link createAlsContext} will carry the
|
|
18
|
+
* bindings, with no need to thread a child logger through call sites.
|
|
19
|
+
*
|
|
20
|
+
* Nested `withContext` calls stack: inner bindings are appended after outer
|
|
21
|
+
* ones, so on key collision the inner (most recent) value wins. A per-call
|
|
22
|
+
* field on the log site still overrides both.
|
|
23
|
+
*
|
|
24
|
+
* Empty bindings (`{}` or a payload that serializes away under the active
|
|
25
|
+
* redaction policy) skip the frame push entirely: `fn` runs in whatever
|
|
26
|
+
* scope the caller was already in.
|
|
27
|
+
*
|
|
28
|
+
* Throws when called before `configure({ contextFactory: createAlsContext() })`
|
|
29
|
+
* has wired up a serializer. A silent no-op would hide misconfiguration; a
|
|
30
|
+
* logger with ambient-context calls that never produce ambient-context fields
|
|
31
|
+
* is exactly the failure mode the throw is meant to surface.
|
|
32
|
+
*/
|
|
33
|
+
declare function withContext<T>(bindings: LogFields, fn: () => T): T;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { createAlsContext, withContext };
|
package/dist/main.mjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
|
|
3
|
+
//#region src/als-context.mts
|
|
4
|
+
const als = new AsyncLocalStorage();
|
|
5
|
+
let serialize = null;
|
|
6
|
+
/**
|
|
7
|
+
* Build a {@link ContextFactory} backed by `AsyncLocalStorage`. Pass it to
|
|
8
|
+
* `configure({ contextFactory: createAlsContext() })` and use {@link withContext}
|
|
9
|
+
* to enter an ambient scope.
|
|
10
|
+
*
|
|
11
|
+
* The active serializer is module-level state owned by this package; calling
|
|
12
|
+
* `configure` again with a different `createAlsContext()` factory replaces
|
|
13
|
+
* it, matching the "configure is a one-shot replace" semantics of the core.
|
|
14
|
+
*/
|
|
15
|
+
function createAlsContext() {
|
|
16
|
+
return { install(serializeBindings) {
|
|
17
|
+
serialize = serializeBindings;
|
|
18
|
+
const provider = () => als.getStore()?.serialized ?? "";
|
|
19
|
+
return provider;
|
|
20
|
+
} };
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Run `fn` with `bindings` added to the ambient context. Every log emitted
|
|
24
|
+
* (synchronously or across `await` boundaries) by a logger built from a
|
|
25
|
+
* configuration that wired in {@link createAlsContext} will carry the
|
|
26
|
+
* bindings, with no need to thread a child logger through call sites.
|
|
27
|
+
*
|
|
28
|
+
* Nested `withContext` calls stack: inner bindings are appended after outer
|
|
29
|
+
* ones, so on key collision the inner (most recent) value wins. A per-call
|
|
30
|
+
* field on the log site still overrides both.
|
|
31
|
+
*
|
|
32
|
+
* Empty bindings (`{}` or a payload that serializes away under the active
|
|
33
|
+
* redaction policy) skip the frame push entirely: `fn` runs in whatever
|
|
34
|
+
* scope the caller was already in.
|
|
35
|
+
*
|
|
36
|
+
* Throws when called before `configure({ contextFactory: createAlsContext() })`
|
|
37
|
+
* has wired up a serializer. A silent no-op would hide misconfiguration; a
|
|
38
|
+
* logger with ambient-context calls that never produce ambient-context fields
|
|
39
|
+
* is exactly the failure mode the throw is meant to surface.
|
|
40
|
+
*/
|
|
41
|
+
function withContext(bindings, fn) {
|
|
42
|
+
if (serialize === null) throw new Error("@ctrlback/logtau-context: configure({ contextFactory: createAlsContext() }) before withContext()");
|
|
43
|
+
const own = serialize(bindings);
|
|
44
|
+
if (own === "") return fn();
|
|
45
|
+
const parent = als.getStore();
|
|
46
|
+
const merged = parent ? { serialized: `${parent.serialized}${own}` } : { serialized: own };
|
|
47
|
+
return als.run(merged, fn);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
export { createAlsContext, withContext };
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ctrlback/logtau-context",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "AsyncLocalStorage-backed ambient context for @ctrlback/logtau.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Andres Correa Casablanca <andreu@ctrlback.coop>",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://forge.cloudsling.dev/CTRLback/logtau.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://forge.cloudsling.dev/CTRLback/logtau/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://forge.cloudsling.dev/CTRLback/logtau",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"logger",
|
|
17
|
+
"logging",
|
|
18
|
+
"log",
|
|
19
|
+
"structured-logging",
|
|
20
|
+
"typescript",
|
|
21
|
+
"context",
|
|
22
|
+
"async-context",
|
|
23
|
+
"async-local-storage",
|
|
24
|
+
"logtau"
|
|
25
|
+
],
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=22.6.0"
|
|
28
|
+
},
|
|
29
|
+
"type": "module",
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/main.d.mts",
|
|
34
|
+
"import": "./dist/main.mjs"
|
|
35
|
+
},
|
|
36
|
+
"./package.json": "./package.json"
|
|
37
|
+
},
|
|
38
|
+
"main": "./dist/main.mjs",
|
|
39
|
+
"types": "./dist/main.d.mts",
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"LICENSE.md",
|
|
43
|
+
"README.md"
|
|
44
|
+
],
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"@ctrlback/logtau": "^0.3.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@biomejs/biome": "^2.4.16",
|
|
53
|
+
"@stryker-mutator/core": "9.6.1",
|
|
54
|
+
"@stryker-mutator/vitest-runner": "9.6.1",
|
|
55
|
+
"@types/node": "^24.12.4",
|
|
56
|
+
"@vitest/coverage-v8": "^4.1.7",
|
|
57
|
+
"rolldown": "^1.0.3",
|
|
58
|
+
"rolldown-plugin-dts": "^0.25.1",
|
|
59
|
+
"typescript": "^6.0.3",
|
|
60
|
+
"vitest": "^4.1.7",
|
|
61
|
+
"@ctrlback/bench-harness": "0.0.0",
|
|
62
|
+
"@ctrlback/logtau": "0.3.0"
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"bench": "node bench/run-regression.mts",
|
|
66
|
+
"build": "rolldown -c rolldown.config.mjs",
|
|
67
|
+
"format": "biome check --write",
|
|
68
|
+
"lint": "biome check --error-on-warnings",
|
|
69
|
+
"test": "vitest run",
|
|
70
|
+
"test:coverage": "vitest run --coverage",
|
|
71
|
+
"test:mutate": "stryker run",
|
|
72
|
+
"typecheck": "tsc --noEmit"
|
|
73
|
+
}
|
|
74
|
+
}
|