@logtape/logtape 0.11.0-dev.175 → 0.12.0-dev.181
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/config.test.ts +591 -0
- package/config.ts +421 -0
- package/context.test.ts +187 -0
- package/context.ts +55 -0
- package/deno.json +36 -0
- package/dist/_virtual/rolldown_runtime.cjs +30 -0
- package/dist/config.cjs +247 -0
- package/dist/config.d.cts +189 -0
- package/dist/config.d.cts.map +1 -0
- package/dist/config.d.ts +189 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +241 -0
- package/dist/config.js.map +1 -0
- package/dist/context.cjs +30 -0
- package/dist/context.d.cts +39 -0
- package/dist/context.d.cts.map +1 -0
- package/dist/context.d.ts +39 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +31 -0
- package/dist/context.js.map +1 -0
- package/dist/filter.cjs +32 -0
- package/dist/filter.d.cts +37 -0
- package/dist/filter.d.cts.map +1 -0
- package/{types → dist}/filter.d.ts +12 -6
- package/dist/filter.d.ts.map +1 -0
- package/dist/filter.js +31 -0
- package/dist/filter.js.map +1 -0
- package/dist/formatter.cjs +281 -0
- package/dist/formatter.d.cts +338 -0
- package/dist/formatter.d.cts.map +1 -0
- package/dist/formatter.d.ts +338 -0
- package/dist/formatter.d.ts.map +1 -0
- package/dist/formatter.js +275 -0
- package/dist/formatter.js.map +1 -0
- package/dist/level.cjs +64 -0
- package/dist/level.d.cts +34 -0
- package/dist/level.d.cts.map +1 -0
- package/{types → dist}/level.d.ts +7 -5
- package/dist/level.d.ts.map +1 -0
- package/dist/level.js +62 -0
- package/dist/level.js.map +1 -0
- package/dist/logger.cjs +351 -0
- package/dist/logger.d.cts +501 -0
- package/dist/logger.d.cts.map +1 -0
- package/dist/logger.d.ts +501 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +351 -0
- package/dist/logger.js.map +1 -0
- package/dist/mod.cjs +33 -0
- package/dist/mod.d.cts +9 -0
- package/dist/mod.d.ts +9 -0
- package/dist/mod.js +9 -0
- package/dist/record.d.cts +50 -0
- package/dist/record.d.cts.map +1 -0
- package/dist/record.d.ts +50 -0
- package/dist/record.d.ts.map +1 -0
- package/dist/sink.cjs +95 -0
- package/dist/sink.d.cts +112 -0
- package/dist/sink.d.cts.map +1 -0
- package/{types → dist}/sink.d.ts +49 -45
- package/dist/sink.d.ts.map +1 -0
- package/dist/sink.js +94 -0
- package/dist/sink.js.map +1 -0
- package/dist/util.cjs +9 -0
- package/dist/util.d.cts +12 -0
- package/dist/util.d.cts.map +1 -0
- package/dist/util.d.ts +12 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.deno.cjs +16 -0
- package/dist/util.deno.d.cts +12 -0
- package/dist/util.deno.d.cts.map +1 -0
- package/dist/util.deno.d.ts +12 -0
- package/dist/util.deno.d.ts.map +1 -0
- package/dist/util.deno.js +16 -0
- package/dist/util.deno.js.map +1 -0
- package/dist/util.js +9 -0
- package/dist/util.js.map +1 -0
- package/dist/util.node.cjs +10 -0
- package/dist/util.node.d.cts +12 -0
- package/dist/util.node.d.cts.map +1 -0
- package/dist/util.node.d.ts +12 -0
- package/dist/util.node.d.ts.map +1 -0
- package/dist/util.node.js +10 -0
- package/dist/util.node.js.map +1 -0
- package/filter.test.ts +70 -0
- package/filter.ts +57 -0
- package/fixtures.ts +30 -0
- package/formatter.test.ts +530 -0
- package/formatter.ts +724 -0
- package/level.test.ts +47 -0
- package/level.ts +67 -0
- package/logger.test.ts +823 -0
- package/logger.ts +1124 -0
- package/mod.ts +54 -0
- package/package.json +35 -23
- package/record.ts +49 -0
- package/sink.test.ts +219 -0
- package/sink.ts +167 -0
- package/tsdown.config.ts +24 -0
- package/util.deno.ts +19 -0
- package/util.node.ts +12 -0
- package/util.ts +11 -0
- package/esm/_dnt.shims.js +0 -57
- package/esm/config.js +0 -297
- package/esm/context.js +0 -23
- package/esm/filter.js +0 -42
- package/esm/formatter.js +0 -370
- package/esm/level.js +0 -59
- package/esm/logger.js +0 -517
- package/esm/mod.js +0 -8
- package/esm/nodeUtil.cjs +0 -20
- package/esm/nodeUtil.js +0 -2
- package/esm/package.json +0 -3
- package/esm/record.js +0 -1
- package/esm/sink.js +0 -96
- package/script/_dnt.shims.js +0 -60
- package/script/config.js +0 -331
- package/script/context.js +0 -26
- package/script/filter.js +0 -46
- package/script/formatter.js +0 -380
- package/script/level.js +0 -64
- package/script/logger.js +0 -548
- package/script/mod.js +0 -36
- package/script/nodeUtil.js +0 -20
- package/script/package.json +0 -3
- package/script/record.js +0 -2
- package/script/sink.js +0 -101
- package/types/_dnt.shims.d.ts +0 -2
- package/types/_dnt.shims.d.ts.map +0 -1
- package/types/_dnt.test_shims.d.ts.map +0 -1
- package/types/config.d.ts +0 -183
- package/types/config.d.ts.map +0 -1
- package/types/config.test.d.ts.map +0 -1
- package/types/context.d.ts +0 -35
- package/types/context.d.ts.map +0 -1
- package/types/context.test.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/_constants.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/_diff.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/_format.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_equals.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_false.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_greater_or_equal.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_is_error.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_less_or_equal.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_rejects.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_strict_equals.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_throws.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assertion_error.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/equal.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/async/0.222.1/delay.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/fmt/0.222.1/colors.d.ts.map +0 -1
- package/types/filter.d.ts.map +0 -1
- package/types/filter.test.d.ts.map +0 -1
- package/types/fixtures.d.ts.map +0 -1
- package/types/formatter.d.ts +0 -332
- package/types/formatter.d.ts.map +0 -1
- package/types/formatter.test.d.ts.map +0 -1
- package/types/level.d.ts.map +0 -1
- package/types/level.test.d.ts.map +0 -1
- package/types/logger.d.ts +0 -573
- package/types/logger.d.ts.map +0 -1
- package/types/logger.test.d.ts.map +0 -1
- package/types/mod.d.ts +0 -9
- package/types/mod.d.ts.map +0 -1
- package/types/nodeUtil.d.ts +0 -12
- package/types/nodeUtil.d.ts.map +0 -1
- package/types/record.d.ts +0 -44
- package/types/record.d.ts.map +0 -1
- package/types/sink.d.ts.map +0 -1
- package/types/sink.test.d.ts.map +0 -1
package/mod.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export {
|
|
2
|
+
type Config,
|
|
3
|
+
ConfigError,
|
|
4
|
+
configure,
|
|
5
|
+
configureSync,
|
|
6
|
+
dispose,
|
|
7
|
+
disposeSync,
|
|
8
|
+
getConfig,
|
|
9
|
+
type LoggerConfig,
|
|
10
|
+
reset,
|
|
11
|
+
resetSync,
|
|
12
|
+
} from "./config.ts";
|
|
13
|
+
export { type ContextLocalStorage, withContext } from "./context.ts";
|
|
14
|
+
export {
|
|
15
|
+
type Filter,
|
|
16
|
+
type FilterLike,
|
|
17
|
+
getLevelFilter,
|
|
18
|
+
toFilter,
|
|
19
|
+
} from "./filter.ts";
|
|
20
|
+
export {
|
|
21
|
+
type AnsiColor,
|
|
22
|
+
ansiColorFormatter,
|
|
23
|
+
type AnsiColorFormatterOptions,
|
|
24
|
+
type AnsiStyle,
|
|
25
|
+
type ConsoleFormatter,
|
|
26
|
+
defaultConsoleFormatter,
|
|
27
|
+
defaultTextFormatter,
|
|
28
|
+
type FormattedValues,
|
|
29
|
+
getAnsiColorFormatter,
|
|
30
|
+
getJsonLinesFormatter,
|
|
31
|
+
getTextFormatter,
|
|
32
|
+
jsonLinesFormatter,
|
|
33
|
+
type JsonLinesFormatterOptions,
|
|
34
|
+
type TextFormatter,
|
|
35
|
+
type TextFormatterOptions,
|
|
36
|
+
} from "./formatter.ts";
|
|
37
|
+
export {
|
|
38
|
+
compareLogLevel,
|
|
39
|
+
isLogLevel,
|
|
40
|
+
type LogLevel,
|
|
41
|
+
parseLogLevel,
|
|
42
|
+
} from "./level.ts";
|
|
43
|
+
export { getLogger, type Logger } from "./logger.ts";
|
|
44
|
+
export type { LogRecord } from "./record.ts";
|
|
45
|
+
export {
|
|
46
|
+
type ConsoleSinkOptions,
|
|
47
|
+
getConsoleSink,
|
|
48
|
+
getStreamSink,
|
|
49
|
+
type Sink,
|
|
50
|
+
type StreamSinkOptions,
|
|
51
|
+
withFilter,
|
|
52
|
+
} from "./sink.ts";
|
|
53
|
+
|
|
54
|
+
// cSpell: ignore filesink
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logtape/logtape",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0-dev.181+e4f33b9d",
|
|
4
4
|
"description": "Simple logging library with zero dependencies for Deno/Node.js/Bun/browsers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"logging",
|
|
7
7
|
"log",
|
|
8
8
|
"logger"
|
|
9
9
|
],
|
|
10
|
+
"license": "MIT",
|
|
10
11
|
"author": {
|
|
11
12
|
"name": "Hong Minhee",
|
|
12
13
|
"email": "hong@minhee.org",
|
|
@@ -18,36 +19,47 @@
|
|
|
18
19
|
"url": "git+https://github.com/dahlia/logtape.git",
|
|
19
20
|
"directory": "logtape/"
|
|
20
21
|
},
|
|
21
|
-
"license": "MIT",
|
|
22
22
|
"bugs": {
|
|
23
23
|
"url": "https://github.com/dahlia/logtape/issues"
|
|
24
24
|
},
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
"funding": [
|
|
26
|
+
"https://github.com/sponsors/dahlia"
|
|
27
|
+
],
|
|
28
|
+
"type": "module",
|
|
29
|
+
"module": "./dist/mod.js",
|
|
30
|
+
"main": "./dist/mod.cjs",
|
|
31
|
+
"types": "./dist/mod.d.ts",
|
|
28
32
|
"exports": {
|
|
29
33
|
".": {
|
|
30
|
-
"import":
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"types": "./types/mod.d.ts",
|
|
36
|
-
"default": "./script/mod.js"
|
|
37
|
-
}
|
|
38
|
-
}
|
|
34
|
+
"import": "./dist/mod.js",
|
|
35
|
+
"require": "./dist/mod.cjs",
|
|
36
|
+
"types": "./dist/mod.d.ts"
|
|
37
|
+
},
|
|
38
|
+
"./package.json": "./package.json"
|
|
39
39
|
},
|
|
40
|
-
"
|
|
41
|
-
"
|
|
40
|
+
"imports": {
|
|
41
|
+
"#util": {
|
|
42
|
+
"node": "./dist/util.node.cjs",
|
|
43
|
+
"bun": "./dist/util.node.js",
|
|
44
|
+
"deno": "./dist/util.deno.js",
|
|
45
|
+
"import": "./dist/util.mjs",
|
|
46
|
+
"require": "./dist/util.js",
|
|
47
|
+
"types": "./dist/util.d.mts"
|
|
48
|
+
}
|
|
42
49
|
},
|
|
43
|
-
"funding": [
|
|
44
|
-
"https://github.com/sponsors/dahlia"
|
|
45
|
-
],
|
|
46
50
|
"devDependencies": {
|
|
47
|
-
"@
|
|
48
|
-
"
|
|
51
|
+
"@hongminhee/suite": "^0.6.2",
|
|
52
|
+
"@std/assert": "npm:@jsr/std__assert@^1.0.13",
|
|
53
|
+
"@std/async": "npm:@jsr/std__async@^1.0.13",
|
|
49
54
|
"consolemock": "^1.1.0",
|
|
50
|
-
"
|
|
55
|
+
"tsdown": "^0.12.7",
|
|
56
|
+
"typescript": "^5.8.3"
|
|
51
57
|
},
|
|
52
|
-
"
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "tsdown",
|
|
60
|
+
"test": "tsdown && node --experimental-transform-types --test",
|
|
61
|
+
"test:bun": "tsdown && bun test",
|
|
62
|
+
"test:deno": "deno test",
|
|
63
|
+
"test-all": "tsdown && node --experimental-transform-types --test && bun test && deno test"
|
|
64
|
+
}
|
|
53
65
|
}
|
package/record.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { LogLevel } from "./level.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A log record.
|
|
5
|
+
*/
|
|
6
|
+
export interface LogRecord {
|
|
7
|
+
/**
|
|
8
|
+
* The category of the logger that produced the log record.
|
|
9
|
+
*/
|
|
10
|
+
readonly category: readonly string[];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The log level.
|
|
14
|
+
*/
|
|
15
|
+
readonly level: LogLevel;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The log message. This is the result of substituting the message template
|
|
19
|
+
* with the values. The number of elements in this array is always odd,
|
|
20
|
+
* with the message template values interleaved between the substitution
|
|
21
|
+
* values.
|
|
22
|
+
*/
|
|
23
|
+
readonly message: readonly unknown[];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The raw log message. This is the original message template without any
|
|
27
|
+
* further processing. It can be either:
|
|
28
|
+
*
|
|
29
|
+
* - A string without any substitutions if the log record was created with
|
|
30
|
+
* a method call syntax, e.g., "Hello, {name}!" for
|
|
31
|
+
* `logger.info("Hello, {name}!", { name })`.
|
|
32
|
+
* - A template string array if the log record was created with a tagged
|
|
33
|
+
* template literal syntax, e.g., `["Hello, ", "!"]` for
|
|
34
|
+
* ``logger.info`Hello, ${name}!```.
|
|
35
|
+
*
|
|
36
|
+
* @since 0.6.0
|
|
37
|
+
*/
|
|
38
|
+
readonly rawMessage: string | TemplateStringsArray;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The timestamp of the log record in milliseconds since the Unix epoch.
|
|
42
|
+
*/
|
|
43
|
+
readonly timestamp: number;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The extra properties of the log record.
|
|
47
|
+
*/
|
|
48
|
+
readonly properties: Record<string, unknown>;
|
|
49
|
+
}
|
package/sink.test.ts
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { suite } from "@hongminhee/suite";
|
|
2
|
+
import { assertEquals } from "@std/assert/equals";
|
|
3
|
+
import { assertThrows } from "@std/assert/throws";
|
|
4
|
+
import makeConsoleMock from "consolemock";
|
|
5
|
+
import { debug, error, fatal, info, warning } from "./fixtures.ts";
|
|
6
|
+
import { defaultTextFormatter } from "./formatter.ts";
|
|
7
|
+
import type { LogLevel } from "./level.ts";
|
|
8
|
+
import type { LogRecord } from "./record.ts";
|
|
9
|
+
import { getConsoleSink, getStreamSink, withFilter } from "./sink.ts";
|
|
10
|
+
|
|
11
|
+
const test = suite(import.meta);
|
|
12
|
+
|
|
13
|
+
test("withFilter()", () => {
|
|
14
|
+
const buffer: LogRecord[] = [];
|
|
15
|
+
const sink = withFilter(buffer.push.bind(buffer), "warning");
|
|
16
|
+
sink(debug);
|
|
17
|
+
sink(info);
|
|
18
|
+
sink(warning);
|
|
19
|
+
sink(error);
|
|
20
|
+
sink(fatal);
|
|
21
|
+
assertEquals(buffer, [warning, error, fatal]);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
interface ConsoleMock extends Console {
|
|
25
|
+
history(): unknown[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
test("getStreamSink()", async () => {
|
|
29
|
+
let buffer: string = "";
|
|
30
|
+
const decoder = new TextDecoder();
|
|
31
|
+
const sink = getStreamSink(
|
|
32
|
+
new WritableStream({
|
|
33
|
+
write(chunk: Uint8Array) {
|
|
34
|
+
buffer += decoder.decode(chunk);
|
|
35
|
+
return Promise.resolve();
|
|
36
|
+
},
|
|
37
|
+
}),
|
|
38
|
+
);
|
|
39
|
+
sink(debug);
|
|
40
|
+
sink(info);
|
|
41
|
+
sink(warning);
|
|
42
|
+
sink(error);
|
|
43
|
+
sink(fatal);
|
|
44
|
+
await sink[Symbol.asyncDispose]();
|
|
45
|
+
assertEquals(
|
|
46
|
+
buffer,
|
|
47
|
+
`\
|
|
48
|
+
2023-11-14 22:13:20.000 +00:00 [DBG] my-app·junk: Hello, 123 & 456!
|
|
49
|
+
2023-11-14 22:13:20.000 +00:00 [INF] my-app·junk: Hello, 123 & 456!
|
|
50
|
+
2023-11-14 22:13:20.000 +00:00 [WRN] my-app·junk: Hello, 123 & 456!
|
|
51
|
+
2023-11-14 22:13:20.000 +00:00 [ERR] my-app·junk: Hello, 123 & 456!
|
|
52
|
+
2023-11-14 22:13:20.000 +00:00 [FTL] my-app·junk: Hello, 123 & 456!
|
|
53
|
+
`,
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("getConsoleSink()", () => {
|
|
58
|
+
// @ts-ignore: consolemock is not typed
|
|
59
|
+
const mock: ConsoleMock = makeConsoleMock();
|
|
60
|
+
const sink = getConsoleSink({ console: mock });
|
|
61
|
+
sink(debug);
|
|
62
|
+
sink(info);
|
|
63
|
+
sink(warning);
|
|
64
|
+
sink(error);
|
|
65
|
+
sink(fatal);
|
|
66
|
+
assertEquals(mock.history(), [
|
|
67
|
+
{
|
|
68
|
+
DEBUG: [
|
|
69
|
+
"%c22:13:20.000 %cDBG%c %cmy-app·junk %cHello, %o & %o!",
|
|
70
|
+
"color: gray;",
|
|
71
|
+
"background-color: gray; color: white;",
|
|
72
|
+
"background-color: default;",
|
|
73
|
+
"color: gray;",
|
|
74
|
+
"color: default;",
|
|
75
|
+
123,
|
|
76
|
+
456,
|
|
77
|
+
],
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
INFO: [
|
|
81
|
+
"%c22:13:20.000 %cINF%c %cmy-app·junk %cHello, %o & %o!",
|
|
82
|
+
"color: gray;",
|
|
83
|
+
"background-color: white; color: black;",
|
|
84
|
+
"background-color: default;",
|
|
85
|
+
"color: gray;",
|
|
86
|
+
"color: default;",
|
|
87
|
+
123,
|
|
88
|
+
456,
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
WARN: [
|
|
93
|
+
"%c22:13:20.000 %cWRN%c %cmy-app·junk %cHello, %o & %o!",
|
|
94
|
+
"color: gray;",
|
|
95
|
+
"background-color: orange; color: black;",
|
|
96
|
+
"background-color: default;",
|
|
97
|
+
"color: gray;",
|
|
98
|
+
"color: default;",
|
|
99
|
+
123,
|
|
100
|
+
456,
|
|
101
|
+
],
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
ERROR: [
|
|
105
|
+
"%c22:13:20.000 %cERR%c %cmy-app·junk %cHello, %o & %o!",
|
|
106
|
+
"color: gray;",
|
|
107
|
+
"background-color: red; color: white;",
|
|
108
|
+
"background-color: default;",
|
|
109
|
+
"color: gray;",
|
|
110
|
+
"color: default;",
|
|
111
|
+
123,
|
|
112
|
+
456,
|
|
113
|
+
],
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
ERROR: [
|
|
117
|
+
"%c22:13:20.000 %cFTL%c %cmy-app·junk %cHello, %o & %o!",
|
|
118
|
+
"color: gray;",
|
|
119
|
+
"background-color: maroon; color: white;",
|
|
120
|
+
"background-color: default;",
|
|
121
|
+
"color: gray;",
|
|
122
|
+
"color: default;",
|
|
123
|
+
123,
|
|
124
|
+
456,
|
|
125
|
+
],
|
|
126
|
+
},
|
|
127
|
+
]);
|
|
128
|
+
|
|
129
|
+
assertThrows(
|
|
130
|
+
() => sink({ ...info, level: "invalid" as LogLevel }),
|
|
131
|
+
TypeError,
|
|
132
|
+
"Invalid log level: invalid.",
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
// @ts-ignore: consolemock is not typed
|
|
136
|
+
const mock2: ConsoleMock = makeConsoleMock();
|
|
137
|
+
const sink2 = getConsoleSink({
|
|
138
|
+
console: mock2,
|
|
139
|
+
formatter: defaultTextFormatter,
|
|
140
|
+
});
|
|
141
|
+
sink2(debug);
|
|
142
|
+
sink2(info);
|
|
143
|
+
sink2(warning);
|
|
144
|
+
sink2(error);
|
|
145
|
+
sink2(fatal);
|
|
146
|
+
assertEquals(mock2.history(), [
|
|
147
|
+
{
|
|
148
|
+
DEBUG: [
|
|
149
|
+
"2023-11-14 22:13:20.000 +00:00 [DBG] my-app·junk: Hello, 123 & 456!",
|
|
150
|
+
],
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
INFO: [
|
|
154
|
+
"2023-11-14 22:13:20.000 +00:00 [INF] my-app·junk: Hello, 123 & 456!",
|
|
155
|
+
],
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
WARN: [
|
|
159
|
+
"2023-11-14 22:13:20.000 +00:00 [WRN] my-app·junk: Hello, 123 & 456!",
|
|
160
|
+
],
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
ERROR: [
|
|
164
|
+
"2023-11-14 22:13:20.000 +00:00 [ERR] my-app·junk: Hello, 123 & 456!",
|
|
165
|
+
],
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
ERROR: [
|
|
169
|
+
"2023-11-14 22:13:20.000 +00:00 [FTL] my-app·junk: Hello, 123 & 456!",
|
|
170
|
+
],
|
|
171
|
+
},
|
|
172
|
+
]);
|
|
173
|
+
|
|
174
|
+
// @ts-ignore: consolemock is not typed
|
|
175
|
+
const mock3: ConsoleMock = makeConsoleMock();
|
|
176
|
+
const sink3 = getConsoleSink({
|
|
177
|
+
console: mock3,
|
|
178
|
+
levelMap: {
|
|
179
|
+
debug: "log",
|
|
180
|
+
info: "log",
|
|
181
|
+
warning: "log",
|
|
182
|
+
error: "log",
|
|
183
|
+
fatal: "log",
|
|
184
|
+
},
|
|
185
|
+
formatter: defaultTextFormatter,
|
|
186
|
+
});
|
|
187
|
+
sink3(debug);
|
|
188
|
+
sink3(info);
|
|
189
|
+
sink3(warning);
|
|
190
|
+
sink3(error);
|
|
191
|
+
sink3(fatal);
|
|
192
|
+
assertEquals(mock3.history(), [
|
|
193
|
+
{
|
|
194
|
+
LOG: [
|
|
195
|
+
"2023-11-14 22:13:20.000 +00:00 [DBG] my-app·junk: Hello, 123 & 456!",
|
|
196
|
+
],
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
LOG: [
|
|
200
|
+
"2023-11-14 22:13:20.000 +00:00 [INF] my-app·junk: Hello, 123 & 456!",
|
|
201
|
+
],
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
LOG: [
|
|
205
|
+
"2023-11-14 22:13:20.000 +00:00 [WRN] my-app·junk: Hello, 123 & 456!",
|
|
206
|
+
],
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
LOG: [
|
|
210
|
+
"2023-11-14 22:13:20.000 +00:00 [ERR] my-app·junk: Hello, 123 & 456!",
|
|
211
|
+
],
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
LOG: [
|
|
215
|
+
"2023-11-14 22:13:20.000 +00:00 [FTL] my-app·junk: Hello, 123 & 456!",
|
|
216
|
+
],
|
|
217
|
+
},
|
|
218
|
+
]);
|
|
219
|
+
});
|
package/sink.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { type FilterLike, toFilter } from "./filter.ts";
|
|
2
|
+
import {
|
|
3
|
+
type ConsoleFormatter,
|
|
4
|
+
defaultConsoleFormatter,
|
|
5
|
+
defaultTextFormatter,
|
|
6
|
+
type TextFormatter,
|
|
7
|
+
} from "./formatter.ts";
|
|
8
|
+
import type { LogLevel } from "./level.ts";
|
|
9
|
+
import type { LogRecord } from "./record.ts";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A sink is a function that accepts a log record and prints it somewhere.
|
|
13
|
+
* Thrown exceptions will be suppressed and then logged to the meta logger,
|
|
14
|
+
* a {@link Logger} with the category `["logtape", "meta"]`. (In that case,
|
|
15
|
+
* the meta log record will not be passed to the sink to avoid infinite
|
|
16
|
+
* recursion.)
|
|
17
|
+
*
|
|
18
|
+
* @param record The log record to sink.
|
|
19
|
+
*/
|
|
20
|
+
export type Sink = (record: LogRecord) => void;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Turns a sink into a filtered sink. The returned sink only logs records that
|
|
24
|
+
* pass the filter.
|
|
25
|
+
*
|
|
26
|
+
* @example Filter a console sink to only log records with the info level
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const sink = withFilter(getConsoleSink(), "info");
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @param sink A sink to be filtered.
|
|
32
|
+
* @param filter A filter to apply to the sink. It can be either a filter
|
|
33
|
+
* function or a {@link LogLevel} string.
|
|
34
|
+
* @returns A sink that only logs records that pass the filter.
|
|
35
|
+
*/
|
|
36
|
+
export function withFilter(sink: Sink, filter: FilterLike): Sink {
|
|
37
|
+
const filterFunc = toFilter(filter);
|
|
38
|
+
return (record: LogRecord) => {
|
|
39
|
+
if (filterFunc(record)) sink(record);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Options for the {@link getStreamSink} function.
|
|
45
|
+
*/
|
|
46
|
+
export interface StreamSinkOptions {
|
|
47
|
+
/**
|
|
48
|
+
* The text formatter to use. Defaults to {@link defaultTextFormatter}.
|
|
49
|
+
*/
|
|
50
|
+
formatter?: TextFormatter;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The text encoder to use. Defaults to an instance of {@link TextEncoder}.
|
|
54
|
+
*/
|
|
55
|
+
encoder?: { encode(text: string): Uint8Array };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A factory that returns a sink that writes to a {@link WritableStream}.
|
|
60
|
+
*
|
|
61
|
+
* Note that the `stream` is of Web Streams API, which is different from
|
|
62
|
+
* Node.js streams. You can convert a Node.js stream to a Web Streams API
|
|
63
|
+
* stream using [`stream.Writable.toWeb()`] method.
|
|
64
|
+
*
|
|
65
|
+
* [`stream.Writable.toWeb()`]: https://nodejs.org/api/stream.html#streamwritabletowebstreamwritable
|
|
66
|
+
*
|
|
67
|
+
* @example Sink to the standard error in Deno
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const stderrSink = getStreamSink(Deno.stderr.writable);
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @example Sink to the standard error in Node.js
|
|
73
|
+
* ```typescript
|
|
74
|
+
* import stream from "node:stream";
|
|
75
|
+
* const stderrSink = getStreamSink(stream.Writable.toWeb(process.stderr));
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @param stream The stream to write to.
|
|
79
|
+
* @param options The options for the sink.
|
|
80
|
+
* @returns A sink that writes to the stream.
|
|
81
|
+
*/
|
|
82
|
+
export function getStreamSink(
|
|
83
|
+
stream: WritableStream,
|
|
84
|
+
options: StreamSinkOptions = {},
|
|
85
|
+
): Sink & AsyncDisposable {
|
|
86
|
+
const formatter = options.formatter ?? defaultTextFormatter;
|
|
87
|
+
const encoder = options.encoder ?? new TextEncoder();
|
|
88
|
+
const writer = stream.getWriter();
|
|
89
|
+
let lastPromise = Promise.resolve();
|
|
90
|
+
const sink: Sink & AsyncDisposable = (record: LogRecord) => {
|
|
91
|
+
const bytes = encoder.encode(formatter(record));
|
|
92
|
+
lastPromise = lastPromise
|
|
93
|
+
.then(() => writer.ready)
|
|
94
|
+
.then(() => writer.write(bytes));
|
|
95
|
+
};
|
|
96
|
+
sink[Symbol.asyncDispose] = async () => {
|
|
97
|
+
await lastPromise;
|
|
98
|
+
await writer.close();
|
|
99
|
+
};
|
|
100
|
+
return sink;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
type ConsoleMethod = "debug" | "info" | "log" | "warn" | "error";
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Options for the {@link getConsoleSink} function.
|
|
107
|
+
*/
|
|
108
|
+
export interface ConsoleSinkOptions {
|
|
109
|
+
/**
|
|
110
|
+
* The console formatter or text formatter to use.
|
|
111
|
+
* Defaults to {@link defaultConsoleFormatter}.
|
|
112
|
+
*/
|
|
113
|
+
formatter?: ConsoleFormatter | TextFormatter;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The mapping from log levels to console methods. Defaults to:
|
|
117
|
+
*
|
|
118
|
+
* ```typescript
|
|
119
|
+
* {
|
|
120
|
+
* debug: "debug",
|
|
121
|
+
* info: "info",
|
|
122
|
+
* warning: "warn",
|
|
123
|
+
* error: "error",
|
|
124
|
+
* fatal: "error",
|
|
125
|
+
* }
|
|
126
|
+
* ```
|
|
127
|
+
* @since 0.9.0
|
|
128
|
+
*/
|
|
129
|
+
levelMap?: Record<LogLevel, ConsoleMethod>;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The console to log to. Defaults to {@link console}.
|
|
133
|
+
*/
|
|
134
|
+
console?: Console;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* A console sink factory that returns a sink that logs to the console.
|
|
139
|
+
*
|
|
140
|
+
* @param options The options for the sink.
|
|
141
|
+
* @returns A sink that logs to the console.
|
|
142
|
+
*/
|
|
143
|
+
export function getConsoleSink(options: ConsoleSinkOptions = {}): Sink {
|
|
144
|
+
const formatter = options.formatter ?? defaultConsoleFormatter;
|
|
145
|
+
const levelMap: Record<LogLevel, ConsoleMethod> = {
|
|
146
|
+
debug: "debug",
|
|
147
|
+
info: "info",
|
|
148
|
+
warning: "warn",
|
|
149
|
+
error: "error",
|
|
150
|
+
fatal: "error",
|
|
151
|
+
...(options.levelMap ?? {}),
|
|
152
|
+
};
|
|
153
|
+
const console = options.console ?? globalThis.console;
|
|
154
|
+
return (record: LogRecord) => {
|
|
155
|
+
const args = formatter(record);
|
|
156
|
+
const method = levelMap[record.level];
|
|
157
|
+
if (method === undefined) {
|
|
158
|
+
throw new TypeError(`Invalid log level: ${record.level}.`);
|
|
159
|
+
}
|
|
160
|
+
if (typeof args === "string") {
|
|
161
|
+
const msg = args.replace(/\r?\n$/, "");
|
|
162
|
+
console[method](msg);
|
|
163
|
+
} else {
|
|
164
|
+
console[method](...args);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
}
|
package/tsdown.config.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { defineConfig } from "tsdown";
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
entry: ["mod.ts", "util.ts", "util.deno.ts", "util.node.ts"],
|
|
5
|
+
dts: {
|
|
6
|
+
sourcemap: true,
|
|
7
|
+
},
|
|
8
|
+
format: ["esm", "cjs"],
|
|
9
|
+
platform: "neutral",
|
|
10
|
+
unbundle: true,
|
|
11
|
+
inputOptions: {
|
|
12
|
+
onLog(level, log, defaultHandler) {
|
|
13
|
+
if (
|
|
14
|
+
level === "warn" && log.code === "UNRESOLVED_IMPORT" &&
|
|
15
|
+
["node:async_hooks", "node:util", "consolemock", "#util"].includes(
|
|
16
|
+
log.exporter ?? "",
|
|
17
|
+
)
|
|
18
|
+
) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
defaultHandler(level, log);
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
});
|
package/util.deno.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface InspectOptions {
|
|
2
|
+
colors?: boolean;
|
|
3
|
+
depth?: number | null;
|
|
4
|
+
compact?: boolean;
|
|
5
|
+
[key: string]: unknown;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function inspect(obj: unknown, options?: InspectOptions): string {
|
|
9
|
+
if ("Deno" in globalThis) {
|
|
10
|
+
return Deno.inspect(obj, {
|
|
11
|
+
colors: options?.colors,
|
|
12
|
+
depth: options?.depth ?? undefined,
|
|
13
|
+
compact: options?.compact ?? true,
|
|
14
|
+
});
|
|
15
|
+
} else {
|
|
16
|
+
const indent = options?.compact === true ? undefined : 2;
|
|
17
|
+
return JSON.stringify(obj, null, indent);
|
|
18
|
+
}
|
|
19
|
+
}
|
package/util.node.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import util from "node:util";
|
|
2
|
+
|
|
3
|
+
export interface InspectOptions {
|
|
4
|
+
colors?: boolean;
|
|
5
|
+
depth?: number | null;
|
|
6
|
+
compact?: boolean;
|
|
7
|
+
[key: string]: unknown;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function inspect(obj: unknown, options?: InspectOptions): string {
|
|
11
|
+
return util.inspect(obj, options);
|
|
12
|
+
}
|
package/util.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface InspectOptions {
|
|
2
|
+
colors?: boolean;
|
|
3
|
+
depth?: number | null;
|
|
4
|
+
compact?: boolean;
|
|
5
|
+
[key: string]: unknown;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function inspect(obj: unknown, options?: InspectOptions): string {
|
|
9
|
+
const indent = options?.compact === true ? undefined : 2;
|
|
10
|
+
return JSON.stringify(obj, null, indent);
|
|
11
|
+
}
|
package/esm/_dnt.shims.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
const dntGlobals = {};
|
|
2
|
-
export const dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
|
|
3
|
-
function createMergeProxy(baseObj, extObj) {
|
|
4
|
-
return new Proxy(baseObj, {
|
|
5
|
-
get(_target, prop, _receiver) {
|
|
6
|
-
if (prop in extObj) {
|
|
7
|
-
return extObj[prop];
|
|
8
|
-
}
|
|
9
|
-
else {
|
|
10
|
-
return baseObj[prop];
|
|
11
|
-
}
|
|
12
|
-
},
|
|
13
|
-
set(_target, prop, value) {
|
|
14
|
-
if (prop in extObj) {
|
|
15
|
-
delete extObj[prop];
|
|
16
|
-
}
|
|
17
|
-
baseObj[prop] = value;
|
|
18
|
-
return true;
|
|
19
|
-
},
|
|
20
|
-
deleteProperty(_target, prop) {
|
|
21
|
-
let success = false;
|
|
22
|
-
if (prop in extObj) {
|
|
23
|
-
delete extObj[prop];
|
|
24
|
-
success = true;
|
|
25
|
-
}
|
|
26
|
-
if (prop in baseObj) {
|
|
27
|
-
delete baseObj[prop];
|
|
28
|
-
success = true;
|
|
29
|
-
}
|
|
30
|
-
return success;
|
|
31
|
-
},
|
|
32
|
-
ownKeys(_target) {
|
|
33
|
-
const baseKeys = Reflect.ownKeys(baseObj);
|
|
34
|
-
const extKeys = Reflect.ownKeys(extObj);
|
|
35
|
-
const extKeysSet = new Set(extKeys);
|
|
36
|
-
return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
|
|
37
|
-
},
|
|
38
|
-
defineProperty(_target, prop, desc) {
|
|
39
|
-
if (prop in extObj) {
|
|
40
|
-
delete extObj[prop];
|
|
41
|
-
}
|
|
42
|
-
Reflect.defineProperty(baseObj, prop, desc);
|
|
43
|
-
return true;
|
|
44
|
-
},
|
|
45
|
-
getOwnPropertyDescriptor(_target, prop) {
|
|
46
|
-
if (prop in extObj) {
|
|
47
|
-
return Reflect.getOwnPropertyDescriptor(extObj, prop);
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
return Reflect.getOwnPropertyDescriptor(baseObj, prop);
|
|
51
|
-
}
|
|
52
|
-
},
|
|
53
|
-
has(_target, prop) {
|
|
54
|
-
return prop in extObj || prop in baseObj;
|
|
55
|
-
},
|
|
56
|
-
});
|
|
57
|
-
}
|