@aipanel/core 1.2.0-beta.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/es/constants.d.ts +145 -0
- package/es/constants.mjs +124 -0
- package/es/file-log-watcher.d.ts +20 -0
- package/es/file-log-watcher.mjs +126 -0
- package/es/index.d.ts +7 -0
- package/es/index.mjs +7 -0
- package/es/logger-core.d.ts +37 -0
- package/es/logger-core.mjs +143 -0
- package/es/logger.d.ts +15 -0
- package/es/logger.mjs +127 -0
- package/es/node-logger.d.ts +38 -0
- package/es/node-logger.mjs +230 -0
- package/es/node-utils.d.ts +28 -0
- package/es/node-utils.mjs +56 -0
- package/es/node.d.ts +6 -0
- package/es/node.mjs +6 -0
- package/es/options.d.ts +63 -0
- package/es/options.mjs +30 -0
- package/es/process-logger.d.ts +97 -0
- package/es/process-logger.mjs +193 -0
- package/es/provider.d.ts +160 -0
- package/es/provider.mjs +0 -0
- package/es/types.d.ts +275 -0
- package/es/types.mjs +40 -0
- package/es/utils.d.ts +34 -0
- package/es/utils.mjs +53 -0
- package/lib/constants.cjs +188 -0
- package/lib/constants.d.ts +145 -0
- package/lib/file-log-watcher.cjs +160 -0
- package/lib/file-log-watcher.d.ts +20 -0
- package/lib/index.cjs +33 -0
- package/lib/index.d.ts +7 -0
- package/lib/logger-core.cjs +177 -0
- package/lib/logger-core.d.ts +37 -0
- package/lib/logger.cjs +145 -0
- package/lib/logger.d.ts +15 -0
- package/lib/node-logger.cjs +252 -0
- package/lib/node-logger.d.ts +38 -0
- package/lib/node-utils.cjs +93 -0
- package/lib/node-utils.d.ts +28 -0
- package/lib/node.cjs +31 -0
- package/lib/node.d.ts +6 -0
- package/lib/options.cjs +54 -0
- package/lib/options.d.ts +63 -0
- package/lib/process-logger.cjs +217 -0
- package/lib/process-logger.d.ts +97 -0
- package/lib/provider.cjs +15 -0
- package/lib/provider.d.ts +160 -0
- package/lib/types.cjs +64 -0
- package/lib/types.d.ts +275 -0
- package/lib/utils.cjs +80 -0
- package/lib/utils.d.ts +34 -0
- package/package.json +34 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var logger_core_exports = {};
|
|
19
|
+
__export(logger_core_exports, {
|
|
20
|
+
LEVEL_NAMES: () => LEVEL_NAMES,
|
|
21
|
+
LogLevel: () => LogLevel,
|
|
22
|
+
configureLogger: () => configureLogger,
|
|
23
|
+
formatBytes: () => formatBytes,
|
|
24
|
+
formatContext: () => formatContext,
|
|
25
|
+
formatDuration: () => formatDuration,
|
|
26
|
+
formatLogMessage: () => formatLogMessage,
|
|
27
|
+
formatValue: () => formatValue,
|
|
28
|
+
generateTraceId: () => generateTraceId,
|
|
29
|
+
getConfig: () => getConfig,
|
|
30
|
+
getTimestamp: () => getTimestamp,
|
|
31
|
+
setVerbose: () => setVerbose
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(logger_core_exports);
|
|
34
|
+
var import_constants = require("./constants.cjs");
|
|
35
|
+
var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
|
|
36
|
+
LogLevel2[LogLevel2["DEBUG"] = 0] = "DEBUG";
|
|
37
|
+
LogLevel2[LogLevel2["INFO"] = 1] = "INFO";
|
|
38
|
+
LogLevel2[LogLevel2["WARN"] = 2] = "WARN";
|
|
39
|
+
LogLevel2[LogLevel2["ERROR"] = 3] = "ERROR";
|
|
40
|
+
LogLevel2[LogLevel2["NONE"] = 4] = "NONE";
|
|
41
|
+
return LogLevel2;
|
|
42
|
+
})(LogLevel || {});
|
|
43
|
+
const LEVEL_NAMES = {
|
|
44
|
+
[0 /* DEBUG */]: "DEBUG",
|
|
45
|
+
[1 /* INFO */]: "INFO",
|
|
46
|
+
[2 /* WARN */]: "WARN",
|
|
47
|
+
[3 /* ERROR */]: "ERROR",
|
|
48
|
+
[4 /* NONE */]: "NONE"
|
|
49
|
+
};
|
|
50
|
+
let traceCounter = 0;
|
|
51
|
+
let globalConfig = {
|
|
52
|
+
verbose: false,
|
|
53
|
+
level: 1 /* INFO */,
|
|
54
|
+
showTimestamp: true,
|
|
55
|
+
showCaller: true,
|
|
56
|
+
showTrace: false,
|
|
57
|
+
indent: " "
|
|
58
|
+
};
|
|
59
|
+
function getConfig() {
|
|
60
|
+
return globalConfig;
|
|
61
|
+
}
|
|
62
|
+
function configureLogger(options) {
|
|
63
|
+
globalConfig = { ...globalConfig, ...options };
|
|
64
|
+
}
|
|
65
|
+
function setVerbose(verbose) {
|
|
66
|
+
globalConfig.verbose = verbose;
|
|
67
|
+
globalConfig.level = verbose ? 0 /* DEBUG */ : 1 /* INFO */;
|
|
68
|
+
}
|
|
69
|
+
function getTimestamp() {
|
|
70
|
+
const now = /* @__PURE__ */ new Date();
|
|
71
|
+
const hours = String(now.getHours()).padStart(2, "0");
|
|
72
|
+
const minutes = String(now.getMinutes()).padStart(2, "0");
|
|
73
|
+
const seconds = String(now.getSeconds()).padStart(2, "0");
|
|
74
|
+
const ms = String(now.getMilliseconds()).padStart(3, "0");
|
|
75
|
+
return `${hours}:${minutes}:${seconds}.${ms}`;
|
|
76
|
+
}
|
|
77
|
+
function formatValue(value, depth = 0) {
|
|
78
|
+
if (depth > 3) return "...";
|
|
79
|
+
if (value === null) return "null";
|
|
80
|
+
if (value === void 0) return "undefined";
|
|
81
|
+
if (typeof value === "string") return depth > 0 ? `"${value}"` : value;
|
|
82
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
83
|
+
if (value instanceof Error) {
|
|
84
|
+
return `${value.name}: ${value.message}${value.stack ? `
|
|
85
|
+
${value.stack}` : ""}`;
|
|
86
|
+
}
|
|
87
|
+
if (Array.isArray(value)) {
|
|
88
|
+
if (value.length === 0) return "[]";
|
|
89
|
+
if (value.length > 5) {
|
|
90
|
+
const items2 = value.slice(0, 3).map((v) => formatValue(v, depth + 1));
|
|
91
|
+
return `[${items2.join(", ")}, ... ${value.length - 3} more items]`;
|
|
92
|
+
}
|
|
93
|
+
const items = value.map((v) => formatValue(v, depth + 1));
|
|
94
|
+
return `[${items.join(", ")}]`;
|
|
95
|
+
}
|
|
96
|
+
if (typeof value === "object") {
|
|
97
|
+
const entries = Object.entries(value);
|
|
98
|
+
if (entries.length === 0) return "{}";
|
|
99
|
+
if (entries.length > 5) {
|
|
100
|
+
const shown = entries.slice(0, 3).map(([k, v]) => `${k}: ${formatValue(v, depth + 1)}`);
|
|
101
|
+
return `{${shown.join(", ")}, ... ${entries.length - 3} more keys}`;
|
|
102
|
+
}
|
|
103
|
+
const formatted = entries.map(([k, v]) => `${k}: ${formatValue(v, depth + 1)}`);
|
|
104
|
+
return `{${formatted.join(", ")}}`;
|
|
105
|
+
}
|
|
106
|
+
return String(value);
|
|
107
|
+
}
|
|
108
|
+
function formatContext(context) {
|
|
109
|
+
if (!context || Object.keys(context).length === 0) return "";
|
|
110
|
+
const parts = [];
|
|
111
|
+
if (context.module) parts.push(`[${context.module}]`);
|
|
112
|
+
if (context.operation) parts.push(`(${context.operation})`);
|
|
113
|
+
if (context.traceId) parts.push(`trace:${context.traceId}`);
|
|
114
|
+
if (context.duration !== void 0) parts.push(`${context.duration}ms`);
|
|
115
|
+
const extraKeys = Object.keys(context).filter(
|
|
116
|
+
(k) => !["module", "operation", "traceId", "duration", "error"].includes(k)
|
|
117
|
+
);
|
|
118
|
+
if (extraKeys.length > 0) {
|
|
119
|
+
const extra = {};
|
|
120
|
+
extraKeys.forEach((k) => extra[k] = context[k]);
|
|
121
|
+
parts.push(formatValue(extra));
|
|
122
|
+
}
|
|
123
|
+
return parts.join(" ");
|
|
124
|
+
}
|
|
125
|
+
function generateTraceId() {
|
|
126
|
+
traceCounter++;
|
|
127
|
+
const timestamp = Date.now().toString(36);
|
|
128
|
+
const counter = traceCounter.toString(36).padStart(4, "0");
|
|
129
|
+
return `${timestamp}-${counter}`;
|
|
130
|
+
}
|
|
131
|
+
function formatBytes(bytes) {
|
|
132
|
+
if (bytes === 0) return "0B";
|
|
133
|
+
const k = 1024;
|
|
134
|
+
const sizes = ["B", "KB", "MB", "GB"];
|
|
135
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
136
|
+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))}${sizes[i]}`;
|
|
137
|
+
}
|
|
138
|
+
function formatDuration(ms) {
|
|
139
|
+
if (ms < 1e3) return `${ms}ms`;
|
|
140
|
+
if (ms < 6e4) return `${(ms / 1e3).toFixed(2)}s`;
|
|
141
|
+
const minutes = Math.floor(ms / 6e4);
|
|
142
|
+
const seconds = Math.round(ms % 6e4 / 1e3);
|
|
143
|
+
return `${minutes}m ${seconds}s`;
|
|
144
|
+
}
|
|
145
|
+
function formatLogMessage(level, message, context, ...args) {
|
|
146
|
+
const parts = [];
|
|
147
|
+
if (getConfig().showTimestamp) {
|
|
148
|
+
parts.push(getTimestamp());
|
|
149
|
+
}
|
|
150
|
+
parts.push(LEVEL_NAMES[level].padEnd(5));
|
|
151
|
+
parts.push(import_constants.LOG_PREFIX);
|
|
152
|
+
const contextStr = formatContext(context);
|
|
153
|
+
if (contextStr) {
|
|
154
|
+
parts.push(contextStr);
|
|
155
|
+
}
|
|
156
|
+
parts.push(message);
|
|
157
|
+
const formattedArgs = args.map((a) => formatValue(a)).join(" ");
|
|
158
|
+
if (formattedArgs) {
|
|
159
|
+
parts.push(formattedArgs);
|
|
160
|
+
}
|
|
161
|
+
return parts.join(" ");
|
|
162
|
+
}
|
|
163
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
164
|
+
0 && (module.exports = {
|
|
165
|
+
LEVEL_NAMES,
|
|
166
|
+
LogLevel,
|
|
167
|
+
configureLogger,
|
|
168
|
+
formatBytes,
|
|
169
|
+
formatContext,
|
|
170
|
+
formatDuration,
|
|
171
|
+
formatLogMessage,
|
|
172
|
+
formatValue,
|
|
173
|
+
generateTraceId,
|
|
174
|
+
getConfig,
|
|
175
|
+
getTimestamp,
|
|
176
|
+
setVerbose
|
|
177
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export declare enum LogLevel {
|
|
2
|
+
DEBUG = 0,
|
|
3
|
+
INFO = 1,
|
|
4
|
+
WARN = 2,
|
|
5
|
+
ERROR = 3,
|
|
6
|
+
NONE = 4
|
|
7
|
+
}
|
|
8
|
+
export interface LogContext {
|
|
9
|
+
module?: string;
|
|
10
|
+
operation?: string;
|
|
11
|
+
traceId?: string;
|
|
12
|
+
duration?: number;
|
|
13
|
+
error?: Error | unknown;
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
export interface LoggerConfig {
|
|
17
|
+
verbose: boolean;
|
|
18
|
+
level: LogLevel;
|
|
19
|
+
showTimestamp: boolean;
|
|
20
|
+
showCaller: boolean;
|
|
21
|
+
showTrace: boolean;
|
|
22
|
+
indent: string;
|
|
23
|
+
}
|
|
24
|
+
export declare const LEVEL_NAMES: Record<LogLevel, string>;
|
|
25
|
+
export declare function getConfig(): LoggerConfig;
|
|
26
|
+
export declare function configureLogger(options: Partial<LoggerConfig>): void;
|
|
27
|
+
export declare function setVerbose(verbose: boolean): void;
|
|
28
|
+
export declare function getTimestamp(): string;
|
|
29
|
+
export declare function formatValue(value: unknown, depth?: number): string;
|
|
30
|
+
export declare function formatContext(context?: LogContext): string;
|
|
31
|
+
export declare function generateTraceId(): string;
|
|
32
|
+
export declare function formatBytes(bytes: number): string;
|
|
33
|
+
export declare function formatDuration(ms: number): string;
|
|
34
|
+
/**
|
|
35
|
+
* 格式化日志消息
|
|
36
|
+
*/
|
|
37
|
+
export declare function formatLogMessage(level: LogLevel, message: string, context?: LogContext, ...args: unknown[]): string;
|
package/lib/logger.cjs
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var logger_exports = {};
|
|
19
|
+
__export(logger_exports, {
|
|
20
|
+
createLogger: () => createLogger,
|
|
21
|
+
logger: () => logger
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(logger_exports);
|
|
24
|
+
var import_constants = require("./constants.cjs");
|
|
25
|
+
var import_logger_core = require("./logger-core.cjs");
|
|
26
|
+
const LEVEL_NAMES = {
|
|
27
|
+
[import_logger_core.LogLevel.DEBUG]: "DEBUG",
|
|
28
|
+
[import_logger_core.LogLevel.INFO]: "INFO ",
|
|
29
|
+
[import_logger_core.LogLevel.WARN]: "WARN ",
|
|
30
|
+
[import_logger_core.LogLevel.ERROR]: "ERROR",
|
|
31
|
+
[import_logger_core.LogLevel.NONE]: "NONE "
|
|
32
|
+
};
|
|
33
|
+
const C = {
|
|
34
|
+
dim: "color: #888",
|
|
35
|
+
bright: "font-weight: bold",
|
|
36
|
+
red: "color: #cd0000; font-weight: bold",
|
|
37
|
+
green: "color: #00cd00",
|
|
38
|
+
yellow: "color: #cdcd00",
|
|
39
|
+
blue: "color: #0000cd",
|
|
40
|
+
magenta: "color: #cd00cd",
|
|
41
|
+
cyan: "color: #00cdcd",
|
|
42
|
+
reset: ""
|
|
43
|
+
};
|
|
44
|
+
const LEVEL_COLORS = {
|
|
45
|
+
[import_logger_core.LogLevel.DEBUG]: C.cyan,
|
|
46
|
+
[import_logger_core.LogLevel.INFO]: C.green,
|
|
47
|
+
[import_logger_core.LogLevel.WARN]: C.yellow,
|
|
48
|
+
[import_logger_core.LogLevel.ERROR]: C.red,
|
|
49
|
+
[import_logger_core.LogLevel.NONE]: C.reset
|
|
50
|
+
};
|
|
51
|
+
function log(level, message, context, ...args) {
|
|
52
|
+
if (level < (0, import_logger_core.getConfig)().level) return;
|
|
53
|
+
const segments = [];
|
|
54
|
+
const styles = [];
|
|
55
|
+
if ((0, import_logger_core.getConfig)().showTimestamp) {
|
|
56
|
+
segments.push("%c%s");
|
|
57
|
+
styles.push(C.dim, (0, import_logger_core.getTimestamp)());
|
|
58
|
+
}
|
|
59
|
+
segments.push(`%c${LEVEL_NAMES[level]}`);
|
|
60
|
+
styles.push(LEVEL_COLORS[level]);
|
|
61
|
+
segments.push(`%c${import_constants.LOG_PREFIX}`);
|
|
62
|
+
styles.push(C.bright);
|
|
63
|
+
const ctxStr = (0, import_logger_core.formatContext)(context);
|
|
64
|
+
if (ctxStr) {
|
|
65
|
+
segments.push(`%c${ctxStr}`);
|
|
66
|
+
styles.push(C.magenta);
|
|
67
|
+
}
|
|
68
|
+
const formattedArgs = args.length > 0 ? ` ${args.map((a) => (0, import_logger_core.formatValue)(a)).join(" ")}` : "";
|
|
69
|
+
segments.push(`%c${message}${formattedArgs}`);
|
|
70
|
+
styles.push(C.reset);
|
|
71
|
+
const output = segments.join(" ");
|
|
72
|
+
if (context?.error) {
|
|
73
|
+
const err = context.error;
|
|
74
|
+
if (err instanceof Error) {
|
|
75
|
+
if (level >= import_logger_core.LogLevel.ERROR && (0, import_logger_core.getConfig)().showTrace && err.stack) {
|
|
76
|
+
console.error(output, ...styles, `
|
|
77
|
+
${err.stack}`);
|
|
78
|
+
} else {
|
|
79
|
+
console.error(output, ...styles, `
|
|
80
|
+
%cError: ${err.message}`, C.red);
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
console.error(output, ...styles, `
|
|
84
|
+
%cError: ${(0, import_logger_core.formatValue)(err)}`, C.red);
|
|
85
|
+
}
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (level >= import_logger_core.LogLevel.ERROR) {
|
|
89
|
+
console.error(output, ...styles);
|
|
90
|
+
} else if (level === import_logger_core.LogLevel.WARN) {
|
|
91
|
+
console.warn(output, ...styles);
|
|
92
|
+
} else if (level === import_logger_core.LogLevel.DEBUG) {
|
|
93
|
+
console.debug(output, ...styles);
|
|
94
|
+
} else {
|
|
95
|
+
console.log(output, ...styles);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const logger = {
|
|
99
|
+
debug(message, context, ...args) {
|
|
100
|
+
log(import_logger_core.LogLevel.DEBUG, message, context, ...args);
|
|
101
|
+
},
|
|
102
|
+
info(message, context, ...args) {
|
|
103
|
+
log(import_logger_core.LogLevel.INFO, message, context, ...args);
|
|
104
|
+
},
|
|
105
|
+
warn(message, context, ...args) {
|
|
106
|
+
log(import_logger_core.LogLevel.WARN, message, context, ...args);
|
|
107
|
+
},
|
|
108
|
+
error(message, context, ...args) {
|
|
109
|
+
log(import_logger_core.LogLevel.ERROR, message, context, ...args);
|
|
110
|
+
},
|
|
111
|
+
group(label, context) {
|
|
112
|
+
if (!(0, import_logger_core.getConfig)().verbose) return;
|
|
113
|
+
console.group(
|
|
114
|
+
`%c${import_constants.LOG_PREFIX}%c ${label}`,
|
|
115
|
+
C.bright,
|
|
116
|
+
context?.module ? `%c[${context.module}]%c ` : C.reset,
|
|
117
|
+
...context?.module ? [C.magenta, C.reset] : []
|
|
118
|
+
);
|
|
119
|
+
},
|
|
120
|
+
groupEnd() {
|
|
121
|
+
if (!(0, import_logger_core.getConfig)().verbose) return;
|
|
122
|
+
console.groupEnd();
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
function createLogger(module2) {
|
|
126
|
+
return {
|
|
127
|
+
debug(message, context, ...args) {
|
|
128
|
+
logger.debug(message, { ...context, module: module2 }, ...args);
|
|
129
|
+
},
|
|
130
|
+
info(message, context, ...args) {
|
|
131
|
+
logger.info(message, { ...context, module: module2 }, ...args);
|
|
132
|
+
},
|
|
133
|
+
warn(message, context, ...args) {
|
|
134
|
+
logger.warn(message, { ...context, module: module2 }, ...args);
|
|
135
|
+
},
|
|
136
|
+
error(message, context, ...args) {
|
|
137
|
+
logger.error(message, { ...context, module: module2 }, ...args);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
142
|
+
0 && (module.exports = {
|
|
143
|
+
createLogger,
|
|
144
|
+
logger
|
|
145
|
+
});
|
package/lib/logger.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type LogContext } from "./logger-core";
|
|
2
|
+
export declare const logger: {
|
|
3
|
+
debug(message: string, context?: LogContext, ...args: unknown[]): void;
|
|
4
|
+
info(message: string, context?: LogContext, ...args: unknown[]): void;
|
|
5
|
+
warn(message: string, context?: LogContext, ...args: unknown[]): void;
|
|
6
|
+
error(message: string, context?: LogContext, ...args: unknown[]): void;
|
|
7
|
+
group(label: string, context?: LogContext): void;
|
|
8
|
+
groupEnd(): void;
|
|
9
|
+
};
|
|
10
|
+
export declare function createLogger(module: string): {
|
|
11
|
+
debug(message: string, context?: Omit<LogContext, "module">, ...args: unknown[]): void;
|
|
12
|
+
info(message: string, context?: Omit<LogContext, "module">, ...args: unknown[]): void;
|
|
13
|
+
warn(message: string, context?: Omit<LogContext, "module">, ...args: unknown[]): void;
|
|
14
|
+
error(message: string, context?: Omit<LogContext, "module">, ...args: unknown[]): void;
|
|
15
|
+
};
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
20
|
+
var node_logger_exports = {};
|
|
21
|
+
__export(node_logger_exports, {
|
|
22
|
+
PerformanceTimer: () => PerformanceTimer,
|
|
23
|
+
RequestContext: () => RequestContext,
|
|
24
|
+
createLogger: () => createNodeLogger,
|
|
25
|
+
createNodeLogger: () => createNodeLogger,
|
|
26
|
+
logMethod: () => logMethod,
|
|
27
|
+
logger: () => nodeLogger,
|
|
28
|
+
nodeLogger: () => nodeLogger
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(node_logger_exports);
|
|
31
|
+
var import_constants = require("./constants.cjs");
|
|
32
|
+
var import_logger_core = require("./logger-core.cjs");
|
|
33
|
+
const COLORS = {
|
|
34
|
+
reset: "\x1B[0m",
|
|
35
|
+
dim: "\x1B[2m",
|
|
36
|
+
bright: "\x1B[1m",
|
|
37
|
+
red: "\x1B[31m",
|
|
38
|
+
green: "\x1B[32m",
|
|
39
|
+
yellow: "\x1B[33m",
|
|
40
|
+
blue: "\x1B[34m",
|
|
41
|
+
magenta: "\x1B[35m",
|
|
42
|
+
cyan: "\x1B[36m",
|
|
43
|
+
white: "\x1B[37m"
|
|
44
|
+
};
|
|
45
|
+
const LEVEL_COLORS = {
|
|
46
|
+
[import_logger_core.LogLevel.DEBUG]: COLORS.cyan,
|
|
47
|
+
[import_logger_core.LogLevel.INFO]: COLORS.green,
|
|
48
|
+
[import_logger_core.LogLevel.WARN]: COLORS.yellow,
|
|
49
|
+
[import_logger_core.LogLevel.ERROR]: COLORS.red,
|
|
50
|
+
[import_logger_core.LogLevel.NONE]: COLORS.reset
|
|
51
|
+
};
|
|
52
|
+
const LEVEL_NAMES = {
|
|
53
|
+
[import_logger_core.LogLevel.DEBUG]: "DEBUG",
|
|
54
|
+
[import_logger_core.LogLevel.INFO]: "INFO",
|
|
55
|
+
[import_logger_core.LogLevel.WARN]: "WARN",
|
|
56
|
+
[import_logger_core.LogLevel.ERROR]: "ERROR",
|
|
57
|
+
[import_logger_core.LogLevel.NONE]: "NONE"
|
|
58
|
+
};
|
|
59
|
+
function getTimestamp() {
|
|
60
|
+
const now = /* @__PURE__ */ new Date();
|
|
61
|
+
const hours = String(now.getHours()).padStart(2, "0");
|
|
62
|
+
const minutes = String(now.getMinutes()).padStart(2, "0");
|
|
63
|
+
const seconds = String(now.getSeconds()).padStart(2, "0");
|
|
64
|
+
const ms = String(now.getMilliseconds()).padStart(3, "0");
|
|
65
|
+
return `${hours}:${minutes}:${seconds}.${ms}`;
|
|
66
|
+
}
|
|
67
|
+
function getCallerInfo(depth = 3) {
|
|
68
|
+
const stack = new Error().stack;
|
|
69
|
+
if (!stack) return "";
|
|
70
|
+
const lines = stack.split("\n");
|
|
71
|
+
const targetLine = lines[depth];
|
|
72
|
+
if (!targetLine) return "";
|
|
73
|
+
const match = targetLine.match(/at\s+(?:(.+?)\s+\()?(.+?):(\d+):(\d+)\)?/);
|
|
74
|
+
if (!match) return "";
|
|
75
|
+
const [, funcName, filePath, line] = match;
|
|
76
|
+
const fileName = filePath.split("/").pop() || filePath;
|
|
77
|
+
const func = funcName || "<anonymous>";
|
|
78
|
+
return `${fileName}:${line} ${func}`;
|
|
79
|
+
}
|
|
80
|
+
function log(level, message, context, ...args) {
|
|
81
|
+
if (level < (0, import_logger_core.getConfig)().level) return;
|
|
82
|
+
const parts = [];
|
|
83
|
+
parts.push(`${COLORS.dim}[${process.pid}]${COLORS.reset}`);
|
|
84
|
+
if ((0, import_logger_core.getConfig)().showTimestamp) {
|
|
85
|
+
parts.push(`${COLORS.dim}${getTimestamp()}${COLORS.reset}`);
|
|
86
|
+
}
|
|
87
|
+
const levelColor = LEVEL_COLORS[level];
|
|
88
|
+
const levelName = LEVEL_NAMES[level].padEnd(5);
|
|
89
|
+
parts.push(`${levelColor}${levelName}${COLORS.reset}`);
|
|
90
|
+
parts.push(`${COLORS.bright}${import_constants.LOG_PREFIX}${COLORS.reset}`);
|
|
91
|
+
const contextStr = (0, import_logger_core.formatContext)(context);
|
|
92
|
+
if (contextStr) {
|
|
93
|
+
parts.push(`${COLORS.magenta}${contextStr}${COLORS.reset}`);
|
|
94
|
+
}
|
|
95
|
+
parts.push(message);
|
|
96
|
+
if ((0, import_logger_core.getConfig)().showCaller && level >= import_logger_core.LogLevel.WARN) {
|
|
97
|
+
const caller = getCallerInfo(4);
|
|
98
|
+
if (caller) {
|
|
99
|
+
parts.push(`${COLORS.dim}(${caller})${COLORS.reset}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const formattedArgs = args.map((a) => (0, import_logger_core.formatValue)(a)).join(" ");
|
|
103
|
+
if (formattedArgs) {
|
|
104
|
+
parts.push(formattedArgs);
|
|
105
|
+
}
|
|
106
|
+
if (context?.error) {
|
|
107
|
+
const err = context.error;
|
|
108
|
+
if (err instanceof Error) {
|
|
109
|
+
parts.push(`${COLORS.red}Error: ${err.message}${COLORS.reset}`);
|
|
110
|
+
if (level >= import_logger_core.LogLevel.ERROR && (0, import_logger_core.getConfig)().showTrace && err.stack) {
|
|
111
|
+
console.error(`${COLORS.dim}${err.stack}${COLORS.reset}`);
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
parts.push(`${COLORS.red}Error: ${(0, import_logger_core.formatValue)(err)}${COLORS.reset}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const output = parts.join(" ");
|
|
118
|
+
if (level >= import_logger_core.LogLevel.ERROR) {
|
|
119
|
+
console.error(output);
|
|
120
|
+
} else if (level === import_logger_core.LogLevel.WARN) {
|
|
121
|
+
console.warn(output);
|
|
122
|
+
} else {
|
|
123
|
+
console.log(output);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const nodeLogger = {
|
|
127
|
+
debug(message, context, ...args) {
|
|
128
|
+
log(import_logger_core.LogLevel.DEBUG, message, context, ...args);
|
|
129
|
+
},
|
|
130
|
+
info(message, context, ...args) {
|
|
131
|
+
log(import_logger_core.LogLevel.INFO, message, context, ...args);
|
|
132
|
+
},
|
|
133
|
+
warn(message, context, ...args) {
|
|
134
|
+
log(import_logger_core.LogLevel.WARN, message, context, ...args);
|
|
135
|
+
},
|
|
136
|
+
error(message, context, ...args) {
|
|
137
|
+
log(import_logger_core.LogLevel.ERROR, message, context, ...args);
|
|
138
|
+
},
|
|
139
|
+
group(label, context) {
|
|
140
|
+
if (!(0, import_logger_core.getConfig)().verbose) return;
|
|
141
|
+
const contextStr = (0, import_logger_core.formatContext)(context);
|
|
142
|
+
console.log(
|
|
143
|
+
`${COLORS.dim}[${process.pid}]${COLORS.reset} ${COLORS.bright}${import_constants.LOG_PREFIX}${COLORS.reset} ${COLORS.blue}\u25BC${COLORS.reset} ${label}${contextStr ? ` ${contextStr}` : ""}`
|
|
144
|
+
);
|
|
145
|
+
},
|
|
146
|
+
groupEnd() {
|
|
147
|
+
if (!(0, import_logger_core.getConfig)().verbose) return;
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
function createNodeLogger(module2) {
|
|
151
|
+
return {
|
|
152
|
+
debug(message, context, ...args) {
|
|
153
|
+
nodeLogger.debug(message, { ...context, module: module2 }, ...args);
|
|
154
|
+
},
|
|
155
|
+
info(message, context, ...args) {
|
|
156
|
+
nodeLogger.info(message, { ...context, module: module2 }, ...args);
|
|
157
|
+
},
|
|
158
|
+
warn(message, context, ...args) {
|
|
159
|
+
nodeLogger.warn(message, { ...context, module: module2 }, ...args);
|
|
160
|
+
},
|
|
161
|
+
error(message, context, ...args) {
|
|
162
|
+
nodeLogger.error(message, { ...context, module: module2 }, ...args);
|
|
163
|
+
},
|
|
164
|
+
timer(operation, context) {
|
|
165
|
+
return new PerformanceTimer(operation, { ...context, module: module2 });
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
class PerformanceTimer {
|
|
170
|
+
constructor(operation, context) {
|
|
171
|
+
__publicField(this, "startTime");
|
|
172
|
+
__publicField(this, "context");
|
|
173
|
+
__publicField(this, "operation");
|
|
174
|
+
this.operation = operation;
|
|
175
|
+
this.context = context || {};
|
|
176
|
+
this.startTime = performance.now();
|
|
177
|
+
nodeLogger.debug(`\u23F1\uFE0F Starting: ${operation}`, this.context);
|
|
178
|
+
}
|
|
179
|
+
end(message) {
|
|
180
|
+
const duration = Math.round(performance.now() - this.startTime);
|
|
181
|
+
const msg = message || `\u2713 Completed: ${this.operation}`;
|
|
182
|
+
nodeLogger.debug(msg, { ...this.context, duration });
|
|
183
|
+
return duration;
|
|
184
|
+
}
|
|
185
|
+
checkpoint(label) {
|
|
186
|
+
const elapsed = Math.round(performance.now() - this.startTime);
|
|
187
|
+
nodeLogger.debug(` \u21B3 ${label}`, { ...this.context, duration: elapsed });
|
|
188
|
+
return elapsed;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
class RequestContext {
|
|
192
|
+
constructor(method, path) {
|
|
193
|
+
__publicField(this, "traceId");
|
|
194
|
+
__publicField(this, "method");
|
|
195
|
+
__publicField(this, "path");
|
|
196
|
+
__publicField(this, "startTime");
|
|
197
|
+
__publicField(this, "checkpoints", []);
|
|
198
|
+
this.traceId = (0, import_logger_core.generateTraceId)();
|
|
199
|
+
this.method = method;
|
|
200
|
+
this.path = path;
|
|
201
|
+
this.startTime = performance.now();
|
|
202
|
+
nodeLogger.debug(`\u2192 ${method} ${path}`, { traceId: this.traceId, module: "HTTP" });
|
|
203
|
+
}
|
|
204
|
+
checkpoint(label) {
|
|
205
|
+
const elapsed = Math.round(performance.now() - this.startTime);
|
|
206
|
+
this.checkpoints.push({ time: elapsed, label });
|
|
207
|
+
nodeLogger.debug(` \u2192 ${label}`, { traceId: this.traceId, duration: elapsed });
|
|
208
|
+
}
|
|
209
|
+
end(statusCode) {
|
|
210
|
+
const duration = Math.round(performance.now() - this.startTime);
|
|
211
|
+
const statusColor = statusCode < 400 ? COLORS.green : COLORS.red;
|
|
212
|
+
nodeLogger.debug(`\u2190 ${this.method} ${this.path} ${statusColor}${statusCode}${COLORS.reset}`, {
|
|
213
|
+
traceId: this.traceId,
|
|
214
|
+
duration,
|
|
215
|
+
checkpoints: this.checkpoints.length
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
error(error) {
|
|
219
|
+
const duration = Math.round(performance.now() - this.startTime);
|
|
220
|
+
nodeLogger.error(`\u2717 ${this.method} ${this.path}`, {
|
|
221
|
+
traceId: this.traceId,
|
|
222
|
+
duration,
|
|
223
|
+
error
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
function logMethod(target, propertyKey, descriptor) {
|
|
228
|
+
const originalMethod = descriptor.value;
|
|
229
|
+
const className = target.constructor.name;
|
|
230
|
+
descriptor.value = async function(...args) {
|
|
231
|
+
const timer = new PerformanceTimer(`${className}.${propertyKey}`);
|
|
232
|
+
try {
|
|
233
|
+
const result = await originalMethod.apply(this, args);
|
|
234
|
+
timer.end();
|
|
235
|
+
return result;
|
|
236
|
+
} catch (error) {
|
|
237
|
+
timer.end("\u274C Failed");
|
|
238
|
+
throw error;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
return descriptor;
|
|
242
|
+
}
|
|
243
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
244
|
+
0 && (module.exports = {
|
|
245
|
+
PerformanceTimer,
|
|
246
|
+
RequestContext,
|
|
247
|
+
createLogger,
|
|
248
|
+
createNodeLogger,
|
|
249
|
+
logMethod,
|
|
250
|
+
logger,
|
|
251
|
+
nodeLogger
|
|
252
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type LogContext } from "./logger-core";
|
|
2
|
+
export declare const nodeLogger: {
|
|
3
|
+
debug(message: string, context?: LogContext, ...args: unknown[]): void;
|
|
4
|
+
info(message: string, context?: LogContext, ...args: unknown[]): void;
|
|
5
|
+
warn(message: string, context?: LogContext, ...args: unknown[]): void;
|
|
6
|
+
error(message: string, context?: LogContext, ...args: unknown[]): void;
|
|
7
|
+
group(label: string, context?: LogContext): void;
|
|
8
|
+
groupEnd(): void;
|
|
9
|
+
};
|
|
10
|
+
export declare function createNodeLogger(module: string): {
|
|
11
|
+
debug(message: string, context?: Omit<LogContext, "module">, ...args: unknown[]): void;
|
|
12
|
+
info(message: string, context?: Omit<LogContext, "module">, ...args: unknown[]): void;
|
|
13
|
+
warn(message: string, context?: Omit<LogContext, "module">, ...args: unknown[]): void;
|
|
14
|
+
error(message: string, context?: Omit<LogContext, "module">, ...args: unknown[]): void;
|
|
15
|
+
timer(operation: string, context?: Omit<LogContext, "module">): PerformanceTimer;
|
|
16
|
+
};
|
|
17
|
+
export declare class PerformanceTimer {
|
|
18
|
+
private startTime;
|
|
19
|
+
private context;
|
|
20
|
+
private operation;
|
|
21
|
+
constructor(operation: string, context?: LogContext);
|
|
22
|
+
end(message?: string): number;
|
|
23
|
+
checkpoint(label: string): number;
|
|
24
|
+
}
|
|
25
|
+
export declare class RequestContext {
|
|
26
|
+
traceId: string;
|
|
27
|
+
method: string;
|
|
28
|
+
path: string;
|
|
29
|
+
startTime: number;
|
|
30
|
+
private checkpoints;
|
|
31
|
+
constructor(method: string, path: string);
|
|
32
|
+
checkpoint(label: string): void;
|
|
33
|
+
end(statusCode: number): void;
|
|
34
|
+
error(error: Error | unknown): void;
|
|
35
|
+
}
|
|
36
|
+
export declare function logMethod(target: unknown, propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor;
|
|
37
|
+
export { nodeLogger as logger };
|
|
38
|
+
export { createNodeLogger as createLogger };
|