-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
244 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { assertEquals } from "@std/assert/assert-equals"; | ||
import { assertThrows } from "@std/assert/assert-throws"; | ||
import { delay } from "@std/async/delay"; | ||
import { AsyncLocalStorage } from "node:async_hooks"; | ||
import { configure, reset } from "./config.ts"; | ||
import { withContext } from "./context.ts"; | ||
import { getLogger } from "./logger.ts"; | ||
import type { LogRecord } from "./record.ts"; | ||
|
||
Deno.test("withContext()", async (t) => { | ||
const buffer: LogRecord[] = []; | ||
|
||
await t.step("set up", async () => { | ||
await configure({ | ||
sinks: { | ||
buffer: buffer.push.bind(buffer), | ||
}, | ||
loggers: [ | ||
{ category: "my-app", sinks: ["buffer"], level: "debug" }, | ||
{ category: ["logtape", "meta"], sinks: [], level: "warning" }, | ||
], | ||
contextLocalStorage: new AsyncLocalStorage(), | ||
reset: true, | ||
}); | ||
}); | ||
|
||
await t.step("test", () => { | ||
getLogger("my-app").debug("hello", { foo: 1, bar: 2 }); | ||
assertEquals(buffer, [ | ||
{ | ||
category: ["my-app"], | ||
level: "debug", | ||
message: ["hello"], | ||
rawMessage: "hello", | ||
properties: { foo: 1, bar: 2 }, | ||
timestamp: buffer[0].timestamp, | ||
}, | ||
]); | ||
buffer.pop(); | ||
const rv = withContext({ foo: 3, baz: 4 }, () => { | ||
getLogger("my-app").debug("world", { foo: 1, bar: 2 }); | ||
return 123; | ||
}); | ||
assertEquals(rv, 123); | ||
assertEquals(buffer, [ | ||
{ | ||
category: ["my-app"], | ||
level: "debug", | ||
message: ["world"], | ||
rawMessage: "world", | ||
properties: { foo: 1, bar: 2, baz: 4 }, | ||
timestamp: buffer[0].timestamp, | ||
}, | ||
]); | ||
buffer.pop(); | ||
getLogger("my-app").debug("hello", { foo: 1, bar: 2 }); | ||
assertEquals(buffer, [ | ||
{ | ||
category: ["my-app"], | ||
level: "debug", | ||
message: ["hello"], | ||
rawMessage: "hello", | ||
properties: { foo: 1, bar: 2 }, | ||
timestamp: buffer[0].timestamp, | ||
}, | ||
]); | ||
}); | ||
|
||
await t.step("nesting", () => { | ||
while (buffer.length > 0) buffer.pop(); | ||
withContext({ foo: 1, bar: 2 }, () => { | ||
withContext({ foo: 3, baz: 4 }, () => { | ||
getLogger("my-app").debug("hello"); | ||
}); | ||
}); | ||
assertEquals(buffer, [ | ||
{ | ||
category: ["my-app"], | ||
level: "debug", | ||
message: ["hello"], | ||
rawMessage: "hello", | ||
properties: { foo: 3, bar: 2, baz: 4 }, | ||
timestamp: buffer[0].timestamp, | ||
}, | ||
]); | ||
}); | ||
|
||
await t.step("concurrent runs", async () => { | ||
while (buffer.length > 0) buffer.pop(); | ||
await Promise.all([ | ||
(async () => { | ||
await delay(Math.random() * 100); | ||
withContext({ foo: 1 }, () => { | ||
getLogger("my-app").debug("foo"); | ||
}); | ||
})(), | ||
(async () => { | ||
await delay(Math.random() * 100); | ||
withContext({ bar: 2 }, () => { | ||
getLogger("my-app").debug("bar"); | ||
}); | ||
})(), | ||
(async () => { | ||
await delay(Math.random() * 100); | ||
withContext({ baz: 3 }, () => { | ||
getLogger("my-app").debug("baz"); | ||
}); | ||
})(), | ||
(async () => { | ||
await delay(Math.random() * 100); | ||
withContext({ qux: 4 }, () => { | ||
getLogger("my-app").debug("qux"); | ||
}); | ||
})(), | ||
]); | ||
assertEquals(buffer.length, 4); | ||
for (const log of buffer) { | ||
if (log.message[0] === "foo") { | ||
assertEquals(log.properties, { foo: 1 }); | ||
} else if (log.message[0] === "bar") { | ||
assertEquals(log.properties, { bar: 2 }); | ||
} else if (log.message[0] === "baz") { | ||
assertEquals(log.properties, { baz: 3 }); | ||
} else { | ||
assertEquals(log.properties, { qux: 4 }); | ||
} | ||
} | ||
}); | ||
|
||
await t.step("tear down", async () => { | ||
await reset(); | ||
}); | ||
|
||
await t.step("set up", async () => { | ||
await configure({ | ||
sinks: { | ||
buffer: buffer.push.bind(buffer), | ||
}, | ||
loggers: [ | ||
{ category: "my-app", sinks: ["buffer"], level: "debug" }, | ||
{ category: ["logtape", "meta"], sinks: [], level: "warning" }, | ||
], | ||
reset: true, | ||
}); | ||
}); | ||
|
||
await t.step("without settings", () => { | ||
assertThrows(() => withContext({}, () => {}), TypeError); | ||
}); | ||
|
||
await t.step("tear down", async () => { | ||
await reset(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { LoggerImpl } from "./logger.ts"; | ||
|
||
/** | ||
* A generic interface for a context-local storage. It resembles | ||
* the {@link AsyncLocalStorage} API from Node.js. | ||
* @typeParam T The type of the context-local store. | ||
* @since 0.7.0 | ||
*/ | ||
export interface ContextLocalStorage<T> { | ||
/** | ||
* Runs a callback with the given store as the context-local store. | ||
* @param store The store to use as the context-local store. | ||
* @param callback The callback to run. | ||
* @returns The return value of the callback. | ||
*/ | ||
run<R>(store: T, callback: () => R): R; | ||
|
||
/** | ||
* Returns the current context-local store. | ||
* @returns The current context-local store, or `undefined` if there is no | ||
* store. | ||
*/ | ||
getStore(): T | undefined; | ||
} | ||
|
||
/** | ||
* Runs a callback with the given implicit context. Every single log record | ||
* in the callback will have the given context. | ||
* @param context The context to inject. | ||
* @param callback The callback to run. | ||
* @returns The return value of the callback. | ||
* @since 0.7.0 | ||
*/ | ||
export function withContext<T>( | ||
context: Record<string, unknown>, | ||
callback: () => T, | ||
): T { | ||
const rootLogger = LoggerImpl.getLogger(); | ||
if (rootLogger.contextLocalStorage == null) { | ||
throw new TypeError( | ||
"Context-local storage is not configured. " + | ||
"Specify contextLocalStorage option in the configure() function.", | ||
); | ||
} | ||
const parentContext = rootLogger.contextLocalStorage.getStore() ?? {}; | ||
return rootLogger.contextLocalStorage.run( | ||
{ ...parentContext, ...context }, | ||
callback, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters