@real-router/logger-plugin 0.4.2 → 0.4.4
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/cjs/index.d.ts.map +1 -0
- package/dist/esm/index.d.mts.map +1 -0
- package/package.json +5 -5
- package/src/constants.ts +15 -0
- package/src/factory.ts +21 -0
- package/src/index.ts +5 -0
- package/src/internal/console-groups.ts +74 -0
- package/src/internal/formatting.ts +56 -0
- package/src/internal/params-diff.ts +91 -0
- package/src/internal/performance-marks.ts +67 -0
- package/src/internal/timing.ts +52 -0
- package/src/plugin.ts +235 -0
- package/src/types.ts +67 -0
- package/src/validation.ts +61 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/types.ts","../../src/factory.ts"],"mappings":";;;;;;AAMA;KAAY,QAAA;;;;UAKK,kBAAA;EAAkB;;;;;;;;;;;;;;ACFnC;;;EDoBE,mBAAA;ECnBU;;;;;;;;;;ED8BV,KAAA,GAAQ,QAAA;;;;;;;EAQR,UAAA;;;;;;;;EASA,cAAA;;;;;;;EAQA,OAAA;AAAA;;;iBCxDc,mBAAA,CACd,OAAA,GAAU,OAAA,CAAQ,kBAAA,IACjB,aAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/types.ts","../../src/factory.ts"],"mappings":";;;;;;AAMA;KAAY,QAAA;;;;UAKK,kBAAA;EAAkB;;;;;;;;;;;;;;ACFnC;;;EDoBE,mBAAA;ECnBU;;;;;;;;;;ED8BV,KAAA,GAAQ,QAAA;;;;;;;EAQR,UAAA;;;;;;;;EASA,cAAA;;;;;;;EAQA,OAAA;AAAA;;;iBCxDc,mBAAA,CACd,OAAA,GAAU,OAAA,CAAQ,kBAAA,IACjB,aAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@real-router/logger-plugin",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "Development logging plugin with transition tracking and performance metrics",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
"types": "./dist/esm/index.d.mts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"development": "./src/index.ts",
|
|
12
11
|
"types": {
|
|
13
12
|
"import": "./dist/esm/index.d.mts",
|
|
14
13
|
"require": "./dist/cjs/index.d.ts"
|
|
@@ -18,7 +17,8 @@
|
|
|
18
17
|
}
|
|
19
18
|
},
|
|
20
19
|
"files": [
|
|
21
|
-
"dist"
|
|
20
|
+
"dist",
|
|
21
|
+
"src"
|
|
22
22
|
],
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"homepage": "https://github.com/greydragon888/real-router",
|
|
45
45
|
"sideEffects": false,
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@real-router/core": "^0.
|
|
47
|
+
"@real-router/core": "^0.46.0"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "tsdown --config-loader unrun",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"lint": "eslint --cache --ext .ts src/ tests/ --fix --max-warnings 0",
|
|
55
55
|
"test:mutation": "stryker run",
|
|
56
56
|
"test:mutation:report": "open reports/mutation-report.html",
|
|
57
|
-
"lint:package": "
|
|
57
|
+
"lint:package": "publint",
|
|
58
58
|
"lint:types": "attw --pack .",
|
|
59
59
|
"build:dist-only": "tsdown --config-loader unrun"
|
|
60
60
|
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// packages/logger-plugin/src/constants.ts
|
|
2
|
+
|
|
3
|
+
import type { LoggerPluginConfig } from "./types";
|
|
4
|
+
|
|
5
|
+
export const LOGGER_CONTEXT = "logger-plugin";
|
|
6
|
+
|
|
7
|
+
export const ERROR_PREFIX = `[@real-router/${LOGGER_CONTEXT}]`;
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_CONFIG: Required<LoggerPluginConfig> = {
|
|
10
|
+
level: "all",
|
|
11
|
+
usePerformanceMarks: false,
|
|
12
|
+
showParamsDiff: true,
|
|
13
|
+
showTiming: true,
|
|
14
|
+
context: LOGGER_CONTEXT,
|
|
15
|
+
};
|
package/src/factory.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// packages/logger-plugin/src/factory.ts
|
|
2
|
+
|
|
3
|
+
import { DEFAULT_CONFIG } from "./constants";
|
|
4
|
+
import { LoggerPlugin } from "./plugin";
|
|
5
|
+
import { validateOptions } from "./validation";
|
|
6
|
+
|
|
7
|
+
import type { LoggerPluginConfig } from "./types";
|
|
8
|
+
import type { PluginFactory } from "@real-router/core";
|
|
9
|
+
|
|
10
|
+
export function loggerPluginFactory(
|
|
11
|
+
options?: Partial<LoggerPluginConfig>,
|
|
12
|
+
): PluginFactory {
|
|
13
|
+
validateOptions(options);
|
|
14
|
+
|
|
15
|
+
const config: Required<LoggerPluginConfig> = {
|
|
16
|
+
...DEFAULT_CONFIG,
|
|
17
|
+
...options,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
return () => new LoggerPlugin(config).getPlugin();
|
|
21
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// packages/logger-plugin/src/internal/console-groups.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks if console.group is supported in the current environment.
|
|
5
|
+
*/
|
|
6
|
+
export const supportsConsoleGroups = (): boolean => {
|
|
7
|
+
return (
|
|
8
|
+
typeof console !== "undefined" &&
|
|
9
|
+
typeof console.group === "function" &&
|
|
10
|
+
typeof console.groupEnd === "function"
|
|
11
|
+
);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Manager for handling console groups
|
|
16
|
+
*/
|
|
17
|
+
export interface GroupManager {
|
|
18
|
+
/**
|
|
19
|
+
* Opens a group if it's not already open
|
|
20
|
+
*/
|
|
21
|
+
open: (label: string) => void;
|
|
22
|
+
/**
|
|
23
|
+
* Closes a group if it's open
|
|
24
|
+
*/
|
|
25
|
+
close: () => void;
|
|
26
|
+
/**
|
|
27
|
+
* Checks if a group is currently open
|
|
28
|
+
*/
|
|
29
|
+
isOpen: () => boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Creates a manager for handling console groups.
|
|
34
|
+
* Prevents duplicate group opening.
|
|
35
|
+
*
|
|
36
|
+
* @param enabled - Whether groups are supported in the environment
|
|
37
|
+
* @returns Object with open and close methods
|
|
38
|
+
*/
|
|
39
|
+
export const createGroupManager = (enabled: boolean): GroupManager => {
|
|
40
|
+
let isOpened = false;
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
/**
|
|
44
|
+
* Opens a group if it's not already open.
|
|
45
|
+
*/
|
|
46
|
+
open(label: string): void {
|
|
47
|
+
if (!enabled || isOpened) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
console.group(label);
|
|
52
|
+
isOpened = true;
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Closes a group if it's open.
|
|
57
|
+
*/
|
|
58
|
+
close(): void {
|
|
59
|
+
if (!enabled || !isOpened) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
console.groupEnd();
|
|
64
|
+
isOpened = false;
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Checks if a group is currently open.
|
|
69
|
+
*/
|
|
70
|
+
isOpen(): boolean {
|
|
71
|
+
return isOpened;
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// packages/logger-plugin/src/internal/formatting.ts
|
|
2
|
+
|
|
3
|
+
import type { State } from "@real-router/core";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Formats route name for logging output.
|
|
7
|
+
* Handles undefined/null.
|
|
8
|
+
*/
|
|
9
|
+
export const formatRouteName = (state?: State): string => {
|
|
10
|
+
return state?.name ?? "(none)";
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Formats execution time information.
|
|
15
|
+
* Uses adaptive units:
|
|
16
|
+
* - Microseconds (μs) for <0.1ms
|
|
17
|
+
* - Milliseconds (ms) for ≥0.1ms
|
|
18
|
+
*
|
|
19
|
+
* @param startTime - Start time or null
|
|
20
|
+
* @param now - Function to get current time
|
|
21
|
+
* @returns String with time or empty string
|
|
22
|
+
*/
|
|
23
|
+
export const formatTiming = (
|
|
24
|
+
startTime: number | null,
|
|
25
|
+
now: () => number,
|
|
26
|
+
): string => {
|
|
27
|
+
if (startTime === null) {
|
|
28
|
+
return "";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const durationMs = now() - startTime;
|
|
32
|
+
|
|
33
|
+
if (!Number.isFinite(durationMs) || durationMs < 0) {
|
|
34
|
+
return " (?)";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (durationMs < 0.1) {
|
|
38
|
+
const durationMks = (durationMs * 1000).toFixed(2);
|
|
39
|
+
|
|
40
|
+
return ` (${durationMks}μs)`;
|
|
41
|
+
} else {
|
|
42
|
+
const duration = durationMs.toFixed(2);
|
|
43
|
+
|
|
44
|
+
return ` (${duration}ms)`;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Creates a label for Performance API from route names.
|
|
50
|
+
*/
|
|
51
|
+
export const createTransitionLabel = (
|
|
52
|
+
fromRoute: string,
|
|
53
|
+
toRoute: string,
|
|
54
|
+
): string => {
|
|
55
|
+
return `${fromRoute}→${toRoute}`;
|
|
56
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// packages/logger-plugin/src/internal/params-diff.ts
|
|
2
|
+
|
|
3
|
+
import type { Params } from "@real-router/core";
|
|
4
|
+
|
|
5
|
+
export interface ParamsDiff {
|
|
6
|
+
changed: Record<string, { from: unknown; to: unknown }>;
|
|
7
|
+
added: Record<string, unknown>;
|
|
8
|
+
removed: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Calculates differences between two parameter objects.
|
|
13
|
+
* Performs only shallow comparison.
|
|
14
|
+
*
|
|
15
|
+
* @param fromParams - Previous parameters
|
|
16
|
+
* @param toParams - New parameters
|
|
17
|
+
* @returns Object with differences or null if there are no changes
|
|
18
|
+
*/
|
|
19
|
+
export const getParamsDiff = (
|
|
20
|
+
fromParams: Params,
|
|
21
|
+
toParams: Params,
|
|
22
|
+
): ParamsDiff | null => {
|
|
23
|
+
const changed: ParamsDiff["changed"] = {};
|
|
24
|
+
const added: ParamsDiff["added"] = {};
|
|
25
|
+
const removed: ParamsDiff["removed"] = {};
|
|
26
|
+
|
|
27
|
+
// Track if any changes found to avoid iterating through objects at the end.
|
|
28
|
+
// This is a performance optimization: instead of calling Object.keys().length
|
|
29
|
+
// three times to check if objects are empty, we set this flag when we find
|
|
30
|
+
// any change and check it once at the end.
|
|
31
|
+
let hasChanges = false;
|
|
32
|
+
|
|
33
|
+
// Find changed and removed
|
|
34
|
+
for (const key in fromParams) {
|
|
35
|
+
if (!(key in toParams)) {
|
|
36
|
+
removed[key] = fromParams[key];
|
|
37
|
+
hasChanges = true;
|
|
38
|
+
} else if (fromParams[key] !== toParams[key]) {
|
|
39
|
+
changed[key] = { from: fromParams[key], to: toParams[key] };
|
|
40
|
+
hasChanges = true;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Find added
|
|
45
|
+
for (const key in toParams) {
|
|
46
|
+
if (!(key in fromParams)) {
|
|
47
|
+
added[key] = toParams[key];
|
|
48
|
+
hasChanges = true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Return null if there are no changes
|
|
53
|
+
if (!hasChanges) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return { changed, added, removed };
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Formats and logs parameter differences.
|
|
62
|
+
*
|
|
63
|
+
* @param diff - Object with differences
|
|
64
|
+
* @param context - Context for console
|
|
65
|
+
*/
|
|
66
|
+
export const logParamsDiff = (diff: ParamsDiff, context: string): void => {
|
|
67
|
+
const parts: string[] = [];
|
|
68
|
+
|
|
69
|
+
// Cache entries to avoid double iteration
|
|
70
|
+
const changedEntries = Object.entries(diff.changed);
|
|
71
|
+
|
|
72
|
+
if (changedEntries.length > 0) {
|
|
73
|
+
const items: string[] = [];
|
|
74
|
+
|
|
75
|
+
for (const [key, { from, to }] of changedEntries) {
|
|
76
|
+
items.push(`${key}: ${JSON.stringify(from)} → ${JSON.stringify(to)}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
parts.push(`Changed: { ${items.join(", ")} }`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (Object.keys(diff.added).length > 0) {
|
|
83
|
+
parts.push(`Added: ${JSON.stringify(diff.added)}`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (Object.keys(diff.removed).length > 0) {
|
|
87
|
+
parts.push(`Removed: ${JSON.stringify(diff.removed)}`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
console.log(`[${context}] ${parts.join(", ")}`);
|
|
91
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// packages/logger-plugin/src/internal/performance-marks.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks if Performance API is supported in the current environment.
|
|
5
|
+
*/
|
|
6
|
+
export const supportsPerformanceAPI = (): boolean => {
|
|
7
|
+
return (
|
|
8
|
+
typeof performance !== "undefined" &&
|
|
9
|
+
typeof performance.mark === "function" &&
|
|
10
|
+
typeof performance.measure === "function"
|
|
11
|
+
);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Performance tracker interface with mark and measure methods.
|
|
16
|
+
*/
|
|
17
|
+
export interface PerformanceTracker {
|
|
18
|
+
mark: (name: string) => void;
|
|
19
|
+
measure: (measureName: string, startMark: string, endMark: string) => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Creates a tracker for working with the Performance API.
|
|
24
|
+
* Ignores calls if the API is unavailable.
|
|
25
|
+
*
|
|
26
|
+
* @param enabled - Whether the functionality is enabled (from config)
|
|
27
|
+
* @param context - Context for error logging
|
|
28
|
+
* @returns Object with mark and measure methods
|
|
29
|
+
*/
|
|
30
|
+
export const createPerformanceTracker = (
|
|
31
|
+
enabled: boolean,
|
|
32
|
+
context: string,
|
|
33
|
+
): PerformanceTracker => {
|
|
34
|
+
const isSupported = enabled && supportsPerformanceAPI();
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
/**
|
|
38
|
+
* Creates a performance mark with the specified name.
|
|
39
|
+
*/
|
|
40
|
+
mark(name: string): void {
|
|
41
|
+
if (!isSupported) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
performance.mark(name);
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Creates a performance measure between two marks.
|
|
50
|
+
* Logs a warning if the marks don't exist.
|
|
51
|
+
*/
|
|
52
|
+
measure(measureName: string, startMark: string, endMark: string): void {
|
|
53
|
+
if (!isSupported) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
performance.measure(measureName, startMark, endMark);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.warn(
|
|
61
|
+
`[${context}] Failed to create performance measure: ${measureName}`,
|
|
62
|
+
error,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// packages/logger-plugin/src/internal/timing.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Function that returns high-resolution timestamp in milliseconds.
|
|
5
|
+
*/
|
|
6
|
+
type TimeProvider = () => number;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* State for Date.now() monotonic emulation
|
|
10
|
+
*/
|
|
11
|
+
let lastTimestamp = 0;
|
|
12
|
+
let timeOffset = 0;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Creates monotonic Date.now() wrapper that ensures time never goes backwards.
|
|
16
|
+
*
|
|
17
|
+
* @returns Time provider function with monotonic guarantee
|
|
18
|
+
*/
|
|
19
|
+
function createMonotonicDateNow(): TimeProvider {
|
|
20
|
+
// eslint-disable-next-line unicorn/consistent-function-scoping -- closure over module-level lastTimestamp/timeOffset
|
|
21
|
+
return (): number => {
|
|
22
|
+
const current: number = Date.now();
|
|
23
|
+
|
|
24
|
+
if (current < lastTimestamp) {
|
|
25
|
+
timeOffset += lastTimestamp - current;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
lastTimestamp = current;
|
|
29
|
+
|
|
30
|
+
return current + timeOffset;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Initialize time provider based on environment.
|
|
36
|
+
* Uses performance.now() in modern environments (Node.js 16+, all browsers),
|
|
37
|
+
* falls back to monotonic Date.now() wrapper for edge cases.
|
|
38
|
+
*/
|
|
39
|
+
const nowFn: TimeProvider =
|
|
40
|
+
typeof performance !== "undefined" && typeof performance.now === "function"
|
|
41
|
+
? (): number => performance.now()
|
|
42
|
+
: createMonotonicDateNow();
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Returns high-resolution monotonic timestamp.
|
|
46
|
+
*
|
|
47
|
+
* Uses performance.now() in modern environments (Node.js 16+, all browsers).
|
|
48
|
+
* Falls back to monotonic Date.now() wrapper (~1ms precision) for edge cases.
|
|
49
|
+
*
|
|
50
|
+
* @returns Timestamp in milliseconds
|
|
51
|
+
*/
|
|
52
|
+
export const now = (): number => nowFn();
|
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
// packages/logger-plugin/src/plugin.ts
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createGroupManager,
|
|
5
|
+
supportsConsoleGroups,
|
|
6
|
+
} from "./internal/console-groups";
|
|
7
|
+
import {
|
|
8
|
+
formatRouteName,
|
|
9
|
+
formatTiming,
|
|
10
|
+
createTransitionLabel,
|
|
11
|
+
} from "./internal/formatting";
|
|
12
|
+
import { getParamsDiff, logParamsDiff } from "./internal/params-diff";
|
|
13
|
+
import { createPerformanceTracker } from "./internal/performance-marks";
|
|
14
|
+
import { now } from "./internal/timing";
|
|
15
|
+
|
|
16
|
+
import type { GroupManager } from "./internal/console-groups";
|
|
17
|
+
import type { PerformanceTracker } from "./internal/performance-marks";
|
|
18
|
+
import type { LoggerPluginConfig } from "./types";
|
|
19
|
+
import type { Plugin, RouterError, State } from "@real-router/core";
|
|
20
|
+
|
|
21
|
+
export class LoggerPlugin {
|
|
22
|
+
readonly #logLifecycle: boolean;
|
|
23
|
+
readonly #logTransition: boolean;
|
|
24
|
+
readonly #logError: boolean;
|
|
25
|
+
|
|
26
|
+
readonly #shouldLogParams: boolean;
|
|
27
|
+
readonly #shouldShowTiming: boolean;
|
|
28
|
+
readonly #usePerf: boolean;
|
|
29
|
+
|
|
30
|
+
readonly #prefix: string;
|
|
31
|
+
readonly #context: string;
|
|
32
|
+
|
|
33
|
+
readonly #groups: GroupManager;
|
|
34
|
+
readonly #perf: PerformanceTracker;
|
|
35
|
+
|
|
36
|
+
// Transition state
|
|
37
|
+
#transitionStartTime: number | null = null;
|
|
38
|
+
#transitionLabel = "";
|
|
39
|
+
#startMarkName = "";
|
|
40
|
+
|
|
41
|
+
constructor(config: Required<LoggerPluginConfig>) {
|
|
42
|
+
this.#logLifecycle = config.level === "all";
|
|
43
|
+
this.#logTransition = config.level !== "none" && config.level !== "errors";
|
|
44
|
+
this.#logError = config.level !== "none";
|
|
45
|
+
|
|
46
|
+
this.#shouldLogParams = this.#logTransition && config.showParamsDiff;
|
|
47
|
+
this.#shouldShowTiming = config.showTiming;
|
|
48
|
+
this.#usePerf = config.usePerformanceMarks;
|
|
49
|
+
|
|
50
|
+
this.#prefix = `[${config.context}]`;
|
|
51
|
+
this.#context = config.context;
|
|
52
|
+
|
|
53
|
+
this.#groups = createGroupManager(supportsConsoleGroups());
|
|
54
|
+
this.#perf = createPerformanceTracker(
|
|
55
|
+
config.usePerformanceMarks,
|
|
56
|
+
config.context,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
getPlugin(): Plugin {
|
|
61
|
+
return {
|
|
62
|
+
onStart: () => {
|
|
63
|
+
this.#perf.mark("router:start");
|
|
64
|
+
|
|
65
|
+
if (this.#logLifecycle) {
|
|
66
|
+
console.log(`${this.#prefix} Router started`);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
onStop: () => {
|
|
71
|
+
this.#groups.close();
|
|
72
|
+
|
|
73
|
+
this.#perf.mark("router:stop");
|
|
74
|
+
this.#perf.measure("router:lifetime", "router:start", "router:stop");
|
|
75
|
+
|
|
76
|
+
if (this.#logLifecycle) {
|
|
77
|
+
console.log(`${this.#prefix} Router stopped`);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
onTransitionStart: (toState: State, fromState?: State) => {
|
|
82
|
+
this.#groups.open("Router transition");
|
|
83
|
+
this.#transitionStartTime = this.#shouldShowTiming ? now() : null;
|
|
84
|
+
|
|
85
|
+
const fromRoute = formatRouteName(fromState);
|
|
86
|
+
const toRoute = formatRouteName(toState);
|
|
87
|
+
|
|
88
|
+
if (this.#usePerf) {
|
|
89
|
+
this.#transitionLabel = createTransitionLabel(fromRoute, toRoute);
|
|
90
|
+
this.#startMarkName = `router:transition-start:${this.#transitionLabel}`;
|
|
91
|
+
this.#perf.mark(this.#startMarkName);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (this.#logTransition) {
|
|
95
|
+
console.log(`${this.#prefix} Transition: ${fromRoute} → ${toRoute}`, {
|
|
96
|
+
from: fromState,
|
|
97
|
+
to: toState,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
this.#logParamsIfNeeded(toState, fromState);
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
onTransitionLeaveApprove: (toState: State, fromState?: State) => {
|
|
105
|
+
if (this.#usePerf) {
|
|
106
|
+
this.#perf.mark(`router:leave-approved:${this.#transitionLabel}`);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (this.#logTransition) {
|
|
110
|
+
console.log(`${this.#prefix} Leave approved`, {
|
|
111
|
+
to: toState,
|
|
112
|
+
from: fromState,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
onTransitionSuccess: (toState: State, fromState?: State) => {
|
|
118
|
+
if (this.#usePerf) {
|
|
119
|
+
const label = this.#transitionLabel;
|
|
120
|
+
const endMark = `router:transition-end:${label}`;
|
|
121
|
+
|
|
122
|
+
this.#perf.mark(endMark);
|
|
123
|
+
this.#perf.measure(
|
|
124
|
+
`router:transition:${label}`,
|
|
125
|
+
this.#startMarkName,
|
|
126
|
+
endMark,
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (this.#logTransition) {
|
|
131
|
+
const timing = this.#shouldShowTiming
|
|
132
|
+
? formatTiming(this.#transitionStartTime, now)
|
|
133
|
+
: "";
|
|
134
|
+
|
|
135
|
+
console.log(`${this.#prefix} Transition success${timing}`, {
|
|
136
|
+
to: toState,
|
|
137
|
+
from: fromState,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
this.#resetTransitionState();
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
onTransitionCancel: (toState: State, fromState?: State) => {
|
|
145
|
+
if (this.#usePerf) {
|
|
146
|
+
const label = this.#transitionLabel;
|
|
147
|
+
const cancelMark = `router:transition-cancel:${label}`;
|
|
148
|
+
|
|
149
|
+
this.#perf.mark(cancelMark);
|
|
150
|
+
this.#perf.measure(
|
|
151
|
+
`router:transition-cancelled:${label}`,
|
|
152
|
+
this.#startMarkName,
|
|
153
|
+
cancelMark,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (this.#logTransition) {
|
|
158
|
+
const timing = this.#shouldShowTiming
|
|
159
|
+
? formatTiming(this.#transitionStartTime, now)
|
|
160
|
+
: "";
|
|
161
|
+
|
|
162
|
+
console.warn(`${this.#prefix} Transition cancelled${timing}`, {
|
|
163
|
+
to: toState,
|
|
164
|
+
from: fromState,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
this.#resetTransitionState();
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
onTransitionError: (
|
|
172
|
+
toState: State | undefined,
|
|
173
|
+
fromState: State | undefined,
|
|
174
|
+
err: RouterError,
|
|
175
|
+
) => {
|
|
176
|
+
if (this.#usePerf) {
|
|
177
|
+
const label = this.#transitionLabel;
|
|
178
|
+
const errorMark = `router:transition-error:${label}`;
|
|
179
|
+
|
|
180
|
+
this.#perf.mark(errorMark);
|
|
181
|
+
this.#perf.measure(
|
|
182
|
+
`router:transition-failed:${label}`,
|
|
183
|
+
this.#startMarkName,
|
|
184
|
+
errorMark,
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (this.#logError) {
|
|
189
|
+
const timing = this.#shouldShowTiming
|
|
190
|
+
? formatTiming(this.#transitionStartTime, now)
|
|
191
|
+
: "";
|
|
192
|
+
|
|
193
|
+
console.error(
|
|
194
|
+
`${this.#prefix} Transition error: ${err.code}${timing}`,
|
|
195
|
+
{
|
|
196
|
+
error: err,
|
|
197
|
+
stack: err.stack,
|
|
198
|
+
to: toState,
|
|
199
|
+
from: fromState,
|
|
200
|
+
},
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
this.#resetTransitionState();
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
teardown: () => {
|
|
208
|
+
this.#resetTransitionState();
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
#logParamsIfNeeded(toState: State, fromState?: State): void {
|
|
214
|
+
if (!this.#shouldLogParams || !fromState) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (toState.name !== fromState.name) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const diff = getParamsDiff(fromState.params, toState.params);
|
|
223
|
+
|
|
224
|
+
if (diff) {
|
|
225
|
+
logParamsDiff(diff, this.#context);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
#resetTransitionState(): void {
|
|
230
|
+
this.#groups.close();
|
|
231
|
+
this.#transitionLabel = "";
|
|
232
|
+
this.#startMarkName = "";
|
|
233
|
+
this.#transitionStartTime = null;
|
|
234
|
+
}
|
|
235
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// packages/logger-plugin/src/types.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Logging level for router events.
|
|
5
|
+
* Controls which events are logged to the console.
|
|
6
|
+
*/
|
|
7
|
+
export type LogLevel = "all" | "transitions" | "errors" | "none";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Configuration options for the logger-plugin.
|
|
11
|
+
*/
|
|
12
|
+
export interface LoggerPluginConfig {
|
|
13
|
+
/**
|
|
14
|
+
* Use Performance API to create marks and measures.
|
|
15
|
+
* Enables integration with browser DevTools Performance tab.
|
|
16
|
+
*
|
|
17
|
+
* Creates marks:
|
|
18
|
+
* - `router:transition-start:{from}→{to}`
|
|
19
|
+
* - `router:transition-end:{from}→{to}` (success)
|
|
20
|
+
* - `router:transition-cancel:{from}→{to}` (cancelled)
|
|
21
|
+
* - `router:transition-error:{from}→{to}` (error)
|
|
22
|
+
*
|
|
23
|
+
* Creates measures:
|
|
24
|
+
* - `router:transition:{from}→{to}` (success)
|
|
25
|
+
* - `router:transition-cancelled:{from}→{to}` (cancelled)
|
|
26
|
+
* - `router:transition-failed:{from}→{to}` (error)
|
|
27
|
+
*
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
usePerformanceMarks?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Logging level - controls what router events to log.
|
|
33
|
+
*
|
|
34
|
+
* - 'all': Log all router events (default)
|
|
35
|
+
* - 'transitions': Log only transition-related events
|
|
36
|
+
* - 'errors': Log only transition errors
|
|
37
|
+
* - 'none': Disable all logging
|
|
38
|
+
*
|
|
39
|
+
* @default 'all'
|
|
40
|
+
*/
|
|
41
|
+
level?: LogLevel;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Show execution time in milliseconds for transitions.
|
|
45
|
+
* Helps identify slow route changes.
|
|
46
|
+
*
|
|
47
|
+
* @default true
|
|
48
|
+
*/
|
|
49
|
+
showTiming?: boolean;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Show diff of changed route parameters between transitions.
|
|
53
|
+
* Only applies when navigating within the same route.
|
|
54
|
+
* Helps identify which parameters changed during navigation.
|
|
55
|
+
*
|
|
56
|
+
* @default true
|
|
57
|
+
*/
|
|
58
|
+
showParamsDiff?: boolean;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Custom context name for console.
|
|
62
|
+
* Useful when running multiple routers.
|
|
63
|
+
*
|
|
64
|
+
* @default 'logger-plugin'
|
|
65
|
+
*/
|
|
66
|
+
context?: string;
|
|
67
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// packages/logger-plugin/src/validation.ts
|
|
2
|
+
|
|
3
|
+
import { ERROR_PREFIX } from "./constants";
|
|
4
|
+
|
|
5
|
+
import type { LoggerPluginConfig } from "./types";
|
|
6
|
+
|
|
7
|
+
const VALID_LEVELS = new Set(["all", "transitions", "errors", "none"]);
|
|
8
|
+
|
|
9
|
+
export function validateOptions(options?: Partial<LoggerPluginConfig>): void {
|
|
10
|
+
if (options === undefined) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Runtime defense: typeof null === "object", so check identity first.
|
|
15
|
+
// TypeScript excludes null from the parameter type, but JS callers may pass it.
|
|
16
|
+
if (options === (null as never) || typeof options !== "object") {
|
|
17
|
+
throw new TypeError(`${ERROR_PREFIX} Options must be an object`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (options.level !== undefined && !VALID_LEVELS.has(options.level)) {
|
|
21
|
+
throw new TypeError(
|
|
22
|
+
`${ERROR_PREFIX} Invalid level: "${options.level}". Expected: ${[...VALID_LEVELS].join(", ")}`,
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (
|
|
27
|
+
options.context !== undefined &&
|
|
28
|
+
(typeof options.context !== "string" || options.context.length === 0)
|
|
29
|
+
) {
|
|
30
|
+
throw new TypeError(
|
|
31
|
+
`${ERROR_PREFIX} Option "context" must be a non-empty string`,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (
|
|
36
|
+
options.showTiming !== undefined &&
|
|
37
|
+
typeof options.showTiming !== "boolean"
|
|
38
|
+
) {
|
|
39
|
+
throw new TypeError(
|
|
40
|
+
`${ERROR_PREFIX} Option "showTiming" must be a boolean`,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (
|
|
45
|
+
options.showParamsDiff !== undefined &&
|
|
46
|
+
typeof options.showParamsDiff !== "boolean"
|
|
47
|
+
) {
|
|
48
|
+
throw new TypeError(
|
|
49
|
+
`${ERROR_PREFIX} Option "showParamsDiff" must be a boolean`,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (
|
|
54
|
+
options.usePerformanceMarks !== undefined &&
|
|
55
|
+
typeof options.usePerformanceMarks !== "boolean"
|
|
56
|
+
) {
|
|
57
|
+
throw new TypeError(
|
|
58
|
+
`${ERROR_PREFIX} Option "usePerformanceMarks" must be a boolean`,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|