@holz/console-backend 0.0.0 → 0.2.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/dist/holz-console-backend.cjs +1 -0
- package/dist/holz-console-backend.js +21 -38
- package/package.json +7 -6
- package/src/__tests__/__snapshots__/console-backend.test.ts.snap +12 -20
- package/src/__tests__/console-backend.test.ts +17 -45
- package/src/console-backend.ts +6 -28
- package/CHANGELOG.md +0 -7
- package/dist/holz-console-backend.js.map +0 -1
- package/dist/holz-console-backend.umd.cjs +0 -2
- package/dist/holz-console-backend.umd.cjs.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});var t=(n=>(n.Error="error",n.Warn="warn",n.Info="info",n.Debug="debug",n))(t||{});class r{constructor(e={}){this.console=e.console??console}processLog(e){const c=[{include:!0,command:"%c%s",content:e.message,style:`color: ${e.level===t.Debug?"gray":"unset"}`},{include:Object.keys(e.context).length>0,command:"%c%o",content:e.context,style:""},{include:e.origin.length>0,command:"%c%s",content:e.origin.join(":"),style:"color: rgba(128, 128, 128, 0.6); font-style: italic"}].filter(o=>o.include),s=c.map(o=>o.command).join(" "),l=c.flatMap(o=>[o.style,o.content]),a=i[e.level];this.console[a](s,...l)}}const i={[t.Debug]:"debug",[t.Info]:"info",[t.Warn]:"warn",[t.Error]:"error"};exports.ConsoleBackend=r;exports.default=r;
|
|
@@ -1,60 +1,43 @@
|
|
|
1
|
-
var
|
|
1
|
+
var c = /* @__PURE__ */ ((e) => (e.Error = "error", e.Warn = "warn", e.Info = "info", e.Debug = "debug", e))(c || {});
|
|
2
2
|
class i {
|
|
3
|
-
constructor(
|
|
4
|
-
this.console =
|
|
3
|
+
constructor(n = {}) {
|
|
4
|
+
this.console = n.console ?? console;
|
|
5
5
|
}
|
|
6
|
-
processLog(
|
|
7
|
-
const
|
|
6
|
+
processLog(n) {
|
|
7
|
+
const t = [
|
|
8
8
|
{
|
|
9
9
|
include: !0,
|
|
10
10
|
command: "%c%s",
|
|
11
|
-
content:
|
|
12
|
-
style: `
|
|
11
|
+
content: n.message,
|
|
12
|
+
style: `color: ${n.level === c.Debug ? "gray" : "unset"}`
|
|
13
13
|
},
|
|
14
14
|
{
|
|
15
|
-
include:
|
|
16
|
-
command: "%c%s",
|
|
17
|
-
content: e.message,
|
|
18
|
-
style: `font-weight: normal; color: ${e.level === n.Debug ? "darkgray" : "unset"}`
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
include: Object.keys(e.context).length > 0,
|
|
15
|
+
include: Object.keys(n.context).length > 0,
|
|
22
16
|
command: "%c%o",
|
|
23
17
|
// Chrome hides object content with `%O`.
|
|
24
|
-
content:
|
|
18
|
+
content: n.context,
|
|
25
19
|
style: ""
|
|
26
20
|
},
|
|
27
21
|
{
|
|
28
|
-
include:
|
|
22
|
+
include: n.origin.length > 0,
|
|
29
23
|
command: "%c%s",
|
|
30
|
-
content:
|
|
31
|
-
style: "color:
|
|
24
|
+
content: n.origin.join(":"),
|
|
25
|
+
style: "color: rgba(128, 128, 128, 0.6); font-style: italic"
|
|
32
26
|
}
|
|
33
|
-
].filter((
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
]), a =
|
|
37
|
-
this.console[a](
|
|
27
|
+
].filter((o) => o.include), r = t.map((o) => o.command).join(" "), s = t.flatMap((o) => [
|
|
28
|
+
o.style,
|
|
29
|
+
o.content
|
|
30
|
+
]), a = l[n.level];
|
|
31
|
+
this.console[a](r, ...s);
|
|
38
32
|
}
|
|
39
33
|
}
|
|
40
34
|
const l = {
|
|
41
|
-
[
|
|
42
|
-
[
|
|
43
|
-
[
|
|
44
|
-
[
|
|
45
|
-
}, u = {
|
|
46
|
-
[n.Debug]: "DEBUG",
|
|
47
|
-
[n.Info]: "INFO ",
|
|
48
|
-
[n.Warn]: "WARN ",
|
|
49
|
-
[n.Error]: "ERROR"
|
|
50
|
-
}, d = {
|
|
51
|
-
[n.Debug]: "log",
|
|
52
|
-
[n.Info]: "log",
|
|
53
|
-
[n.Warn]: "warn",
|
|
54
|
-
[n.Error]: "error"
|
|
35
|
+
[c.Debug]: "debug",
|
|
36
|
+
[c.Info]: "info",
|
|
37
|
+
[c.Warn]: "warn",
|
|
38
|
+
[c.Error]: "error"
|
|
55
39
|
};
|
|
56
40
|
export {
|
|
57
41
|
i as ConsoleBackend,
|
|
58
42
|
i as default
|
|
59
43
|
};
|
|
60
|
-
//# sourceMappingURL=holz-console-backend.js.map
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@holz/console-backend",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A console backend for Holz",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./dist/holz-console-backend.
|
|
6
|
+
"main": "./dist/holz-console-backend.cjs",
|
|
7
7
|
"module": "./dist/holz-console-backend.js",
|
|
8
8
|
"types": "./src/index.ts",
|
|
9
9
|
"repository": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"exports": {
|
|
15
15
|
".": {
|
|
16
|
-
"require": "./dist/holz-console-backend.
|
|
16
|
+
"require": "./dist/holz-console-backend.cjs",
|
|
17
17
|
"import": "./dist/holz-console-backend.js"
|
|
18
18
|
}
|
|
19
19
|
},
|
|
@@ -34,14 +34,15 @@
|
|
|
34
34
|
"debugger"
|
|
35
35
|
],
|
|
36
36
|
"scripts": {
|
|
37
|
-
"
|
|
37
|
+
"prepack": "vite build",
|
|
38
38
|
"test:unit": "vitest --color --passWithNoTests",
|
|
39
|
-
"test:
|
|
39
|
+
"test:types": "tsc"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@holz/core": "0.
|
|
42
|
+
"@holz/core": "0.2.0",
|
|
43
43
|
"@types/node": "^18.14.0",
|
|
44
44
|
"@vitest/coverage-c8": "0.28.5",
|
|
45
|
+
"typescript": "4.9.5",
|
|
45
46
|
"vite": "^4.0.0",
|
|
46
47
|
"vitest": "^0.28.5"
|
|
47
48
|
}
|
|
@@ -2,64 +2,56 @@
|
|
|
2
2
|
|
|
3
3
|
exports[`Console backend > avoids changing debug messages 1`] = `
|
|
4
4
|
[
|
|
5
|
-
"%c%s %c%
|
|
6
|
-
"
|
|
7
|
-
"DEBUG",
|
|
8
|
-
"font-weight: normal; color: darkgray",
|
|
5
|
+
"%c%s %c%o %c%s",
|
|
6
|
+
"color: gray",
|
|
9
7
|
"just spam",
|
|
10
8
|
"",
|
|
11
9
|
{
|
|
12
10
|
"id": 1234,
|
|
13
11
|
},
|
|
14
|
-
"color:
|
|
12
|
+
"color: rgba(128, 128, 128, 0.6); font-style: italic",
|
|
15
13
|
"ns-1:ns-2",
|
|
16
14
|
]
|
|
17
15
|
`;
|
|
18
16
|
|
|
19
17
|
exports[`Console backend > avoids changing error messages 1`] = `
|
|
20
18
|
[
|
|
21
|
-
"%c%s %c%
|
|
22
|
-
"
|
|
23
|
-
"ERROR",
|
|
24
|
-
"font-weight: normal; color: unset",
|
|
19
|
+
"%c%s %c%o %c%s",
|
|
20
|
+
"color: unset",
|
|
25
21
|
"some error message",
|
|
26
22
|
"",
|
|
27
23
|
{
|
|
28
24
|
"id": 1234,
|
|
29
25
|
},
|
|
30
|
-
"color:
|
|
26
|
+
"color: rgba(128, 128, 128, 0.6); font-style: italic",
|
|
31
27
|
"ns-1:ns-2",
|
|
32
28
|
]
|
|
33
29
|
`;
|
|
34
30
|
|
|
35
31
|
exports[`Console backend > avoids changing info messages 1`] = `
|
|
36
32
|
[
|
|
37
|
-
"%c%s %c%
|
|
38
|
-
"
|
|
39
|
-
"INFO ",
|
|
40
|
-
"font-weight: normal; color: unset",
|
|
33
|
+
"%c%s %c%o %c%s",
|
|
34
|
+
"color: unset",
|
|
41
35
|
"a little info",
|
|
42
36
|
"",
|
|
43
37
|
{
|
|
44
38
|
"id": 1234,
|
|
45
39
|
},
|
|
46
|
-
"color:
|
|
40
|
+
"color: rgba(128, 128, 128, 0.6); font-style: italic",
|
|
47
41
|
"ns-1:ns-2",
|
|
48
42
|
]
|
|
49
43
|
`;
|
|
50
44
|
|
|
51
45
|
exports[`Console backend > avoids changing warn messages 1`] = `
|
|
52
46
|
[
|
|
53
|
-
"%c%s %c%
|
|
54
|
-
"
|
|
55
|
-
"WARN ",
|
|
56
|
-
"font-weight: normal; color: unset",
|
|
47
|
+
"%c%s %c%o %c%s",
|
|
48
|
+
"color: unset",
|
|
57
49
|
"a warning",
|
|
58
50
|
"",
|
|
59
51
|
{
|
|
60
52
|
"id": 1234,
|
|
61
53
|
},
|
|
62
|
-
"color:
|
|
54
|
+
"color: rgba(128, 128, 128, 0.6); font-style: italic",
|
|
63
55
|
"ns-1:ns-2",
|
|
64
56
|
]
|
|
65
57
|
`;
|
|
@@ -4,20 +4,17 @@ import type { MinimalConsole } from '../console-backend';
|
|
|
4
4
|
import ConsoleBackend from '../console-backend';
|
|
5
5
|
|
|
6
6
|
class MockConsole implements MinimalConsole {
|
|
7
|
-
|
|
8
|
-
this.
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
warn = vi.fn((...strings: Array<unknown>) => {
|
|
12
|
-
this.stdout(format(...strings));
|
|
13
|
-
});
|
|
7
|
+
private fmt = (level: string, ...strings: Array<unknown>) => {
|
|
8
|
+
this.print(format(level, ...strings));
|
|
9
|
+
};
|
|
14
10
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
debug = vi.fn(this.fmt);
|
|
12
|
+
info = vi.fn(this.fmt);
|
|
13
|
+
warn = vi.fn(this.fmt);
|
|
14
|
+
error = vi.fn(this.fmt);
|
|
18
15
|
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
// Approximation of what gets printed.
|
|
17
|
+
print = vi.fn();
|
|
21
18
|
}
|
|
22
19
|
|
|
23
20
|
describe('Console backend', () => {
|
|
@@ -28,32 +25,11 @@ describe('Console backend', () => {
|
|
|
28
25
|
const logger = createLogger(backend);
|
|
29
26
|
logger.info('hello world');
|
|
30
27
|
|
|
31
|
-
expect(output.
|
|
28
|
+
expect(output.print).toHaveBeenCalledWith(
|
|
32
29
|
expect.stringContaining('hello world')
|
|
33
30
|
);
|
|
34
31
|
});
|
|
35
32
|
|
|
36
|
-
it('includes the log level', () => {
|
|
37
|
-
const output = new MockConsole();
|
|
38
|
-
const backend = new ConsoleBackend({ console: output });
|
|
39
|
-
|
|
40
|
-
const logger = createLogger(backend);
|
|
41
|
-
logger.debug('shout');
|
|
42
|
-
logger.info('normal');
|
|
43
|
-
logger.warn('hmmmm');
|
|
44
|
-
logger.error('oh no');
|
|
45
|
-
|
|
46
|
-
expect(output.stdout).toHaveBeenCalledWith(
|
|
47
|
-
expect.stringContaining('DEBUG')
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
expect(output.stdout).toHaveBeenCalledWith(expect.stringContaining('INFO'));
|
|
51
|
-
expect(output.stdout).toHaveBeenCalledWith(expect.stringContaining('WARN'));
|
|
52
|
-
expect(output.stderr).toHaveBeenCalledWith(
|
|
53
|
-
expect.stringContaining('ERROR')
|
|
54
|
-
);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
33
|
it('includes the log namespace', () => {
|
|
58
34
|
const output = new MockConsole();
|
|
59
35
|
const backend = new ConsoleBackend({ console: output });
|
|
@@ -63,7 +39,7 @@ describe('Console backend', () => {
|
|
|
63
39
|
|
|
64
40
|
logger.debug('initialized');
|
|
65
41
|
|
|
66
|
-
expect(output.
|
|
42
|
+
expect(output.print).toHaveBeenCalledWith(
|
|
67
43
|
expect.stringContaining('my-lib:MyClass')
|
|
68
44
|
);
|
|
69
45
|
});
|
|
@@ -76,11 +52,11 @@ describe('Console backend', () => {
|
|
|
76
52
|
logger.info('creating session', { sessionId: 3109 });
|
|
77
53
|
|
|
78
54
|
// Hard to test without replicating the implementation.
|
|
79
|
-
expect(output.
|
|
55
|
+
expect(output.print).toHaveBeenCalledWith(
|
|
80
56
|
expect.stringContaining('sessionId')
|
|
81
57
|
);
|
|
82
58
|
|
|
83
|
-
expect(output.
|
|
59
|
+
expect(output.print).toHaveBeenCalledWith(expect.stringContaining('3109'));
|
|
84
60
|
});
|
|
85
61
|
|
|
86
62
|
it('does not include the log context if it is empty', () => {
|
|
@@ -91,20 +67,16 @@ describe('Console backend', () => {
|
|
|
91
67
|
logger.warn('activating death ray', {});
|
|
92
68
|
|
|
93
69
|
// Hard to test without replicating the implementation.
|
|
94
|
-
expect(output.
|
|
95
|
-
expect.stringContaining('{')
|
|
96
|
-
);
|
|
70
|
+
expect(output.print).not.toHaveBeenCalledWith(expect.stringContaining('{'));
|
|
97
71
|
|
|
98
|
-
expect(output.
|
|
99
|
-
expect.stringContaining('}')
|
|
100
|
-
);
|
|
72
|
+
expect(output.print).not.toHaveBeenCalledWith(expect.stringContaining('}'));
|
|
101
73
|
});
|
|
102
74
|
|
|
103
75
|
// Snapshot the exact output of each log level. This mostly prevents
|
|
104
76
|
// regressions while doing innocent refactors.
|
|
105
77
|
it.each([
|
|
106
|
-
['debug' as const, 'just spam', ['ns-1', 'ns-2'], '
|
|
107
|
-
['info' as const, 'a little info', ['ns-1', 'ns-2'], '
|
|
78
|
+
['debug' as const, 'just spam', ['ns-1', 'ns-2'], 'debug' as const],
|
|
79
|
+
['info' as const, 'a little info', ['ns-1', 'ns-2'], 'info' as const],
|
|
108
80
|
['warn' as const, 'a warning', ['ns-1', 'ns-2'], 'warn' as const],
|
|
109
81
|
[
|
|
110
82
|
'error' as const,
|
package/src/console-backend.ts
CHANGED
|
@@ -14,19 +14,11 @@ export default class ConsoleBackend implements LogProcessor {
|
|
|
14
14
|
|
|
15
15
|
processLog(log: Log) {
|
|
16
16
|
const segments = [
|
|
17
|
-
{
|
|
18
|
-
include: true,
|
|
19
|
-
command: '%c%s',
|
|
20
|
-
content: LOG_LEVEL_TITLE[log.level],
|
|
21
|
-
style: `font-weight: bold; color: ${LOG_LEVEL_STYLE[log.level]}`,
|
|
22
|
-
},
|
|
23
17
|
{
|
|
24
18
|
include: true,
|
|
25
19
|
command: '%c%s',
|
|
26
20
|
content: log.message,
|
|
27
|
-
style: `
|
|
28
|
-
log.level === LogLevel.Debug ? 'darkgray' : 'unset'
|
|
29
|
-
}`,
|
|
21
|
+
style: `color: ${log.level === LogLevel.Debug ? 'gray' : 'unset'}`,
|
|
30
22
|
},
|
|
31
23
|
{
|
|
32
24
|
include: Object.keys(log.context).length > 0,
|
|
@@ -38,7 +30,7 @@ export default class ConsoleBackend implements LogProcessor {
|
|
|
38
30
|
include: log.origin.length > 0,
|
|
39
31
|
command: '%c%s',
|
|
40
32
|
content: log.origin.join(':'),
|
|
41
|
-
style: 'color:
|
|
33
|
+
style: 'color: rgba(128, 128, 128, 0.6); font-style: italic',
|
|
42
34
|
},
|
|
43
35
|
].filter((segment) => segment.include);
|
|
44
36
|
|
|
@@ -55,24 +47,10 @@ export default class ConsoleBackend implements LogProcessor {
|
|
|
55
47
|
}
|
|
56
48
|
}
|
|
57
49
|
|
|
58
|
-
//
|
|
59
|
-
const LOG_LEVEL_STYLE: Record<LogLevel, string> = {
|
|
60
|
-
[LogLevel.Debug]: 'darkgray',
|
|
61
|
-
[LogLevel.Info]: 'darkturquoise',
|
|
62
|
-
[LogLevel.Warn]: 'unset',
|
|
63
|
-
[LogLevel.Error]: 'unset',
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
const LOG_LEVEL_TITLE: Record<LogLevel, string> = {
|
|
67
|
-
[LogLevel.Debug]: 'DEBUG',
|
|
68
|
-
[LogLevel.Info]: 'INFO ',
|
|
69
|
-
[LogLevel.Warn]: 'WARN ',
|
|
70
|
-
[LogLevel.Error]: 'ERROR',
|
|
71
|
-
};
|
|
72
|
-
|
|
50
|
+
// Browsers have UIs for filtering by log level. Leverage that.
|
|
73
51
|
const LOG_METHOD: Record<LogLevel, keyof MinimalConsole> = {
|
|
74
|
-
[LogLevel.Debug]: '
|
|
75
|
-
[LogLevel.Info]: '
|
|
52
|
+
[LogLevel.Debug]: 'debug',
|
|
53
|
+
[LogLevel.Info]: 'info',
|
|
76
54
|
[LogLevel.Warn]: 'warn',
|
|
77
55
|
[LogLevel.Error]: 'error',
|
|
78
56
|
};
|
|
@@ -85,4 +63,4 @@ interface Options {
|
|
|
85
63
|
* A subset of the Console interface. Must support printf-style interpolation.
|
|
86
64
|
* @see https://console.spec.whatwg.org/#formatting-specifiers
|
|
87
65
|
*/
|
|
88
|
-
export type MinimalConsole = Pick<Console, '
|
|
66
|
+
export type MinimalConsole = Pick<Console, 'debug' | 'info' | 'warn' | 'error'>;
|
package/CHANGELOG.md
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file.
|
|
4
|
-
|
|
5
|
-
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
-
|
|
7
|
-
## Unreleased
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"holz-console-backend.js","sources":["../../holz-core/dist/holz-core.js","../src/console-backend.ts"],"sourcesContent":["var t = /* @__PURE__ */ ((s) => (s.Error = \"error\", s.Warn = \"warn\", s.Info = \"info\", s.Debug = \"debug\", s))(t || {});\nclass e {\n constructor(r, o) {\n this.processor = r, this.origin = o;\n }\n static create(r) {\n return new e(r, []);\n }\n /** Extend the logger to attach a class or module name to logs. */\n namespace(r) {\n return new e(this.processor, this.origin.concat(r));\n }\n /** Log a frequent and verbose progress update. */\n debug(r, o) {\n this.forwardLog(t.Debug, r, o);\n }\n /** Log a high-level progress update. */\n info(r, o) {\n this.forwardLog(t.Info, r, o);\n }\n /** Log something concerning. */\n warn(r, o) {\n this.forwardLog(t.Warn, r, o);\n }\n /** Log a critical failure. */\n error(r, o) {\n this.forwardLog(t.Error, r, o);\n }\n forwardLog(r, o, c = {}) {\n this.processor.processLog({\n message: o,\n level: r,\n origin: this.origin,\n context: c\n });\n }\n}\nconst a = e.create;\nclass i {\n constructor(r) {\n this.processors = r;\n }\n processLog(r) {\n this.processors.forEach((o) => {\n o.processLog(r);\n });\n }\n}\nfunction h(s) {\n return new i(s);\n}\nclass n {\n constructor(r, o) {\n this.predicate = r, this.processor = o;\n }\n processLog(r) {\n this.predicate(r) && this.processor.processLog(r);\n }\n}\nfunction g(s, r) {\n return new n(s, r);\n}\nexport {\n t as LogLevel,\n h as combine,\n a as createLogger,\n g as filter\n};\n//# sourceMappingURL=holz-core.js.map\n","import type { Log, LogProcessor } from '@holz/core';\nimport { LogLevel } from '@holz/core';\n\n/**\n * A backend that pretty-prints logs to a browser console, or any\n * remote-attached console.\n */\nexport default class ConsoleBackend implements LogProcessor {\n private console: MinimalConsole;\n\n constructor(options: Options = {}) {\n this.console = options.console ?? console;\n }\n\n processLog(log: Log) {\n const segments = [\n {\n include: true,\n command: '%c%s',\n content: LOG_LEVEL_TITLE[log.level],\n style: `font-weight: bold; color: ${LOG_LEVEL_STYLE[log.level]}`,\n },\n {\n include: true,\n command: '%c%s',\n content: log.message,\n style: `font-weight: normal; color: ${\n log.level === LogLevel.Debug ? 'darkgray' : 'unset'\n }`,\n },\n {\n include: Object.keys(log.context).length > 0,\n command: '%c%o', // Chrome hides object content with `%O`.\n content: log.context,\n style: '',\n },\n {\n include: log.origin.length > 0,\n command: '%c%s',\n content: log.origin.join(':'),\n style: 'color: gray',\n },\n ].filter((segment) => segment.include);\n\n const command = segments.map((segment) => segment.command).join(' ');\n const values = segments.flatMap((segment) => [\n segment.style,\n segment.content,\n ]);\n\n // Errors should always use stderr.\n const channel = LOG_METHOD[log.level];\n\n this.console[channel](command, ...values);\n }\n}\n\n// Before changing these, verify contrast on dark/light themes.\nconst LOG_LEVEL_STYLE: Record<LogLevel, string> = {\n [LogLevel.Debug]: 'darkgray',\n [LogLevel.Info]: 'darkturquoise',\n [LogLevel.Warn]: 'unset',\n [LogLevel.Error]: 'unset',\n};\n\nconst LOG_LEVEL_TITLE: Record<LogLevel, string> = {\n [LogLevel.Debug]: 'DEBUG',\n [LogLevel.Info]: 'INFO ',\n [LogLevel.Warn]: 'WARN ',\n [LogLevel.Error]: 'ERROR',\n};\n\nconst LOG_METHOD: Record<LogLevel, keyof MinimalConsole> = {\n [LogLevel.Debug]: 'log',\n [LogLevel.Info]: 'log',\n [LogLevel.Warn]: 'warn',\n [LogLevel.Error]: 'error',\n};\n\ninterface Options {\n console?: MinimalConsole;\n}\n\n/**\n * A subset of the Console interface. Must support printf-style interpolation.\n * @see https://console.spec.whatwg.org/#formatting-specifiers\n */\nexport type MinimalConsole = Pick<Console, 'log' | 'warn' | 'error'>;\n"],"names":["t","s","ConsoleBackend","options","log","segments","LOG_LEVEL_TITLE","LOG_LEVEL_STYLE","LogLevel","segment","command","values","channel","LOG_METHOD"],"mappings":"AAAA,IAAIA,IAAqB,kBAACC,OAAOA,EAAE,QAAQ,SAASA,EAAE,OAAO,QAAQA,EAAE,OAAO,QAAQA,EAAE,QAAQ,SAASA,IAAID,KAAK,EAAE;ACOpH,MAAqBE,EAAuC;AAAA,EAG1D,YAAYC,IAAmB,IAAI;AAC5B,SAAA,UAAUA,EAAQ,WAAW;AAAA,EACpC;AAAA,EAEA,WAAWC,GAAU;AACnB,UAAMC,IAAW;AAAA,MACf;AAAA,QACE,SAAS;AAAA,QACT,SAAS;AAAA,QACT,SAASC,EAAgBF,EAAI,KAAK;AAAA,QAClC,OAAO,6BAA6BG,EAAgBH,EAAI,KAAK;AAAA,MAC/D;AAAA,MACA;AAAA,QACE,SAAS;AAAA,QACT,SAAS;AAAA,QACT,SAASA,EAAI;AAAA,QACb,OAAO,+BACLA,EAAI,UAAUI,EAAS,QAAQ,aAAa;AAAA,MAEhD;AAAA,MACA;AAAA,QACE,SAAS,OAAO,KAAKJ,EAAI,OAAO,EAAE,SAAS;AAAA,QAC3C,SAAS;AAAA;AAAA,QACT,SAASA,EAAI;AAAA,QACb,OAAO;AAAA,MACT;AAAA,MACA;AAAA,QACE,SAASA,EAAI,OAAO,SAAS;AAAA,QAC7B,SAAS;AAAA,QACT,SAASA,EAAI,OAAO,KAAK,GAAG;AAAA,QAC5B,OAAO;AAAA,MACT;AAAA,IACA,EAAA,OAAO,CAACK,MAAYA,EAAQ,OAAO,GAE/BC,IAAUL,EAAS,IAAI,CAACI,MAAYA,EAAQ,OAAO,EAAE,KAAK,GAAG,GAC7DE,IAASN,EAAS,QAAQ,CAACI,MAAY;AAAA,MAC3CA,EAAQ;AAAA,MACRA,EAAQ;AAAA,IAAA,CACT,GAGKG,IAAUC,EAAWT,EAAI,KAAK;AAEpC,SAAK,QAAQQ,CAAO,EAAEF,GAAS,GAAGC,CAAM;AAAA,EAC1C;AACF;AAGA,MAAMJ,IAA4C;AAAA,EAChD,CAACC,EAAS,KAAK,GAAG;AAAA,EAClB,CAACA,EAAS,IAAI,GAAG;AAAA,EACjB,CAACA,EAAS,IAAI,GAAG;AAAA,EACjB,CAACA,EAAS,KAAK,GAAG;AACpB,GAEMF,IAA4C;AAAA,EAChD,CAACE,EAAS,KAAK,GAAG;AAAA,EAClB,CAACA,EAAS,IAAI,GAAG;AAAA,EACjB,CAACA,EAAS,IAAI,GAAG;AAAA,EACjB,CAACA,EAAS,KAAK,GAAG;AACpB,GAEMK,IAAqD;AAAA,EACzD,CAACL,EAAS,KAAK,GAAG;AAAA,EAClB,CAACA,EAAS,IAAI,GAAG;AAAA,EACjB,CAACA,EAAS,IAAI,GAAG;AAAA,EACjB,CAACA,EAAS,KAAK,GAAG;AACpB;"}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
(function(o,e){typeof exports=="object"&&typeof module<"u"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(o=typeof globalThis<"u"?globalThis:o||self,e(o["holz-console-backend"]={}))})(this,function(o){"use strict";var e=(t=>(t.Error="error",t.Warn="warn",t.Info="info",t.Debug="debug",t))(e||{});class r{constructor(n={}){this.console=n.console??console}processLog(n){const s=[{include:!0,command:"%c%s",content:a[n.level],style:`font-weight: bold; color: ${l[n.level]}`},{include:!0,command:"%c%s",content:n.message,style:`font-weight: normal; color: ${n.level===e.Debug?"darkgray":"unset"}`},{include:Object.keys(n.context).length>0,command:"%c%o",content:n.context,style:""},{include:n.origin.length>0,command:"%c%s",content:n.origin.join(":"),style:"color: gray"}].filter(c=>c.include),u=s.map(c=>c.command).join(" "),d=s.flatMap(c=>[c.style,c.content]),f=i[n.level];this.console[f](u,...d)}}const l={[e.Debug]:"darkgray",[e.Info]:"darkturquoise",[e.Warn]:"unset",[e.Error]:"unset"},a={[e.Debug]:"DEBUG",[e.Info]:"INFO ",[e.Warn]:"WARN ",[e.Error]:"ERROR"},i={[e.Debug]:"log",[e.Info]:"log",[e.Warn]:"warn",[e.Error]:"error"};o.ConsoleBackend=r,o.default=r,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
|
2
|
-
//# sourceMappingURL=holz-console-backend.umd.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"holz-console-backend.umd.cjs","sources":["../../holz-core/dist/holz-core.js","../src/console-backend.ts"],"sourcesContent":["var t = /* @__PURE__ */ ((s) => (s.Error = \"error\", s.Warn = \"warn\", s.Info = \"info\", s.Debug = \"debug\", s))(t || {});\nclass e {\n constructor(r, o) {\n this.processor = r, this.origin = o;\n }\n static create(r) {\n return new e(r, []);\n }\n /** Extend the logger to attach a class or module name to logs. */\n namespace(r) {\n return new e(this.processor, this.origin.concat(r));\n }\n /** Log a frequent and verbose progress update. */\n debug(r, o) {\n this.forwardLog(t.Debug, r, o);\n }\n /** Log a high-level progress update. */\n info(r, o) {\n this.forwardLog(t.Info, r, o);\n }\n /** Log something concerning. */\n warn(r, o) {\n this.forwardLog(t.Warn, r, o);\n }\n /** Log a critical failure. */\n error(r, o) {\n this.forwardLog(t.Error, r, o);\n }\n forwardLog(r, o, c = {}) {\n this.processor.processLog({\n message: o,\n level: r,\n origin: this.origin,\n context: c\n });\n }\n}\nconst a = e.create;\nclass i {\n constructor(r) {\n this.processors = r;\n }\n processLog(r) {\n this.processors.forEach((o) => {\n o.processLog(r);\n });\n }\n}\nfunction h(s) {\n return new i(s);\n}\nclass n {\n constructor(r, o) {\n this.predicate = r, this.processor = o;\n }\n processLog(r) {\n this.predicate(r) && this.processor.processLog(r);\n }\n}\nfunction g(s, r) {\n return new n(s, r);\n}\nexport {\n t as LogLevel,\n h as combine,\n a as createLogger,\n g as filter\n};\n//# sourceMappingURL=holz-core.js.map\n","import type { Log, LogProcessor } from '@holz/core';\nimport { LogLevel } from '@holz/core';\n\n/**\n * A backend that pretty-prints logs to a browser console, or any\n * remote-attached console.\n */\nexport default class ConsoleBackend implements LogProcessor {\n private console: MinimalConsole;\n\n constructor(options: Options = {}) {\n this.console = options.console ?? console;\n }\n\n processLog(log: Log) {\n const segments = [\n {\n include: true,\n command: '%c%s',\n content: LOG_LEVEL_TITLE[log.level],\n style: `font-weight: bold; color: ${LOG_LEVEL_STYLE[log.level]}`,\n },\n {\n include: true,\n command: '%c%s',\n content: log.message,\n style: `font-weight: normal; color: ${\n log.level === LogLevel.Debug ? 'darkgray' : 'unset'\n }`,\n },\n {\n include: Object.keys(log.context).length > 0,\n command: '%c%o', // Chrome hides object content with `%O`.\n content: log.context,\n style: '',\n },\n {\n include: log.origin.length > 0,\n command: '%c%s',\n content: log.origin.join(':'),\n style: 'color: gray',\n },\n ].filter((segment) => segment.include);\n\n const command = segments.map((segment) => segment.command).join(' ');\n const values = segments.flatMap((segment) => [\n segment.style,\n segment.content,\n ]);\n\n // Errors should always use stderr.\n const channel = LOG_METHOD[log.level];\n\n this.console[channel](command, ...values);\n }\n}\n\n// Before changing these, verify contrast on dark/light themes.\nconst LOG_LEVEL_STYLE: Record<LogLevel, string> = {\n [LogLevel.Debug]: 'darkgray',\n [LogLevel.Info]: 'darkturquoise',\n [LogLevel.Warn]: 'unset',\n [LogLevel.Error]: 'unset',\n};\n\nconst LOG_LEVEL_TITLE: Record<LogLevel, string> = {\n [LogLevel.Debug]: 'DEBUG',\n [LogLevel.Info]: 'INFO ',\n [LogLevel.Warn]: 'WARN ',\n [LogLevel.Error]: 'ERROR',\n};\n\nconst LOG_METHOD: Record<LogLevel, keyof MinimalConsole> = {\n [LogLevel.Debug]: 'log',\n [LogLevel.Info]: 'log',\n [LogLevel.Warn]: 'warn',\n [LogLevel.Error]: 'error',\n};\n\ninterface Options {\n console?: MinimalConsole;\n}\n\n/**\n * A subset of the Console interface. Must support printf-style interpolation.\n * @see https://console.spec.whatwg.org/#formatting-specifiers\n */\nexport type MinimalConsole = Pick<Console, 'log' | 'warn' | 'error'>;\n"],"names":["t","s","ConsoleBackend","options","log","segments","LOG_LEVEL_TITLE","LOG_LEVEL_STYLE","LogLevel","segment","command","values","channel","LOG_METHOD"],"mappings":"+OAAA,IAAIA,GAAsBC,IAAOA,EAAE,MAAQ,QAASA,EAAE,KAAO,OAAQA,EAAE,KAAO,OAAQA,EAAE,MAAQ,QAASA,IAAID,GAAK,EAAE,ECOpH,MAAqBE,CAAuC,CAG1D,YAAYC,EAAmB,GAAI,CAC5B,KAAA,QAAUA,EAAQ,SAAW,OACpC,CAEA,WAAWC,EAAU,CACnB,MAAMC,EAAW,CACf,CACE,QAAS,GACT,QAAS,OACT,QAASC,EAAgBF,EAAI,KAAK,EAClC,MAAO,6BAA6BG,EAAgBH,EAAI,KAAK,GAC/D,EACA,CACE,QAAS,GACT,QAAS,OACT,QAASA,EAAI,QACb,MAAO,+BACLA,EAAI,QAAUI,EAAS,MAAQ,WAAa,SAEhD,EACA,CACE,QAAS,OAAO,KAAKJ,EAAI,OAAO,EAAE,OAAS,EAC3C,QAAS,OACT,QAASA,EAAI,QACb,MAAO,EACT,EACA,CACE,QAASA,EAAI,OAAO,OAAS,EAC7B,QAAS,OACT,QAASA,EAAI,OAAO,KAAK,GAAG,EAC5B,MAAO,aACT,CACA,EAAA,OAAQK,GAAYA,EAAQ,OAAO,EAE/BC,EAAUL,EAAS,IAAKI,GAAYA,EAAQ,OAAO,EAAE,KAAK,GAAG,EAC7DE,EAASN,EAAS,QAASI,GAAY,CAC3CA,EAAQ,MACRA,EAAQ,OAAA,CACT,EAGKG,EAAUC,EAAWT,EAAI,KAAK,EAEpC,KAAK,QAAQQ,CAAO,EAAEF,EAAS,GAAGC,CAAM,CAC1C,CACF,CAGA,MAAMJ,EAA4C,CAChD,CAACC,EAAS,KAAK,EAAG,WAClB,CAACA,EAAS,IAAI,EAAG,gBACjB,CAACA,EAAS,IAAI,EAAG,QACjB,CAACA,EAAS,KAAK,EAAG,OACpB,EAEMF,EAA4C,CAChD,CAACE,EAAS,KAAK,EAAG,QAClB,CAACA,EAAS,IAAI,EAAG,QACjB,CAACA,EAAS,IAAI,EAAG,QACjB,CAACA,EAAS,KAAK,EAAG,OACpB,EAEMK,EAAqD,CACzD,CAACL,EAAS,KAAK,EAAG,MAClB,CAACA,EAAS,IAAI,EAAG,MACjB,CAACA,EAAS,IAAI,EAAG,OACjB,CAACA,EAAS,KAAK,EAAG,OACpB"}
|