@sincpro/client-framework 1.8.0 → 1.9.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/README.md
CHANGED
|
@@ -587,9 +587,26 @@ The console is the default engine. In production it is silent; in development it
|
|
|
587
587
|
everything, and `level` and `layers` hide only INFO/DEBUG. A supplied `engine` owns its own
|
|
588
588
|
policy, and each sink of `combineLoggers` keeps its own — the console's silence never silences
|
|
589
589
|
another. Every execution writes `execution.started` (DEBUG) and `execution.finished` with its
|
|
590
|
-
outcome and duration (INFO,
|
|
591
|
-
result, an error body or the session's context unless the author writes it. Delivery,
|
|
592
|
-
and credentials (Loki, a phone's queue) are the platform's.
|
|
590
|
+
outcome and duration (INFO, also when refused or aborted; ERROR when failed). No line carries a
|
|
591
|
+
DTO, a result, an error body or the session's context unless the author writes it. Delivery,
|
|
592
|
+
buffering and credentials (Loki, a phone's queue) are the platform's.
|
|
593
|
+
|
|
594
|
+
In development each record is one line for a person — time, level, layer, use case, message, its
|
|
595
|
+
attributes as `key=value`, and the run's short id — never the record object after it:
|
|
596
|
+
|
|
597
|
+
```text
|
|
598
|
+
10:20:30.045 DEBUG [feature] ▶ shop.Pay #0193a1
|
|
599
|
+
10:20:30.051 INFO [feature] shop.Pay Paying order=A-1 note="two words" #0193a1
|
|
600
|
+
10:20:30.057 INFO [feature] ✔ shop.Pay 12ms #0193a1
|
|
601
|
+
10:20:31.002 INFO [feature] ✖ shop.Pay refused: domain 3ms #0193b7
|
|
602
|
+
10:20:32.118 ERROR [feature] ✖ shop.Pay TypeError: card is undefined 4ms #0193c2 + the Error, with its stack
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
An unexpected failure also prints its `Error`, so LogBox, Expo's terminal and a browser show its
|
|
606
|
+
code frame. `consoleLogger({ mode, colors })` colors the lines of the levels named (`true`: every
|
|
607
|
+
one) with ANSI codes: the platform knows where its console renders them — a terminal does, React
|
|
608
|
+
Native's LogBox (WARN and ERROR in the app) does not. `prettyLine(record)` is the same line, for a
|
|
609
|
+
sink of one's own.
|
|
593
610
|
|
|
594
611
|
## Context
|
|
595
612
|
|
|
@@ -1,6 +1,27 @@
|
|
|
1
|
-
import { type LogFilter, type Logger } from "../domain/logging.js";
|
|
2
|
-
/**
|
|
1
|
+
import { type LogFilter, type Logger, type LogLevel, type LogRecord } from "../domain/logging.js";
|
|
2
|
+
/**
|
|
3
|
+
* Context: a development console is read by a person, a line at a glance. Each record is ONE
|
|
4
|
+
* line — its time, its level, its layer and use case, its message and the keys worth reading —,
|
|
5
|
+
* never the record object dumped after it, and a run's start and end read as `▶` / `✔` / `✖`.
|
|
6
|
+
* An unexpected failure still goes as an `Error` with its stack, because that is what React
|
|
7
|
+
* Native's LogBox, Expo's terminal and a browser print with a code frame. Production is silent.
|
|
8
|
+
*
|
|
9
|
+
* Colors are ANSI escape codes, and only the platform knows where its console renders them: a
|
|
10
|
+
* terminal and a browser's devtools do; React Native's LogBox, which shows WARN and ERROR in the
|
|
11
|
+
* app, prints them as text. So the platform names the levels to color (`colors`), and the line
|
|
12
|
+
* reads the same without them.
|
|
13
|
+
*/
|
|
14
|
+
/** The console's mode, which INFO and DEBUG lines it prints (`LogFilter`), and their colors. */
|
|
3
15
|
export type ConsoleLoggerOptions = LogFilter & {
|
|
4
16
|
readonly mode: "development" | "production";
|
|
17
|
+
/** The levels whose lines get ANSI colors: `true` every one, none unless said. */
|
|
18
|
+
readonly colors?: boolean | readonly LogLevel[];
|
|
5
19
|
};
|
|
20
|
+
/** One line for a person: time, level, layer, what happened, its keys, the run. */
|
|
21
|
+
export declare function prettyLine(record: LogRecord, colored?: boolean): string;
|
|
22
|
+
/**
|
|
23
|
+
* Built-in console engine: one readable line per record in development, silent in production;
|
|
24
|
+
* which INFO and DEBUG lines it prints is its own filter, WARN and ERROR always. Other sinks are
|
|
25
|
+
* filtered independently.
|
|
26
|
+
*/
|
|
6
27
|
export declare function consoleLogger(options: ConsoleLoggerOptions): Logger;
|
|
@@ -1,8 +1,83 @@
|
|
|
1
1
|
import { isLogLevel, speaks, } from "../domain/logging.js";
|
|
2
|
+
const LABELS = {
|
|
3
|
+
debug: "DEBUG",
|
|
4
|
+
info: "INFO ",
|
|
5
|
+
warn: "WARN ",
|
|
6
|
+
error: "ERROR",
|
|
7
|
+
};
|
|
8
|
+
const LEVEL_COLORS = {
|
|
9
|
+
debug: "90",
|
|
10
|
+
info: "36",
|
|
11
|
+
warn: "33",
|
|
12
|
+
error: "31",
|
|
13
|
+
};
|
|
14
|
+
/** Past this, a value is cut: a line is read, not scrolled. */
|
|
15
|
+
const MAX_VALUE = 160;
|
|
16
|
+
/** How much of a run's id a line shows: enough to tell two runs apart. */
|
|
17
|
+
const SHORT_ID = 6;
|
|
18
|
+
const painted = (code, text) => `\u001b[${code}m${text}\u001b[0m`;
|
|
19
|
+
const plain = (_code, text) => text;
|
|
20
|
+
function pad(value, width = 2) {
|
|
21
|
+
return String(value).padStart(width, "0");
|
|
22
|
+
}
|
|
23
|
+
/** `HH:mm:ss.SSS` on the device's clock; the record's own text when it is not an instant. */
|
|
24
|
+
function timeOf(timestamp) {
|
|
25
|
+
const at = new Date(timestamp);
|
|
26
|
+
if (Number.isNaN(at.getTime()))
|
|
27
|
+
return timestamp;
|
|
28
|
+
return `${pad(at.getHours())}:${pad(at.getMinutes())}:${pad(at.getSeconds())}.${pad(at.getMilliseconds(), 3)}`;
|
|
29
|
+
}
|
|
30
|
+
/** `key=value`, the value quoted when it would not read as one word, and cut when long. */
|
|
31
|
+
function keyValue(key, value) {
|
|
32
|
+
const text = String(value);
|
|
33
|
+
const cut = text.length > MAX_VALUE ? `${text.slice(0, MAX_VALUE)}…` : text;
|
|
34
|
+
return `${key}=${/^[^\s"=]+$/.test(cut) ? cut : JSON.stringify(cut)}`;
|
|
35
|
+
}
|
|
2
36
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
37
|
+
* What the line says after its level and layer:
|
|
38
|
+
*
|
|
39
|
+
* ▶ common.PrintImage a run starts
|
|
40
|
+
* ✔ common.PrintImage 12ms it answered
|
|
41
|
+
* ✖ common.PrintImage TypeError: printer is undefined 8ms it failed
|
|
42
|
+
* ✖ common.ConnectPrinter refused: unavailable 8ms it was refused, or the caller left
|
|
43
|
+
* common.PrintImage Printing page=2 a line its use case wrote
|
|
5
44
|
*/
|
|
45
|
+
function saying(record, paint) {
|
|
46
|
+
const where = record.useCase ? paint("1", record.useCase) : "";
|
|
47
|
+
const took = record.durationMs === undefined ? "" : ` ${Math.round(record.durationMs)}ms`;
|
|
48
|
+
if (record.event === "execution.started")
|
|
49
|
+
return `${paint("34", "▶")} ${where}`;
|
|
50
|
+
if (record.event === "execution.finished") {
|
|
51
|
+
if (record.outcome === "ok")
|
|
52
|
+
return `${paint("32", "✔")} ${where}${took}`;
|
|
53
|
+
const failure = record.failure;
|
|
54
|
+
const why = record.outcome === "failed"
|
|
55
|
+
? failure?.message
|
|
56
|
+
? `${failure.type}: ${failure.message}`
|
|
57
|
+
: (failure?.type ?? "failed")
|
|
58
|
+
: `${record.outcome}${failure?.kind ? `: ${failure.kind}` : ""}`;
|
|
59
|
+
return `${paint("31", "✖")} ${where} ${why}${took}`;
|
|
60
|
+
}
|
|
61
|
+
return where ? `${where} ${record.message}` : record.message;
|
|
62
|
+
}
|
|
63
|
+
/** One line for a person: time, level, layer, what happened, its keys, the run. */
|
|
64
|
+
export function prettyLine(record, colored = false) {
|
|
65
|
+
const paint = colored ? painted : plain;
|
|
66
|
+
const keys = Object.entries(record.attributes ?? {}).map(([key, value]) => keyValue(key, value));
|
|
67
|
+
const run = record.executionId
|
|
68
|
+
? ` ${paint("2", `#${record.executionId.slice(0, SHORT_ID)}`)}`
|
|
69
|
+
: "";
|
|
70
|
+
return [
|
|
71
|
+
paint("2", timeOf(record.timestamp)),
|
|
72
|
+
paint(LEVEL_COLORS[record.level], LABELS[record.level]),
|
|
73
|
+
paint("2", `[${record.layer}]`),
|
|
74
|
+
saying(record, paint),
|
|
75
|
+
...keys,
|
|
76
|
+
]
|
|
77
|
+
.filter((part) => part !== "")
|
|
78
|
+
.join(" ")
|
|
79
|
+
.concat(run);
|
|
80
|
+
}
|
|
6
81
|
/** The failure as an Error to print: its type as its name, its message, its stack. */
|
|
7
82
|
function errorOf(stack, failure) {
|
|
8
83
|
const shown = new Error(failure.message ?? failure.type ?? "");
|
|
@@ -10,6 +85,20 @@ function errorOf(stack, failure) {
|
|
|
10
85
|
shown.stack = stack;
|
|
11
86
|
return shown;
|
|
12
87
|
}
|
|
88
|
+
function methodOf(level) {
|
|
89
|
+
if (level === "debug")
|
|
90
|
+
return console.debug;
|
|
91
|
+
if (level === "warn")
|
|
92
|
+
return console.warn;
|
|
93
|
+
if (level === "error")
|
|
94
|
+
return console.error;
|
|
95
|
+
return console.log;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Built-in console engine: one readable line per record in development, silent in production;
|
|
99
|
+
* which INFO and DEBUG lines it prints is its own filter, WARN and ERROR always. Other sinks are
|
|
100
|
+
* filtered independently.
|
|
101
|
+
*/
|
|
13
102
|
export function consoleLogger(options) {
|
|
14
103
|
const mode = options.mode;
|
|
15
104
|
if (mode !== "development" && mode !== "production") {
|
|
@@ -18,6 +107,11 @@ export function consoleLogger(options) {
|
|
|
18
107
|
if (options.level !== undefined && !isLogLevel(options.level)) {
|
|
19
108
|
throw new TypeError("Console logger level must be debug, info, warn or error");
|
|
20
109
|
}
|
|
110
|
+
const colors = options.colors ?? false;
|
|
111
|
+
if (Array.isArray(colors) && !colors.every(isLogLevel)) {
|
|
112
|
+
throw new TypeError("Console logger colors must be true, false or log levels");
|
|
113
|
+
}
|
|
114
|
+
const colored = (level) => colors === true || (Array.isArray(colors) && colors.includes(level));
|
|
21
115
|
const filter = {
|
|
22
116
|
level: options.level ?? "debug",
|
|
23
117
|
layers: { ...options.layers },
|
|
@@ -27,24 +121,21 @@ export function consoleLogger(options) {
|
|
|
27
121
|
log(record) {
|
|
28
122
|
if (mode === "production" || !speaks(record, filter))
|
|
29
123
|
return;
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
: console.log;
|
|
38
|
-
const stack = record.failure?.stack;
|
|
39
|
-
if (!stack) {
|
|
40
|
-
method.call(console, prefix, record.message, record);
|
|
41
|
-
return;
|
|
124
|
+
const method = methodOf(record.level);
|
|
125
|
+
let line;
|
|
126
|
+
try {
|
|
127
|
+
line = prettyLine(record, colored(record.level));
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
line = `${LABELS[record.level]} [${record.layer}] ${record.message}`;
|
|
42
131
|
}
|
|
132
|
+
const stack = record.failure?.stack;
|
|
43
133
|
// An unexpected failure goes as an Error — its name, its message, its stack —: what React
|
|
44
134
|
// Native's LogBox, Expo's terminal and a browser print as a failure, with its code frame.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
135
|
+
if (stack)
|
|
136
|
+
method.call(console, line, errorOf(stack, record.failure ?? {}));
|
|
137
|
+
else
|
|
138
|
+
method.call(console, line);
|
|
48
139
|
},
|
|
49
140
|
};
|
|
50
141
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sincpro/client-framework",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "The client side of sincpro_framework, for every platform: Feature and ApplicationService traced and reported by default, applications made of domains, and the typed client generated from a service's contract. No React, no DOM.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|