@sincpro/client-framework 1.8.0 → 1.10.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.
@@ -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
- * Built-in console engine: silent in production, configurable
4
- * development layers, and WARN/ERROR preserved. Other sinks are filtered independently.
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 prefix = `${record.timestamp} [${record.layer}] ${record.level.toUpperCase()}`;
31
- const method = record.level === "debug"
32
- ? console.debug
33
- : record.level === "warn"
34
- ? console.warn
35
- : record.level === "error"
36
- ? console.error
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
- // A bare stack string read as one printed `ERROR null null`.
46
- const { stack: _shownBelow, ...failure } = record.failure ?? {};
47
- method.call(console, `${prefix} ${record.message}`, { ...record, failure }, errorOf(stack, failure));
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.8.0",
3
+ "version": "1.10.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",