@wsxjs/wsx-logger 0.0.18 → 0.0.20
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 +23 -13
- package/dist/index.cjs +159 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +24 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +137 -75
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
- package/src/index.ts +122 -94
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @wsxjs/wsx-logger
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Browser-optimized logging utility for WSXJS, powered by [loglevel](https://github.com/pimterry/loglevel).
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -39,28 +39,29 @@ import { createLoggerWithConfig } from "@wsxjs/wsx-logger";
|
|
|
39
39
|
const logger = createLoggerWithConfig({
|
|
40
40
|
name: "MyApp",
|
|
41
41
|
level: "debug",
|
|
42
|
-
pretty: true, // Enable pretty printing in development
|
|
43
42
|
});
|
|
44
43
|
```
|
|
45
44
|
|
|
46
|
-
### Advanced Usage with
|
|
45
|
+
### Advanced Usage with Loglevel
|
|
47
46
|
|
|
48
47
|
```typescript
|
|
49
|
-
import { createLogger, type
|
|
48
|
+
import { createLogger, type LoglevelLogger } from "@wsxjs/wsx-logger";
|
|
50
49
|
|
|
51
50
|
const wsxLogger = createLogger("MyComponent");
|
|
52
|
-
const
|
|
51
|
+
const loglevelLogger = wsxLogger.getLoglevelLogger(); // Access underlying loglevel logger
|
|
53
52
|
|
|
54
|
-
// Use
|
|
55
|
-
|
|
53
|
+
// Use loglevel's advanced features
|
|
54
|
+
loglevelLogger.setLevel("warn");
|
|
55
|
+
const currentLevel = wsxLogger.getLevel();
|
|
56
|
+
wsxLogger.setLevel("debug");
|
|
56
57
|
```
|
|
57
58
|
|
|
58
59
|
## Configuration
|
|
59
60
|
|
|
60
61
|
### Environment Variables
|
|
61
62
|
|
|
62
|
-
- `NODE_ENV=production`: Automatically sets log level to `info`
|
|
63
|
-
- `NODE_ENV=development`: Automatically sets log level to `debug`
|
|
63
|
+
- `NODE_ENV=production` or `MODE=production`: Automatically sets log level to `info`
|
|
64
|
+
- `NODE_ENV=development` or `MODE=development`: Automatically sets log level to `debug`
|
|
64
65
|
|
|
65
66
|
### Log Levels
|
|
66
67
|
|
|
@@ -69,18 +70,27 @@ pinoLogger.child({ component: "MyComponent" }).info("Child logger");
|
|
|
69
70
|
- `info` - General information (default in production)
|
|
70
71
|
- `warn` - Warnings
|
|
71
72
|
- `error` - Errors
|
|
72
|
-
- `
|
|
73
|
+
- `silent` - Disable all logging
|
|
73
74
|
|
|
74
75
|
## Features
|
|
75
76
|
|
|
76
|
-
- ✅
|
|
77
|
-
- ✅
|
|
77
|
+
- ✅ Browser-optimized with loglevel (~1KB)
|
|
78
|
+
- ✅ Zero dependencies (loglevel is the only dependency)
|
|
78
79
|
- ✅ Compatible with WSXJS core logger interface
|
|
79
80
|
- ✅ TypeScript support
|
|
80
81
|
- ✅ Environment-aware configuration
|
|
81
82
|
- ✅ Component-specific loggers
|
|
83
|
+
- ✅ Dynamic log level control
|
|
84
|
+
- ✅ Production-ready (automatically reduces verbosity in production)
|
|
85
|
+
|
|
86
|
+
## Why Loglevel?
|
|
87
|
+
|
|
88
|
+
- **Lightweight**: Only ~1KB minified
|
|
89
|
+
- **Browser-first**: Designed specifically for browser environments
|
|
90
|
+
- **No Node.js dependencies**: Pure browser implementation
|
|
91
|
+
- **Performance**: Minimal overhead, uses native console methods
|
|
92
|
+
- **Flexible**: Easy to configure and extend
|
|
82
93
|
|
|
83
94
|
## License
|
|
84
95
|
|
|
85
96
|
MIT
|
|
86
|
-
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,160 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const log = require("loglevel");
|
|
4
|
+
const LOG_LEVEL_MAP = {
|
|
5
|
+
trace: "trace",
|
|
6
|
+
debug: "debug",
|
|
7
|
+
info: "info",
|
|
8
|
+
warn: "warn",
|
|
9
|
+
error: "error",
|
|
10
|
+
silent: "silent"
|
|
11
|
+
};
|
|
12
|
+
const NUMERIC_TO_LEVEL = {
|
|
13
|
+
0: "trace",
|
|
14
|
+
1: "debug",
|
|
15
|
+
2: "info",
|
|
16
|
+
3: "warn",
|
|
17
|
+
4: "error",
|
|
18
|
+
5: "silent"
|
|
19
|
+
};
|
|
20
|
+
function isProduction() {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
if (typeof process !== "undefined") {
|
|
23
|
+
return process.env.NODE_ENV === "production" || process.env.MODE === "production";
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
const meta = globalThis;
|
|
27
|
+
if ((_b = (_a = meta.import) == null ? void 0 : _a.meta) == null ? void 0 : _b.env) {
|
|
28
|
+
const viteEnv = meta.import.meta.env;
|
|
29
|
+
return viteEnv.MODE === "production" || viteEnv.PROD === true;
|
|
30
|
+
}
|
|
31
|
+
} catch {
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const DEFAULT_CONFIG = {
|
|
36
|
+
name: "WSX",
|
|
37
|
+
level: isProduction() ? "info" : "debug",
|
|
38
|
+
pretty: !isProduction()
|
|
39
|
+
};
|
|
40
|
+
function createLoglevelLogger(config = {}) {
|
|
41
|
+
const { name, level } = { ...DEFAULT_CONFIG, ...config };
|
|
42
|
+
const loggerName = name || DEFAULT_CONFIG.name;
|
|
43
|
+
const loggerInstance = log.getLogger(loggerName);
|
|
44
|
+
const targetLevel = level || DEFAULT_CONFIG.level;
|
|
45
|
+
loggerInstance.setLevel(LOG_LEVEL_MAP[targetLevel]);
|
|
46
|
+
return loggerInstance;
|
|
47
|
+
}
|
|
48
|
+
function formatMessage(name, message) {
|
|
49
|
+
return name ? `[${name}] ${message}` : message;
|
|
50
|
+
}
|
|
51
|
+
class WSXLogger {
|
|
52
|
+
constructor(config = {}) {
|
|
53
|
+
this.isProd = isProduction();
|
|
54
|
+
this.name = config.name || DEFAULT_CONFIG.name;
|
|
55
|
+
this.currentLevel = config.level || DEFAULT_CONFIG.level;
|
|
56
|
+
this.logInstance = createLoglevelLogger(config);
|
|
57
|
+
}
|
|
58
|
+
debug(message, ...args) {
|
|
59
|
+
if (!this.isProd || this.shouldLog("debug")) {
|
|
60
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
61
|
+
if (args.length > 0) {
|
|
62
|
+
this.logInstance.debug(formattedMessage, ...args);
|
|
63
|
+
} else {
|
|
64
|
+
this.logInstance.debug(formattedMessage);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
info(message, ...args) {
|
|
69
|
+
if (this.shouldLog("info")) {
|
|
70
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
71
|
+
if (args.length > 0) {
|
|
72
|
+
this.logInstance.info(formattedMessage, ...args);
|
|
73
|
+
} else {
|
|
74
|
+
this.logInstance.info(formattedMessage);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
warn(message, ...args) {
|
|
79
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
80
|
+
if (args.length > 0) {
|
|
81
|
+
this.logInstance.warn(formattedMessage, ...args);
|
|
82
|
+
} else {
|
|
83
|
+
this.logInstance.warn(formattedMessage);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
error(message, ...args) {
|
|
87
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
88
|
+
if (args.length > 0) {
|
|
89
|
+
this.logInstance.error(formattedMessage, ...args);
|
|
90
|
+
} else {
|
|
91
|
+
this.logInstance.error(formattedMessage);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
fatal(message, ...args) {
|
|
95
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
96
|
+
if (args.length > 0) {
|
|
97
|
+
this.logInstance.error(`[FATAL] ${formattedMessage}`, ...args);
|
|
98
|
+
} else {
|
|
99
|
+
this.logInstance.error(`[FATAL] ${formattedMessage}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
trace(message, ...args) {
|
|
103
|
+
if (!this.isProd || this.shouldLog("trace")) {
|
|
104
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
105
|
+
if (args.length > 0) {
|
|
106
|
+
this.logInstance.trace(formattedMessage, ...args);
|
|
107
|
+
} else {
|
|
108
|
+
this.logInstance.trace(formattedMessage);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Check if a log level should be logged based on current level
|
|
114
|
+
*/
|
|
115
|
+
shouldLog(level) {
|
|
116
|
+
const levels = ["trace", "debug", "info", "warn", "error", "silent"];
|
|
117
|
+
const currentLevelIndex = levels.indexOf(this.currentLevel);
|
|
118
|
+
const messageLevelIndex = levels.indexOf(level);
|
|
119
|
+
return messageLevelIndex >= currentLevelIndex;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Get the underlying loglevel logger instance
|
|
123
|
+
*/
|
|
124
|
+
getLoglevelLogger() {
|
|
125
|
+
return this.logInstance;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Set the log level dynamically
|
|
129
|
+
*/
|
|
130
|
+
setLevel(level) {
|
|
131
|
+
this.currentLevel = level;
|
|
132
|
+
this.logInstance.setLevel(LOG_LEVEL_MAP[level]);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Get the current log level
|
|
136
|
+
*/
|
|
137
|
+
getLevel() {
|
|
138
|
+
const numericLevel = this.logInstance.getLevel();
|
|
139
|
+
return NUMERIC_TO_LEVEL[numericLevel] || this.currentLevel;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const logger = new WSXLogger();
|
|
143
|
+
function createLogger(componentName, config = {}) {
|
|
144
|
+
return new WSXLogger({
|
|
145
|
+
...config,
|
|
146
|
+
name: config.name || `WSX:${componentName}`
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
function createLoggerWithConfig(config) {
|
|
150
|
+
return new WSXLogger(config);
|
|
151
|
+
}
|
|
152
|
+
Object.defineProperty(exports, "loglevel", {
|
|
153
|
+
enumerable: true,
|
|
154
|
+
get: () => log.log
|
|
155
|
+
});
|
|
156
|
+
exports.WSXLogger = WSXLogger;
|
|
157
|
+
exports.createLogger = createLogger;
|
|
158
|
+
exports.createLoggerWithConfig = createLoggerWithConfig;
|
|
159
|
+
exports.logger = logger;
|
|
2
160
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["/**\n * @wsxjs/wsx-logger\n * Pino-based logging utility for WSXJS\n */\n\nimport pino, { type LoggerOptions } from \"pino\";\nimport type { Logger as PinoLoggerType } from \"pino\";\n\nexport type LogLevel = \"debug\" | \"info\" | \"warn\" | \"error\" | \"fatal\" | \"trace\";\n\n/**\n * Logger interface compatible with WSXJS core logger\n */\nexport interface Logger {\n debug(message: string, ...args: unknown[]): void;\n info(message: string, ...args: unknown[]): void;\n warn(message: string, ...args: unknown[]): void;\n error(message: string, ...args: unknown[]): void;\n fatal?(message: string, ...args: unknown[]): void;\n trace?(message: string, ...args: unknown[]): void;\n}\n\n/**\n * Logger configuration options\n */\nexport interface LoggerConfig {\n /** Logger name/prefix */\n name?: string;\n /** Minimum log level */\n level?: LogLevel;\n /** Enable pretty printing (for development) */\n pretty?: boolean;\n /** Additional pino options */\n pinoOptions?: LoggerOptions;\n}\n\n/**\n * Check if we're in production environment\n */\nfunction isProduction(): boolean {\n return typeof process !== \"undefined\" && process.env.NODE_ENV === \"production\";\n}\n\n/**\n * Check if we're in a Node.js environment\n */\nfunction isNodeEnvironment(): boolean {\n return typeof process !== \"undefined\" && process.versions?.node !== undefined;\n}\n\n/**\n * Check if we're in a browser environment\n */\nfunction isBrowserEnvironment(): boolean {\n return typeof window !== \"undefined\" && typeof document !== \"undefined\";\n}\n\n/**\n * Default logger configuration\n * - Production: info level, no pretty printing\n * - Development: debug level, pretty printing enabled\n */\nconst DEFAULT_CONFIG: LoggerConfig = {\n name: \"WSX\",\n level: isProduction() ? \"info\" : \"debug\",\n pretty: !isProduction(),\n};\n\n/**\n * Create a pino logger instance\n */\nfunction createPinoLogger(config: LoggerConfig = {}): PinoLoggerType {\n const { name, level, pretty, pinoOptions } = { ...DEFAULT_CONFIG, ...config };\n\n const options: LoggerOptions = {\n name: name || DEFAULT_CONFIG.name,\n level: level || DEFAULT_CONFIG.level,\n ...pinoOptions,\n };\n\n // Configure browser-specific options if in browser environment\n if (isBrowserEnvironment()) {\n // In browser, pino automatically uses console methods\n // We can optionally configure browser-specific behavior\n options.browser = {\n asObject: false, // Use console methods directly (default behavior)\n write: undefined, // Use default console write\n ...(pinoOptions?.browser || {}), // Allow override via pinoOptions\n };\n }\n\n // In development and Node.js environment, use pino-pretty for better readability\n if (pretty && isNodeEnvironment() && !isProduction()) {\n try {\n return pino(\n options,\n pino.transport({\n target: \"pino-pretty\",\n options: {\n colorize: true,\n translateTime: \"HH:MM:ss.l\",\n ignore: \"pid,hostname\",\n singleLine: false,\n },\n })\n );\n } catch {\n // Fallback to regular pino if pino-pretty is not available\n console.warn(\"[wsx-logger] pino-pretty not available, using default formatter\");\n return pino(options);\n }\n }\n\n return pino(options);\n}\n\n/**\n * WSX Logger wrapper that implements the Logger interface\n * and uses pino under the hood\n */\nexport class WSXLogger implements Logger {\n private pinoLogger: PinoLoggerType;\n private isProd: boolean;\n\n constructor(config: LoggerConfig = {}) {\n this.isProd = isProduction();\n this.pinoLogger = createPinoLogger(config);\n }\n\n debug(message: string, ...args: unknown[]): void {\n // Always show debug logs in non-production environments\n if (!this.isProd) {\n if (args.length > 0) {\n this.pinoLogger.debug({ args }, message);\n } else {\n this.pinoLogger.debug(message);\n }\n }\n }\n\n info(message: string, ...args: unknown[]): void {\n // Always show info logs in non-production environments\n if (!this.isProd) {\n if (args.length > 0) {\n this.pinoLogger.info({ args }, message);\n } else {\n this.pinoLogger.info(message);\n }\n } else {\n // In production, respect pino's level configuration\n if (args.length > 0) {\n this.pinoLogger.info({ args }, message);\n } else {\n this.pinoLogger.info(message);\n }\n }\n }\n\n warn(message: string, ...args: unknown[]): void {\n // Always show warnings (in both production and development)\n if (args.length > 0) {\n this.pinoLogger.warn({ args }, message);\n } else {\n this.pinoLogger.warn(message);\n }\n }\n\n error(message: string, ...args: unknown[]): void {\n // Always show errors (in both production and development)\n if (args.length > 0) {\n this.pinoLogger.error({ args }, message);\n } else {\n this.pinoLogger.error(message);\n }\n }\n\n fatal(message: string, ...args: unknown[]): void {\n if (args.length > 0) {\n this.pinoLogger.fatal({ args }, message);\n } else {\n this.pinoLogger.fatal(message);\n }\n }\n\n trace(message: string, ...args: unknown[]): void {\n // Always show trace logs in non-production environments\n if (!this.isProd) {\n if (args.length > 0) {\n this.pinoLogger.trace({ args }, message);\n } else {\n this.pinoLogger.trace(message);\n }\n }\n }\n\n /**\n * Get the underlying pino logger instance\n */\n getPinoLogger(): PinoLoggerType {\n return this.pinoLogger;\n }\n}\n\n/**\n * Default logger instance\n */\nexport const logger = new WSXLogger();\n\n/**\n * Create a component-specific logger\n *\n * @param componentName - Name of the component/module\n * @param config - Optional logger configuration\n * @returns Logger instance\n */\nexport function createLogger(componentName: string, config: LoggerConfig = {}): Logger {\n return new WSXLogger({\n ...config,\n name: config.name || `WSX:${componentName}`,\n });\n}\n\n/**\n * Create a logger with custom configuration\n *\n * @param config - Logger configuration\n * @returns Logger instance\n */\nexport function createLoggerWithConfig(config: LoggerConfig): Logger {\n return new WSXLogger(config);\n}\n\n// Export pino types for advanced usage\nexport type { Logger as PinoLogger, LoggerOptions } from \"pino\";\nexport { pino } from \"pino\";\n"],"names":["isProduction","isNodeEnvironment","_a","isBrowserEnvironment","DEFAULT_CONFIG","createPinoLogger","config","name","level","pretty","pinoOptions","options","pino","WSXLogger","message","args","logger","createLogger","componentName","createLoggerWithConfig"],"mappings":"wGAuCA,SAASA,GAAwB,CAC7B,OAAO,OAAO,QAAY,KAAe,QAAQ,IAAI,WAAa,YACtE,CAKA,SAASC,GAA6B,OAClC,OAAO,OAAO,QAAY,OAAeC,EAAA,QAAQ,WAAR,YAAAA,EAAkB,QAAS,MACxE,CAKA,SAASC,GAAgC,CACrC,OAAO,OAAO,OAAW,KAAe,OAAO,SAAa,GAChE,CAOA,MAAMC,EAA+B,CACjC,KAAM,MACN,MAAOJ,IAAiB,OAAS,QACjC,OAAQ,CAACA,EAAA,CACb,EAKA,SAASK,EAAiBC,EAAuB,GAAoB,CACjE,KAAM,CAAE,KAAAC,EAAM,MAAAC,EAAO,OAAAC,EAAQ,YAAAC,CAAA,EAAgB,CAAE,GAAGN,EAAgB,GAAGE,CAAA,EAE/DK,EAAyB,CAC3B,KAAMJ,GAAQH,EAAe,KAC7B,MAAOI,GAASJ,EAAe,MAC/B,GAAGM,CAAA,EAeP,GAXIP,MAGAQ,EAAQ,QAAU,CACd,SAAU,GACV,MAAO,OACP,IAAID,GAAA,YAAAA,EAAa,UAAW,CAAA,CAAC,GAKjCD,GAAUR,KAAuB,CAACD,IAClC,GAAI,CACA,OAAOY,EACHD,EACAC,EAAK,UAAU,CACX,OAAQ,cACR,QAAS,CACL,SAAU,GACV,cAAe,aACf,OAAQ,eACR,WAAY,EAAA,CAChB,CACH,CAAA,CAET,MAAQ,CAEJ,eAAQ,KAAK,iEAAiE,EACvEA,EAAKD,CAAO,CACvB,CAGJ,OAAOC,EAAKD,CAAO,CACvB,CAMO,MAAME,CAA4B,CAIrC,YAAYP,EAAuB,GAAI,CACnC,KAAK,OAASN,EAAA,EACd,KAAK,WAAaK,EAAiBC,CAAM,CAC7C,CAEA,MAAMQ,KAAoBC,EAAuB,CAExC,KAAK,SACFA,EAAK,OAAS,EACd,KAAK,WAAW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s","sources":["../src/index.ts"],"sourcesContent":["/**\n * @wsxjs/wsx-logger\n * Browser-optimized logging utility for WSXJS\n */\n\nimport log from \"loglevel\";\n\nexport type LogLevel = \"trace\" | \"debug\" | \"info\" | \"warn\" | \"error\" | \"silent\";\n\n/**\n * Logger interface compatible with WSXJS core logger\n */\nexport interface Logger {\n debug(message: string, ...args: unknown[]): void;\n info(message: string, ...args: unknown[]): void;\n warn(message: string, ...args: unknown[]): void;\n error(message: string, ...args: unknown[]): void;\n fatal?(message: string, ...args: unknown[]): void;\n trace?(message: string, ...args: unknown[]): void;\n}\n\n/**\n * Logger configuration options\n */\nexport interface LoggerConfig {\n /** Logger name/prefix */\n name?: string;\n /** Minimum log level */\n level?: LogLevel;\n /** Enable pretty printing (for development) - kept for API compatibility */\n pretty?: boolean;\n}\n\n/**\n * Map WSXJS log levels to loglevel string levels\n */\nconst LOG_LEVEL_MAP: Record<LogLevel, log.LogLevelDesc> = {\n trace: \"trace\",\n debug: \"debug\",\n info: \"info\",\n warn: \"warn\",\n error: \"error\",\n silent: \"silent\",\n};\n\n/**\n * Map loglevel numeric levels to WSXJS log levels\n */\nconst NUMERIC_TO_LEVEL: Record<number, LogLevel> = {\n 0: \"trace\",\n 1: \"debug\",\n 2: \"info\",\n 3: \"warn\",\n 4: \"error\",\n 5: \"silent\",\n};\n\n/**\n * Check if we're in production environment\n */\nfunction isProduction(): boolean {\n if (typeof process !== \"undefined\") {\n return process.env.NODE_ENV === \"production\" || process.env.MODE === \"production\";\n }\n // Check Vite's import.meta.env in browser (if available)\n try {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const meta = globalThis as any;\n if (meta.import?.meta?.env) {\n const viteEnv = meta.import.meta.env;\n return viteEnv.MODE === \"production\" || viteEnv.PROD === true;\n }\n } catch {\n // Ignore errors\n }\n return false;\n}\n\n/**\n * Default logger configuration\n * - Production: info level\n * - Development: debug level\n */\nconst DEFAULT_CONFIG: LoggerConfig = {\n name: \"WSX\",\n level: isProduction() ? \"info\" : \"debug\",\n pretty: !isProduction(),\n};\n\n/**\n * Create a loglevel logger instance with prefix support\n */\nfunction createLoglevelLogger(config: LoggerConfig = {}): log.Logger {\n const { name, level } = { ...DEFAULT_CONFIG, ...config };\n\n // Create a new logger instance for this component\n const loggerName = name || DEFAULT_CONFIG.name || \"WSX\";\n const loggerInstance = log.getLogger(loggerName);\n\n // Set the log level\n const targetLevel = level || DEFAULT_CONFIG.level || \"info\";\n loggerInstance.setLevel(LOG_LEVEL_MAP[targetLevel]);\n\n return loggerInstance;\n}\n\n/**\n * Format log message with prefix\n */\nfunction formatMessage(name: string, message: string): string {\n return name ? `[${name}] ${message}` : message;\n}\n\n/**\n * WSX Logger wrapper that implements the Logger interface\n * and uses loglevel under the hood\n */\nexport class WSXLogger implements Logger {\n private logInstance: log.Logger;\n private name: string;\n private isProd: boolean;\n private currentLevel: LogLevel;\n\n constructor(config: LoggerConfig = {}) {\n this.isProd = isProduction();\n this.name = config.name || DEFAULT_CONFIG.name || \"WSX\";\n this.currentLevel =\n config.level || DEFAULT_CONFIG.level || (this.isProd ? \"info\" : \"debug\");\n this.logInstance = createLoglevelLogger(config);\n }\n\n debug(message: string, ...args: unknown[]): void {\n // Always show debug logs in non-production environments\n if (!this.isProd || this.shouldLog(\"debug\")) {\n const formattedMessage = formatMessage(this.name, message);\n if (args.length > 0) {\n this.logInstance.debug(formattedMessage, ...args);\n } else {\n this.logInstance.debug(formattedMessage);\n }\n }\n }\n\n info(message: string, ...args: unknown[]): void {\n if (this.shouldLog(\"info\")) {\n const formattedMessage = formatMessage(this.name, message);\n if (args.length > 0) {\n this.logInstance.info(formattedMessage, ...args);\n } else {\n this.logInstance.info(formattedMessage);\n }\n }\n }\n\n warn(message: string, ...args: unknown[]): void {\n // Always show warnings (in both production and development)\n const formattedMessage = formatMessage(this.name, message);\n if (args.length > 0) {\n this.logInstance.warn(formattedMessage, ...args);\n } else {\n this.logInstance.warn(formattedMessage);\n }\n }\n\n error(message: string, ...args: unknown[]): void {\n // Always show errors (in both production and development)\n const formattedMessage = formatMessage(this.name, message);\n if (args.length > 0) {\n this.logInstance.error(formattedMessage, ...args);\n } else {\n this.logInstance.error(formattedMessage);\n }\n }\n\n fatal(message: string, ...args: unknown[]): void {\n // Fatal is treated as error in loglevel\n const formattedMessage = formatMessage(this.name, message);\n if (args.length > 0) {\n this.logInstance.error(`[FATAL] ${formattedMessage}`, ...args);\n } else {\n this.logInstance.error(`[FATAL] ${formattedMessage}`);\n }\n }\n\n trace(message: string, ...args: unknown[]): void {\n // Always show trace logs in non-production environments\n if (!this.isProd || this.shouldLog(\"trace\")) {\n const formattedMessage = formatMessage(this.name, message);\n if (args.length > 0) {\n this.logInstance.trace(formattedMessage, ...args);\n } else {\n this.logInstance.trace(formattedMessage);\n }\n }\n }\n\n /**\n * Check if a log level should be logged based on current level\n */\n private shouldLog(level: LogLevel): boolean {\n const levels: LogLevel[] = [\"trace\", \"debug\", \"info\", \"warn\", \"error\", \"silent\"];\n const currentLevelIndex = levels.indexOf(this.currentLevel);\n const messageLevelIndex = levels.indexOf(level);\n\n return messageLevelIndex >= currentLevelIndex;\n }\n\n /**\n * Get the underlying loglevel logger instance\n */\n getLoglevelLogger(): log.Logger {\n return this.logInstance;\n }\n\n /**\n * Set the log level dynamically\n */\n setLevel(level: LogLevel): void {\n this.currentLevel = level;\n this.logInstance.setLevel(LOG_LEVEL_MAP[level] as log.LogLevelDesc);\n }\n\n /**\n * Get the current log level\n */\n getLevel(): LogLevel {\n const numericLevel = this.logInstance.getLevel();\n return NUMERIC_TO_LEVEL[numericLevel] || this.currentLevel;\n }\n}\n\n/**\n * Default logger instance\n */\nexport const logger = new WSXLogger();\n\n/**\n * Create a component-specific logger\n *\n * @param componentName - Name of the component/module\n * @param config - Optional logger configuration\n * @returns Logger instance\n */\nexport function createLogger(componentName: string, config: LoggerConfig = {}): Logger {\n return new WSXLogger({\n ...config,\n name: config.name || `WSX:${componentName}`,\n });\n}\n\n/**\n * Create a logger with custom configuration\n *\n * @param config - Logger configuration\n * @returns Logger instance\n */\nexport function createLoggerWithConfig(config: LoggerConfig): Logger {\n return new WSXLogger(config);\n}\n\n// Export loglevel types for advanced usage\nexport type { Logger as LoglevelLogger } from \"loglevel\";\nexport { log as loglevel } from \"loglevel\";\n"],"names":[],"mappings":";;;AAoCA,MAAM,gBAAoD;AAAA,EACtD,OAAO;AAAA,EACP,OAAO;AAAA,EACP,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO;AAAA,EACP,QAAQ;AACZ;AAKA,MAAM,mBAA6C;AAAA,EAC/C,GAAG;AAAA,EACH,GAAG;AAAA,EACH,GAAG;AAAA,EACH,GAAG;AAAA,EACH,GAAG;AAAA,EACH,GAAG;AACP;AAKA,SAAS,eAAwB;;AAC7B,MAAI,OAAO,YAAY,aAAa;AAChC,WAAO,QAAQ,IAAI,aAAa,gBAAgB,QAAQ,IAAI,SAAS;AAAA,EACzE;AAEA,MAAI;AAEA,UAAM,OAAO;AACb,SAAI,gBAAK,WAAL,mBAAa,SAAb,mBAAmB,KAAK;AACxB,YAAM,UAAU,KAAK,OAAO,KAAK;AACjC,aAAO,QAAQ,SAAS,gBAAgB,QAAQ,SAAS;AAAA,IAC7D;AAAA,EACJ,QAAQ;AAAA,EAER;AACA,SAAO;AACX;AAOA,MAAM,iBAA+B;AAAA,EACjC,MAAM;AAAA,EACN,OAAO,iBAAiB,SAAS;AAAA,EACjC,QAAQ,CAAC,aAAA;AACb;AAKA,SAAS,qBAAqB,SAAuB,IAAgB;AACjE,QAAM,EAAE,MAAM,MAAA,IAAU,EAAE,GAAG,gBAAgB,GAAG,OAAA;AAGhD,QAAM,aAAa,QAAQ,eAAe;AAC1C,QAAM,iBAAiB,IAAI,UAAU,UAAU;AAG/C,QAAM,cAAc,SAAS,eAAe;AAC5C,iBAAe,SAAS,cAAc,WAAW,CAAC;AAElD,SAAO;AACX;AAKA,SAAS,cAAc,MAAc,SAAyB;AAC1D,SAAO,OAAO,IAAI,IAAI,K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O,QAAQ,OAAO,aAAa;AAAA,EAAA,CAC5C;AACL;AAQO,SAAS,uBAAuB,QAA8B;AACjE,SAAO,IAAI,UAAU,MAAM;AAC/B;;;;;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @wsxjs/wsx-logger
|
|
3
|
-
*
|
|
3
|
+
* Browser-optimized logging utility for WSXJS
|
|
4
4
|
*/
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
export type LogLevel = "debug" | "info" | "warn" | "error" | "fatal" | "trace";
|
|
5
|
+
import log from "loglevel";
|
|
6
|
+
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "silent";
|
|
8
7
|
/**
|
|
9
8
|
* Logger interface compatible with WSXJS core logger
|
|
10
9
|
*/
|
|
@@ -24,18 +23,18 @@ export interface LoggerConfig {
|
|
|
24
23
|
name?: string;
|
|
25
24
|
/** Minimum log level */
|
|
26
25
|
level?: LogLevel;
|
|
27
|
-
/** Enable pretty printing (for development) */
|
|
26
|
+
/** Enable pretty printing (for development) - kept for API compatibility */
|
|
28
27
|
pretty?: boolean;
|
|
29
|
-
/** Additional pino options */
|
|
30
|
-
pinoOptions?: LoggerOptions;
|
|
31
28
|
}
|
|
32
29
|
/**
|
|
33
30
|
* WSX Logger wrapper that implements the Logger interface
|
|
34
|
-
* and uses
|
|
31
|
+
* and uses loglevel under the hood
|
|
35
32
|
*/
|
|
36
33
|
export declare class WSXLogger implements Logger {
|
|
37
|
-
private
|
|
34
|
+
private logInstance;
|
|
35
|
+
private name;
|
|
38
36
|
private isProd;
|
|
37
|
+
private currentLevel;
|
|
39
38
|
constructor(config?: LoggerConfig);
|
|
40
39
|
debug(message: string, ...args: unknown[]): void;
|
|
41
40
|
info(message: string, ...args: unknown[]): void;
|
|
@@ -44,9 +43,21 @@ export declare class WSXLogger implements Logger {
|
|
|
44
43
|
fatal(message: string, ...args: unknown[]): void;
|
|
45
44
|
trace(message: string, ...args: unknown[]): void;
|
|
46
45
|
/**
|
|
47
|
-
*
|
|
46
|
+
* Check if a log level should be logged based on current level
|
|
48
47
|
*/
|
|
49
|
-
|
|
48
|
+
private shouldLog;
|
|
49
|
+
/**
|
|
50
|
+
* Get the underlying loglevel logger instance
|
|
51
|
+
*/
|
|
52
|
+
getLoglevelLogger(): log.Logger;
|
|
53
|
+
/**
|
|
54
|
+
* Set the log level dynamically
|
|
55
|
+
*/
|
|
56
|
+
setLevel(level: LogLevel): void;
|
|
57
|
+
/**
|
|
58
|
+
* Get the current log level
|
|
59
|
+
*/
|
|
60
|
+
getLevel(): LogLevel;
|
|
50
61
|
}
|
|
51
62
|
/**
|
|
52
63
|
* Default logger instance
|
|
@@ -67,6 +78,6 @@ export declare function createLogger(componentName: string, config?: LoggerConfi
|
|
|
67
78
|
* @returns Logger instance
|
|
68
79
|
*/
|
|
69
80
|
export declare function createLoggerWithConfig(config: LoggerConfig): Logger;
|
|
70
|
-
export type { Logger as
|
|
71
|
-
export {
|
|
81
|
+
export type { Logger as LoglevelLogger } from "loglevel";
|
|
82
|
+
export { log as loglevel } from "loglevel";
|
|
72
83
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,GAAG,MAAM,UAAU,CAAC;AAE3B,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEhF;;GAEG;AACH,MAAM,WAAW,MAAM;IACnB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC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AAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI;IAUhD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI;IAYhD;;OAEG;IACH,OAAO,CAAC,SAAS;IAQjB;;OAEG;IACH,iBAAiB,IAAI,GAAG,CAAC,MAAM;IAI/B;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI;IAK/B;;OAEG;IACH,QAAQ,IAAI,QAAQ;CAIvB;AAED;;GAEG;AACH,eAAO,MAAM,MAAM,WAAkB,CAAC;AAEtC;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,aAAa,EAAE,MAAM,EAAE,MAAM,GAAE,YAAiB,GAAG,MAAM,CAKrF;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CAEnE;AAGD,YAAY,EAAE,MAAM,IAAI,cAAc,EAAE,MAAM,UAAU,CAAC;AACzD,OAAO,EAAE,GAAG,IAAI,QAAQ,EAAE,MAAM,UAAU,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,96 +1,158 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import log from "loglevel";
|
|
2
|
+
import { log as log2 } from "loglevel";
|
|
3
|
+
const LOG_LEVEL_MAP = {
|
|
4
|
+
trace: "trace",
|
|
5
|
+
debug: "debug",
|
|
6
|
+
info: "info",
|
|
7
|
+
warn: "warn",
|
|
8
|
+
error: "error",
|
|
9
|
+
silent: "silent"
|
|
10
|
+
};
|
|
11
|
+
const NUMERIC_TO_LEVEL = {
|
|
12
|
+
0: "trace",
|
|
13
|
+
1: "debug",
|
|
14
|
+
2: "info",
|
|
15
|
+
3: "warn",
|
|
16
|
+
4: "error",
|
|
17
|
+
5: "silent"
|
|
18
|
+
};
|
|
19
|
+
function isProduction() {
|
|
20
|
+
var _a, _b;
|
|
21
|
+
if (typeof process !== "undefined") {
|
|
22
|
+
return process.env.NODE_ENV === "production" || process.env.MODE === "production";
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
const meta = globalThis;
|
|
26
|
+
if ((_b = (_a = meta.import) == null ? void 0 : _a.meta) == null ? void 0 : _b.env) {
|
|
27
|
+
const viteEnv = meta.import.meta.env;
|
|
28
|
+
return viteEnv.MODE === "production" || viteEnv.PROD === true;
|
|
29
|
+
}
|
|
30
|
+
} catch {
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
12
33
|
}
|
|
13
|
-
const
|
|
34
|
+
const DEFAULT_CONFIG = {
|
|
14
35
|
name: "WSX",
|
|
15
|
-
level:
|
|
16
|
-
pretty: !
|
|
36
|
+
level: isProduction() ? "info" : "debug",
|
|
37
|
+
pretty: !isProduction()
|
|
17
38
|
};
|
|
18
|
-
function
|
|
19
|
-
const { name
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
// Use default console write
|
|
29
|
-
...(i == null ? void 0 : i.browser) || {}
|
|
30
|
-
// Allow override via pinoOptions
|
|
31
|
-
}), l && p() && !g())
|
|
32
|
-
try {
|
|
33
|
-
return r(
|
|
34
|
-
t,
|
|
35
|
-
r.transport({
|
|
36
|
-
target: "pino-pretty",
|
|
37
|
-
options: {
|
|
38
|
-
colorize: !0,
|
|
39
|
-
translateTime: "HH:MM:ss.l",
|
|
40
|
-
ignore: "pid,hostname",
|
|
41
|
-
singleLine: !1
|
|
42
|
-
}
|
|
43
|
-
})
|
|
44
|
-
);
|
|
45
|
-
} catch {
|
|
46
|
-
return console.warn("[wsx-logger] pino-pretty not available, using default formatter"), r(t);
|
|
47
|
-
}
|
|
48
|
-
return r(t);
|
|
39
|
+
function createLoglevelLogger(config = {}) {
|
|
40
|
+
const { name, level } = { ...DEFAULT_CONFIG, ...config };
|
|
41
|
+
const loggerName = name || DEFAULT_CONFIG.name;
|
|
42
|
+
const loggerInstance = log.getLogger(loggerName);
|
|
43
|
+
const targetLevel = level || DEFAULT_CONFIG.level;
|
|
44
|
+
loggerInstance.setLevel(LOG_LEVEL_MAP[targetLevel]);
|
|
45
|
+
return loggerInstance;
|
|
46
|
+
}
|
|
47
|
+
function formatMessage(name, message) {
|
|
48
|
+
return name ? `[${name}] ${message}` : message;
|
|
49
49
|
}
|
|
50
|
-
class
|
|
51
|
-
constructor(
|
|
52
|
-
this.isProd =
|
|
50
|
+
class WSXLogger {
|
|
51
|
+
constructor(config = {}) {
|
|
52
|
+
this.isProd = isProduction();
|
|
53
|
+
this.name = config.name || DEFAULT_CONFIG.name;
|
|
54
|
+
this.currentLevel = config.level || DEFAULT_CONFIG.level;
|
|
55
|
+
this.logInstance = createLoglevelLogger(config);
|
|
56
|
+
}
|
|
57
|
+
debug(message, ...args) {
|
|
58
|
+
if (!this.isProd || this.shouldLog("debug")) {
|
|
59
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
60
|
+
if (args.length > 0) {
|
|
61
|
+
this.logInstance.debug(formattedMessage, ...args);
|
|
62
|
+
} else {
|
|
63
|
+
this.logInstance.debug(formattedMessage);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
info(message, ...args) {
|
|
68
|
+
if (this.shouldLog("info")) {
|
|
69
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
70
|
+
if (args.length > 0) {
|
|
71
|
+
this.logInstance.info(formattedMessage, ...args);
|
|
72
|
+
} else {
|
|
73
|
+
this.logInstance.info(formattedMessage);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
warn(message, ...args) {
|
|
78
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
79
|
+
if (args.length > 0) {
|
|
80
|
+
this.logInstance.warn(formattedMessage, ...args);
|
|
81
|
+
} else {
|
|
82
|
+
this.logInstance.warn(formattedMessage);
|
|
83
|
+
}
|
|
53
84
|
}
|
|
54
|
-
|
|
55
|
-
|
|
85
|
+
error(message, ...args) {
|
|
86
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
87
|
+
if (args.length > 0) {
|
|
88
|
+
this.logInstance.error(formattedMessage, ...args);
|
|
89
|
+
} else {
|
|
90
|
+
this.logInstance.error(formattedMessage);
|
|
91
|
+
}
|
|
56
92
|
}
|
|
57
|
-
|
|
58
|
-
|
|
93
|
+
fatal(message, ...args) {
|
|
94
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
95
|
+
if (args.length > 0) {
|
|
96
|
+
this.logInstance.error(`[FATAL] ${formattedMessage}`, ...args);
|
|
97
|
+
} else {
|
|
98
|
+
this.logInstance.error(`[FATAL] ${formattedMessage}`);
|
|
99
|
+
}
|
|
59
100
|
}
|
|
60
|
-
|
|
61
|
-
|
|
101
|
+
trace(message, ...args) {
|
|
102
|
+
if (!this.isProd || this.shouldLog("trace")) {
|
|
103
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
104
|
+
if (args.length > 0) {
|
|
105
|
+
this.logInstance.trace(formattedMessage, ...args);
|
|
106
|
+
} else {
|
|
107
|
+
this.logInstance.trace(formattedMessage);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
62
110
|
}
|
|
63
|
-
|
|
64
|
-
|
|
111
|
+
/**
|
|
112
|
+
* Check if a log level should be logged based on current level
|
|
113
|
+
*/
|
|
114
|
+
shouldLog(level) {
|
|
115
|
+
const levels = ["trace", "debug", "info", "warn", "error", "silent"];
|
|
116
|
+
const currentLevelIndex = levels.indexOf(this.currentLevel);
|
|
117
|
+
const messageLevelIndex = levels.indexOf(level);
|
|
118
|
+
return messageLevelIndex >= currentLevelIndex;
|
|
65
119
|
}
|
|
66
|
-
|
|
67
|
-
|
|
120
|
+
/**
|
|
121
|
+
* Get the underlying loglevel logger instance
|
|
122
|
+
*/
|
|
123
|
+
getLoglevelLogger() {
|
|
124
|
+
return this.logInstance;
|
|
68
125
|
}
|
|
69
|
-
|
|
70
|
-
|
|
126
|
+
/**
|
|
127
|
+
* Set the log level dynamically
|
|
128
|
+
*/
|
|
129
|
+
setLevel(level) {
|
|
130
|
+
this.currentLevel = level;
|
|
131
|
+
this.logInstance.setLevel(LOG_LEVEL_MAP[level]);
|
|
71
132
|
}
|
|
72
133
|
/**
|
|
73
|
-
* Get the
|
|
134
|
+
* Get the current log level
|
|
74
135
|
*/
|
|
75
|
-
|
|
76
|
-
|
|
136
|
+
getLevel() {
|
|
137
|
+
const numericLevel = this.logInstance.getLevel();
|
|
138
|
+
return NUMERIC_TO_LEVEL[numericLevel] || this.currentLevel;
|
|
77
139
|
}
|
|
78
140
|
}
|
|
79
|
-
const
|
|
80
|
-
function
|
|
81
|
-
return new
|
|
82
|
-
...
|
|
83
|
-
name:
|
|
141
|
+
const logger = new WSXLogger();
|
|
142
|
+
function createLogger(componentName, config = {}) {
|
|
143
|
+
return new WSXLogger({
|
|
144
|
+
...config,
|
|
145
|
+
name: config.name || `WSX:${componentName}`
|
|
84
146
|
});
|
|
85
147
|
}
|
|
86
|
-
function
|
|
87
|
-
return new
|
|
148
|
+
function createLoggerWithConfig(config) {
|
|
149
|
+
return new WSXLogger(config);
|
|
88
150
|
}
|
|
89
151
|
export {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
152
|
+
WSXLogger,
|
|
153
|
+
createLogger,
|
|
154
|
+
createLoggerWithConfig,
|
|
155
|
+
logger,
|
|
156
|
+
log2 as loglevel
|
|
95
157
|
};
|
|
96
158
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/**\n * @wsxjs/wsx-logger\n * Pino-based logging utility for WSXJS\n */\n\nimport pino, { type LoggerOptions } from \"pino\";\nimport type { Logger as PinoLoggerType } from \"pino\";\n\nexport type LogLevel = \"debug\" | \"info\" | \"warn\" | \"error\" | \"fatal\" | \"trace\";\n\n/**\n * Logger interface compatible with WSXJS core logger\n */\nexport interface Logger {\n debug(message: string, ...args: unknown[]): void;\n info(message: string, ...args: unknown[]): void;\n warn(message: string, ...args: unknown[]): void;\n error(message: string, ...args: unknown[]): void;\n fatal?(message: string, ...args: unknown[]): void;\n trace?(message: string, ...args: unknown[]): void;\n}\n\n/**\n * Logger configuration options\n */\nexport interface LoggerConfig {\n /** Logger name/prefix */\n name?: string;\n /** Minimum log level */\n level?: LogLevel;\n /** Enable pretty printing (for development) */\n pretty?: boolean;\n /** Additional pino options */\n pinoOptions?: LoggerOptions;\n}\n\n/**\n * Check if we're in production environment\n */\nfunction isProduction(): boolean {\n return typeof process !== \"undefined\" && process.env.NODE_ENV === \"production\";\n}\n\n/**\n * Check if we're in a Node.js environment\n */\nfunction isNodeEnvironment(): boolean {\n return typeof process !== \"undefined\" && process.versions?.node !== undefined;\n}\n\n/**\n * Check if we're in a browser environment\n */\nfunction isBrowserEnvironment(): boolean {\n return typeof window !== \"undefined\" && typeof document !== \"undefined\";\n}\n\n/**\n * Default logger configuration\n * - Production: info level, no pretty printing\n * - Development: debug level, pretty printing enabled\n */\nconst DEFAULT_CONFIG: LoggerConfig = {\n name: \"WSX\",\n level: isProduction() ? \"info\" : \"debug\",\n pretty: !isProduction(),\n};\n\n/**\n * Create a pino logger instance\n */\nfunction createPinoLogger(config: LoggerConfig = {}): PinoLoggerType {\n const { name, level, pretty, pinoOptions } = { ...DEFAULT_CONFIG, ...config };\n\n const options: LoggerOptions = {\n name: name || DEFAULT_CONFIG.name,\n level: level || DEFAULT_CONFIG.level,\n ...pinoOptions,\n };\n\n // Configure browser-specific options if in browser environment\n if (isBrowserEnvironment()) {\n // In browser, pino automatically uses console methods\n // We can optionally configure browser-specific behavior\n options.browser = {\n asObject: false, // Use console methods directly (default behavior)\n write: undefined, // Use default console write\n ...(pinoOptions?.browser || {}), // Allow override via pinoOptions\n };\n }\n\n // In development and Node.js environment, use pino-pretty for better readability\n if (pretty && isNodeEnvironment() && !isProduction()) {\n try {\n return pino(\n options,\n pino.transport({\n target: \"pino-pretty\",\n options: {\n colorize: true,\n translateTime: \"HH:MM:ss.l\",\n ignore: \"pid,hostname\",\n singleLine: false,\n },\n })\n );\n } catch {\n // Fallback to regular pino if pino-pretty is not available\n console.warn(\"[wsx-logger] pino-pretty not available, using default formatter\");\n return pino(options);\n }\n }\n\n return pino(options);\n}\n\n/**\n * WSX Logger wrapper that implements the Logger interface\n * and uses pino under the hood\n */\nexport class WSXLogger implements Logger {\n private pinoLogger: PinoLoggerType;\n private isProd: boolean;\n\n constructor(config: LoggerConfig = {}) {\n this.isProd = isProduction();\n this.pinoLogger = createPinoLogger(config);\n }\n\n debug(message: string, ...args: unknown[]): void {\n // Always show debug logs in non-production environments\n if (!this.isProd) {\n if (args.length > 0) {\n this.pinoLogger.debug({ args }, message);\n } else {\n this.pinoLogger.debug(message);\n }\n }\n }\n\n info(message: string, ...args: unknown[]): void {\n // Always show info logs in non-production environments\n if (!this.isProd) {\n if (args.length > 0) {\n this.pinoLogger.info({ args }, message);\n } else {\n this.pinoLogger.info(message);\n }\n } else {\n // In production, respect pino's level configuration\n if (args.length > 0) {\n this.pinoLogger.info({ args }, message);\n } else {\n this.pinoLogger.info(message);\n }\n }\n }\n\n warn(message: string, ...args: unknown[]): void {\n // Always show warnings (in both production and development)\n if (args.length > 0) {\n this.pinoLogger.warn({ args }, message);\n } else {\n this.pinoLogger.warn(message);\n }\n }\n\n error(message: string, ...args: unknown[]): void {\n // Always show errors (in both production and development)\n if (args.length > 0) {\n this.pinoLogger.error({ args }, message);\n } else {\n this.pinoLogger.error(message);\n }\n }\n\n fatal(message: string, ...args: unknown[]): void {\n if (args.length > 0) {\n this.pinoLogger.fatal({ args }, message);\n } else {\n this.pinoLogger.fatal(message);\n }\n }\n\n trace(message: string, ...args: unknown[]): void {\n // Always show trace logs in non-production environments\n if (!this.isProd) {\n if (args.length > 0) {\n this.pinoLogger.trace({ args }, message);\n } else {\n this.pinoLogger.trace(message);\n }\n }\n }\n\n /**\n * Get the underlying pino logger instance\n */\n getPinoLogger(): PinoLoggerType {\n return this.pinoLogger;\n }\n}\n\n/**\n * Default logger instance\n */\nexport const logger = new WSXLogger();\n\n/**\n * Create a component-specific logger\n *\n * @param componentName - Name of the component/module\n * @param config - Optional logger configuration\n * @returns Logger instance\n */\nexport function createLogger(componentName: string, config: LoggerConfig = {}): Logger {\n return new WSXLogger({\n ...config,\n name: config.name || `WSX:${componentName}`,\n });\n}\n\n/**\n * Create a logger with custom configuration\n *\n * @param config - Logger configuration\n * @returns Logger instance\n */\nexport function createLoggerWithConfig(config: LoggerConfig): Logger {\n return new WSXLogger(config);\n}\n\n// Export pino types for advanced usage\nexport type { Logger as PinoLogger, LoggerOptions } from \"pino\";\nexport { pino } from \"pino\";\n"],"names":["isProduction","isNodeEnvironment","_a","isBrowserEnvironment","DEFAULT_CONFIG","createPinoLogger","config","name","level","pretty","pinoOptions","options","pino","WSXLogger","message","args","logger","createLogger","componentName","createLoggerWithConfig"],"mappings":";;AAuCA,SAASA,IAAwB;AAC7B,SAAO,OAAO,UAAY,OAAe,QAAQ,IAAI,aAAa;AACtE;AAKA,SAASC,IAA6B;;AAClC,SAAO,OAAO,UAAY,SAAeC,IAAA,QAAQ,aAAR,gBAAAA,EAAkB,UAAS;AACxE;AAKA,SAASC,IAAgC;AACrC,SAAO,OAAO,SAAW,OAAe,OAAO,WAAa;AAChE;AAOA,MAAMC,IAA+B;AAAA,EACjC,MAAM;AAAA,EACN,OAAOJ,MAAiB,SAAS;AAAA,EACjC,QAAQ,CAACA,EAAA;AACb;AAKA,SAASK,EAAiBC,IAAuB,IAAoB;AACjE,QAAM,EAAE,MAAAC,GAAM,OAAAC,GAAO,QAAAC,GAAQ,aAAAC,EAAA,IAAgB,EAAE,GAAGN,GA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AoBC,GAAuB;AAE5C,IAAIA,EAAK,SAAS,IACd,KAAK,WAAW,KAAK,EAAE,MAAAA,EAAA,GAAQD,CAAO,IAEtC,KAAK,WAAW,KAAKA,CAAO;AAAA,EAEpC;AAAA,EAEA,MAAMA,MAAoBC,GAAuB;AAE7C,IAAIA,EAAK,SAAS,IACd,KAAK,WAAW,MAAM,EAAE,MAAAA,EAAA,GAAQD,CAAO,IAEvC,KAAK,WAAW,MAAMA,CAAO;AAAA,EAErC;AAAA,EAEA,MAAMA,MAAoBC,GAAuB;AAC7C,IAAIA,EAAK,SAAS,IACd,KAAK,WAAW,MAAM,EAAE,MAAAA,EAAA,GAAQD,CAAO,IAEvC,KAAK,WAAW,MAAMA,CAAO;AAAA,EAErC;AAAA,EAEA,MAAMA,MAAoBC,GAAuB;AAE7C,IAAK,KAAK,WACFA,EAAK,SAAS,IACd,KAAK,WAAW,MAAM,EAAE,MAAAA,EAAA,GAAQD,CAAO,IAEvC,KAAK,WAAW,MAAMA,CAAO;AAAA,EAGzC;AAAA;AAAA;AAAA;AAAA,EAKA,gBAAgC;AAC5B,WAAO,KAAK;AAAA,EAChB;AACJ;AAKO,MAAME,IAAS,IAAIH,EAAA;AASnB,SAASI,EAAaC,GAAuBZ,IAAuB,IAAY;AACnF,SAAO,IAAIO,EAAU;AAAA,IACjB,GAAGP;AAAA,IACH,MAAMA,EAAO,QAAQ,OAAOY,CAAa;AAAA,EAAA,CAC5C;AACL;AAQO,SAASC,EAAuBb,GAA8B;AACjE,SAAO,IAAIO,EAAUP,CAAM;AAC/B;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/**\n * @wsxjs/wsx-logger\n * Browser-optimized logging utility for WSXJS\n */\n\nimport log from \"loglevel\";\n\nexport type LogLevel = \"trace\" | \"debug\" | \"info\" | \"warn\" | \"error\" | \"silent\";\n\n/**\n * Logger interface compatible with WSXJS core logger\n */\nexport interface Logger {\n debug(message: string, ...args: unknown[]): void;\n info(message: string, ...args: unknown[]): void;\n warn(message: string, ...args: unknown[]): void;\n error(message: string, ...args: unknown[]): void;\n fatal?(message: string, ...args: unknown[]): void;\n trace?(message: string, ...args: unknown[]): void;\n}\n\n/**\n * Logger configuration options\n */\nexport interface LoggerConfig {\n /** Logger name/prefix */\n name?: string;\n /** Minimum log level */\n level?: LogLevel;\n /** Enable pretty printing (for development) - kept for API compatibility */\n pretty?: boolean;\n}\n\n/**\n * Map WSXJS log levels to loglevel string levels\n */\nconst LOG_LEVEL_MAP: Record<LogLevel, log.LogLevelDesc> = {\n trace: \"trace\",\n debug: \"debug\",\n info: \"info\",\n warn: \"warn\",\n error: \"error\",\n silent: \"silent\",\n};\n\n/**\n * Map loglevel numeric levels to WSXJS log levels\n */\nconst NUMERIC_TO_LEVEL: Record<number, LogLevel> = {\n 0: \"trace\",\n 1: \"debug\",\n 2: \"info\",\n 3: \"warn\",\n 4: \"error\",\n 5: \"silent\",\n};\n\n/**\n * Check if we're in production environment\n */\nfunction isProduction(): boolean {\n if (typeof process !== \"undefined\") {\n return process.env.NODE_ENV === \"production\" || process.env.MODE === \"production\";\n }\n // Check Vite's import.meta.env in browser (if available)\n try {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const meta = globalThis as any;\n if (meta.import?.meta?.env) {\n const viteEnv = meta.import.meta.env;\n return viteEnv.MODE === \"production\" || viteEnv.PROD === true;\n }\n } catch {\n // Ignore errors\n }\n return false;\n}\n\n/**\n * Default logger configuration\n * - Production: info level\n * - Development: debug level\n */\nconst DEFAULT_CONFIG: LoggerConfig = {\n name: \"WSX\",\n level: isProduction() ? \"info\" : \"debug\",\n pretty: !isProduction(),\n};\n\n/**\n * Create a loglevel logger instance with prefix support\n */\nfunction createLoglevelLogger(config: LoggerConfig = {}): log.Logger {\n const { name, level } = { ...DEFAULT_CONFIG, ...config };\n\n // Create a new logger instance for this component\n const loggerName = name || DEFAULT_CONFIG.name || \"WSX\";\n const loggerInstance = log.getLogger(loggerName);\n\n // Set the log level\n const targetLevel = level || DEFAULT_CONFIG.level || \"info\";\n loggerInstance.setLevel(LOG_LEVEL_MAP[targetLevel]);\n\n return loggerInstance;\n}\n\n/**\n * Format log message with prefix\n */\nfunction formatMessage(name: string, message: string): string {\n return name ? `[${name}] ${message}` : message;\n}\n\n/**\n * WSX Logger wrapper that implements the Logger interface\n * and uses loglevel under the hood\n */\nexport class WSXLogger implements Logger {\n private logInstance: log.Logger;\n private name: string;\n private isProd: boolean;\n private currentLevel: LogLevel;\n\n constructor(config: LoggerConfig = {}) {\n this.isProd = isProduction();\n this.name = config.name || DEFAULT_CONFIG.name || \"WSX\";\n this.currentLevel =\n config.level || DEFAULT_CONFIG.level || (this.isProd ? \"info\" : \"debug\");\n this.logInstance = createLoglevelLogger(config);\n }\n\n debug(message: string, ...args: unknown[]): void {\n // Always show debug logs in non-production environments\n if (!this.isProd || this.shouldLog(\"debug\")) {\n const formattedMessage = formatMessage(this.name, message);\n if (args.length > 0) {\n this.logInstance.debug(formattedMessage, ...args);\n } else {\n this.logInstance.debug(formattedMessage);\n }\n }\n }\n\n info(message: string, ...args: unknown[]): void {\n if (this.shouldLog(\"info\")) {\n const formattedMessage = formatMessage(this.name, message);\n if (args.length > 0) {\n this.logInstance.info(formattedMessage, ...args);\n } else {\n this.logInstance.info(formattedMessage);\n }\n }\n }\n\n warn(message: string, ...args: unknown[]): void {\n // Always show warnings (in both production and development)\n const formattedMessage = formatMessage(this.name, message);\n if (args.length > 0) {\n this.logInstance.warn(formattedMessage, ...args);\n } else {\n this.logInstance.warn(formattedMessage);\n }\n }\n\n error(message: string, ...args: unknown[]): void {\n // Always show errors (in both production and development)\n const formattedMessage = formatMessage(this.name, message);\n if (args.length > 0) {\n this.logInstance.error(formattedMessage, ...args);\n } else {\n this.logInstance.error(formattedMessage);\n }\n }\n\n fatal(message: string, ...args: unknown[]): void {\n // Fatal is treated as error in loglevel\n const formattedMessage = formatMessage(this.name, message);\n if (args.length > 0) {\n this.logInstance.error(`[FATAL] ${formattedMessage}`, ...args);\n } else {\n this.logInstance.error(`[FATAL] ${formattedMessage}`);\n }\n }\n\n trace(message: string, ...args: unknown[]): void {\n // Always show trace logs in non-production environments\n if (!this.isProd || this.shouldLog(\"trace\")) {\n const formattedMessage = formatMessage(this.name, message);\n if (args.length > 0) {\n this.logInstance.trace(formattedMessage, ...args);\n } else {\n this.logInstance.trace(formattedMessage);\n }\n }\n }\n\n /**\n * Check if a log level should be logged based on current level\n */\n private shouldLog(level: LogLevel): boolean {\n const levels: LogLevel[] = [\"trace\", \"debug\", \"info\", \"warn\", \"error\", \"silent\"];\n const currentLevelIndex = levels.indexOf(this.currentLevel);\n const messageLevelIndex = levels.indexOf(level);\n\n return messageLevelIndex >= currentLevelIndex;\n }\n\n /**\n * Get the underlying loglevel logger instance\n */\n getLoglevelLogger(): log.Logger {\n return this.logInstance;\n }\n\n /**\n * Set the log level dynamically\n */\n setLevel(level: LogLevel): void {\n this.currentLevel = level;\n this.logInstance.setLevel(LOG_LEVEL_MAP[level] as log.LogLevelDesc);\n }\n\n /**\n * Get the current log level\n */\n getLevel(): LogLevel {\n const numericLevel = this.logInstance.getLevel();\n return NUMERIC_TO_LEVEL[numericLevel] || this.currentLevel;\n }\n}\n\n/**\n * Default logger instance\n */\nexport const logger = new WSXLogger();\n\n/**\n * Create a component-specific logger\n *\n * @param componentName - Name of the component/module\n * @param config - Optional logger configuration\n * @returns Logger instance\n */\nexport function createLogger(componentName: string, config: LoggerConfig = {}): Logger {\n return new WSXLogger({\n ...config,\n name: config.name || `WSX:${componentName}`,\n });\n}\n\n/**\n * Create a logger with custom configuration\n *\n * @param config - Logger configuration\n * @returns Logger instance\n */\nexport function createLoggerWithConfig(config: LoggerConfig): Logger {\n return new WSXLogger(config);\n}\n\n// Export loglevel types for advanced usage\nexport type { Logger as LoglevelLogger } from \"loglevel\";\nexport { log as loglevel } from \"loglevel\";\n"],"names":[],"mappings":";;AAoCA,MAAM,gBAAoD;AAAA,EACtD,OAAO;AAA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-
"description": "WSXJS Logger -
|
|
3
|
+
"version": "0.0.20",
|
|
4
|
+
"description": "WSXJS Logger - Browser-optimized logging utility for WSXJS",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
7
7
|
"module": "./dist/index.js",
|
|
@@ -20,8 +20,7 @@
|
|
|
20
20
|
"!**/test"
|
|
21
21
|
],
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"
|
|
24
|
-
"pino-pretty": "^13.0.0"
|
|
23
|
+
"loglevel": "^1.9.1"
|
|
25
24
|
},
|
|
26
25
|
"devDependencies": {
|
|
27
26
|
"@types/node": "^20.0.0",
|
|
@@ -34,9 +33,10 @@
|
|
|
34
33
|
"keywords": [
|
|
35
34
|
"wsx",
|
|
36
35
|
"logger",
|
|
37
|
-
"
|
|
36
|
+
"loglevel",
|
|
38
37
|
"logging",
|
|
39
|
-
"web-components"
|
|
38
|
+
"web-components",
|
|
39
|
+
"browser"
|
|
40
40
|
],
|
|
41
41
|
"author": "WSXJS Team",
|
|
42
42
|
"license": "MIT",
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @wsxjs/wsx-logger
|
|
3
|
-
*
|
|
3
|
+
* Browser-optimized logging utility for WSXJS
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import
|
|
7
|
-
import type { Logger as PinoLoggerType } from "pino";
|
|
6
|
+
import log from "loglevel";
|
|
8
7
|
|
|
9
|
-
export type LogLevel = "
|
|
8
|
+
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "silent";
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
11
|
* Logger interface compatible with WSXJS core logger
|
|
@@ -28,37 +27,59 @@ export interface LoggerConfig {
|
|
|
28
27
|
name?: string;
|
|
29
28
|
/** Minimum log level */
|
|
30
29
|
level?: LogLevel;
|
|
31
|
-
/** Enable pretty printing (for development) */
|
|
30
|
+
/** Enable pretty printing (for development) - kept for API compatibility */
|
|
32
31
|
pretty?: boolean;
|
|
33
|
-
/** Additional pino options */
|
|
34
|
-
pinoOptions?: LoggerOptions;
|
|
35
32
|
}
|
|
36
33
|
|
|
37
34
|
/**
|
|
38
|
-
*
|
|
35
|
+
* Map WSXJS log levels to loglevel string levels
|
|
39
36
|
*/
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
37
|
+
const LOG_LEVEL_MAP: Record<LogLevel, log.LogLevelDesc> = {
|
|
38
|
+
trace: "trace",
|
|
39
|
+
debug: "debug",
|
|
40
|
+
info: "info",
|
|
41
|
+
warn: "warn",
|
|
42
|
+
error: "error",
|
|
43
|
+
silent: "silent",
|
|
44
|
+
};
|
|
43
45
|
|
|
44
46
|
/**
|
|
45
|
-
*
|
|
47
|
+
* Map loglevel numeric levels to WSXJS log levels
|
|
46
48
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
const NUMERIC_TO_LEVEL: Record<number, LogLevel> = {
|
|
50
|
+
0: "trace",
|
|
51
|
+
1: "debug",
|
|
52
|
+
2: "info",
|
|
53
|
+
3: "warn",
|
|
54
|
+
4: "error",
|
|
55
|
+
5: "silent",
|
|
56
|
+
};
|
|
50
57
|
|
|
51
58
|
/**
|
|
52
|
-
* Check if we're in
|
|
59
|
+
* Check if we're in production environment
|
|
53
60
|
*/
|
|
54
|
-
function
|
|
55
|
-
|
|
61
|
+
function isProduction(): boolean {
|
|
62
|
+
if (typeof process !== "undefined") {
|
|
63
|
+
return process.env.NODE_ENV === "production" || process.env.MODE === "production";
|
|
64
|
+
}
|
|
65
|
+
// Check Vite's import.meta.env in browser (if available)
|
|
66
|
+
try {
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
68
|
+
const meta = globalThis as any;
|
|
69
|
+
if (meta.import?.meta?.env) {
|
|
70
|
+
const viteEnv = meta.import.meta.env;
|
|
71
|
+
return viteEnv.MODE === "production" || viteEnv.PROD === true;
|
|
72
|
+
}
|
|
73
|
+
} catch {
|
|
74
|
+
// Ignore errors
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
56
77
|
}
|
|
57
78
|
|
|
58
79
|
/**
|
|
59
80
|
* Default logger configuration
|
|
60
|
-
* - Production: info level
|
|
61
|
-
* - Development: debug level
|
|
81
|
+
* - Production: info level
|
|
82
|
+
* - Development: debug level
|
|
62
83
|
*/
|
|
63
84
|
const DEFAULT_CONFIG: LoggerConfig = {
|
|
64
85
|
name: "WSX",
|
|
@@ -67,137 +88,144 @@ const DEFAULT_CONFIG: LoggerConfig = {
|
|
|
67
88
|
};
|
|
68
89
|
|
|
69
90
|
/**
|
|
70
|
-
* Create a
|
|
91
|
+
* Create a loglevel logger instance with prefix support
|
|
71
92
|
*/
|
|
72
|
-
function
|
|
73
|
-
const { name, level
|
|
74
|
-
|
|
75
|
-
const options: LoggerOptions = {
|
|
76
|
-
name: name || DEFAULT_CONFIG.name,
|
|
77
|
-
level: level || DEFAULT_CONFIG.level,
|
|
78
|
-
...pinoOptions,
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
// Configure browser-specific options if in browser environment
|
|
82
|
-
if (isBrowserEnvironment()) {
|
|
83
|
-
// In browser, pino automatically uses console methods
|
|
84
|
-
// We can optionally configure browser-specific behavior
|
|
85
|
-
options.browser = {
|
|
86
|
-
asObject: false, // Use console methods directly (default behavior)
|
|
87
|
-
write: undefined, // Use default console write
|
|
88
|
-
...(pinoOptions?.browser || {}), // Allow override via pinoOptions
|
|
89
|
-
};
|
|
90
|
-
}
|
|
93
|
+
function createLoglevelLogger(config: LoggerConfig = {}): log.Logger {
|
|
94
|
+
const { name, level } = { ...DEFAULT_CONFIG, ...config };
|
|
91
95
|
|
|
92
|
-
//
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return pino(
|
|
96
|
-
options,
|
|
97
|
-
pino.transport({
|
|
98
|
-
target: "pino-pretty",
|
|
99
|
-
options: {
|
|
100
|
-
colorize: true,
|
|
101
|
-
translateTime: "HH:MM:ss.l",
|
|
102
|
-
ignore: "pid,hostname",
|
|
103
|
-
singleLine: false,
|
|
104
|
-
},
|
|
105
|
-
})
|
|
106
|
-
);
|
|
107
|
-
} catch {
|
|
108
|
-
// Fallback to regular pino if pino-pretty is not available
|
|
109
|
-
console.warn("[wsx-logger] pino-pretty not available, using default formatter");
|
|
110
|
-
return pino(options);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
96
|
+
// Create a new logger instance for this component
|
|
97
|
+
const loggerName = name || DEFAULT_CONFIG.name || "WSX";
|
|
98
|
+
const loggerInstance = log.getLogger(loggerName);
|
|
113
99
|
|
|
114
|
-
|
|
100
|
+
// Set the log level
|
|
101
|
+
const targetLevel = level || DEFAULT_CONFIG.level || "info";
|
|
102
|
+
loggerInstance.setLevel(LOG_LEVEL_MAP[targetLevel]);
|
|
103
|
+
|
|
104
|
+
return loggerInstance;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Format log message with prefix
|
|
109
|
+
*/
|
|
110
|
+
function formatMessage(name: string, message: string): string {
|
|
111
|
+
return name ? `[${name}] ${message}` : message;
|
|
115
112
|
}
|
|
116
113
|
|
|
117
114
|
/**
|
|
118
115
|
* WSX Logger wrapper that implements the Logger interface
|
|
119
|
-
* and uses
|
|
116
|
+
* and uses loglevel under the hood
|
|
120
117
|
*/
|
|
121
118
|
export class WSXLogger implements Logger {
|
|
122
|
-
private
|
|
119
|
+
private logInstance: log.Logger;
|
|
120
|
+
private name: string;
|
|
123
121
|
private isProd: boolean;
|
|
122
|
+
private currentLevel: LogLevel;
|
|
124
123
|
|
|
125
124
|
constructor(config: LoggerConfig = {}) {
|
|
126
125
|
this.isProd = isProduction();
|
|
127
|
-
this.
|
|
126
|
+
this.name = config.name || DEFAULT_CONFIG.name || "WSX";
|
|
127
|
+
this.currentLevel =
|
|
128
|
+
config.level || DEFAULT_CONFIG.level || (this.isProd ? "info" : "debug");
|
|
129
|
+
this.logInstance = createLoglevelLogger(config);
|
|
128
130
|
}
|
|
129
131
|
|
|
130
132
|
debug(message: string, ...args: unknown[]): void {
|
|
131
133
|
// Always show debug logs in non-production environments
|
|
132
|
-
if (!this.isProd) {
|
|
134
|
+
if (!this.isProd || this.shouldLog("debug")) {
|
|
135
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
133
136
|
if (args.length > 0) {
|
|
134
|
-
this.
|
|
137
|
+
this.logInstance.debug(formattedMessage, ...args);
|
|
135
138
|
} else {
|
|
136
|
-
this.
|
|
139
|
+
this.logInstance.debug(formattedMessage);
|
|
137
140
|
}
|
|
138
141
|
}
|
|
139
142
|
}
|
|
140
143
|
|
|
141
144
|
info(message: string, ...args: unknown[]): void {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
if (args.length > 0) {
|
|
145
|
-
this.pinoLogger.info({ args }, message);
|
|
146
|
-
} else {
|
|
147
|
-
this.pinoLogger.info(message);
|
|
148
|
-
}
|
|
149
|
-
} else {
|
|
150
|
-
// In production, respect pino's level configuration
|
|
145
|
+
if (this.shouldLog("info")) {
|
|
146
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
151
147
|
if (args.length > 0) {
|
|
152
|
-
this.
|
|
148
|
+
this.logInstance.info(formattedMessage, ...args);
|
|
153
149
|
} else {
|
|
154
|
-
this.
|
|
150
|
+
this.logInstance.info(formattedMessage);
|
|
155
151
|
}
|
|
156
152
|
}
|
|
157
153
|
}
|
|
158
154
|
|
|
159
155
|
warn(message: string, ...args: unknown[]): void {
|
|
160
156
|
// Always show warnings (in both production and development)
|
|
157
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
161
158
|
if (args.length > 0) {
|
|
162
|
-
this.
|
|
159
|
+
this.logInstance.warn(formattedMessage, ...args);
|
|
163
160
|
} else {
|
|
164
|
-
this.
|
|
161
|
+
this.logInstance.warn(formattedMessage);
|
|
165
162
|
}
|
|
166
163
|
}
|
|
167
164
|
|
|
168
165
|
error(message: string, ...args: unknown[]): void {
|
|
169
166
|
// Always show errors (in both production and development)
|
|
167
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
170
168
|
if (args.length > 0) {
|
|
171
|
-
this.
|
|
169
|
+
this.logInstance.error(formattedMessage, ...args);
|
|
172
170
|
} else {
|
|
173
|
-
this.
|
|
171
|
+
this.logInstance.error(formattedMessage);
|
|
174
172
|
}
|
|
175
173
|
}
|
|
176
174
|
|
|
177
175
|
fatal(message: string, ...args: unknown[]): void {
|
|
176
|
+
// Fatal is treated as error in loglevel
|
|
177
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
178
178
|
if (args.length > 0) {
|
|
179
|
-
this.
|
|
179
|
+
this.logInstance.error(`[FATAL] ${formattedMessage}`, ...args);
|
|
180
180
|
} else {
|
|
181
|
-
this.
|
|
181
|
+
this.logInstance.error(`[FATAL] ${formattedMessage}`);
|
|
182
182
|
}
|
|
183
183
|
}
|
|
184
184
|
|
|
185
185
|
trace(message: string, ...args: unknown[]): void {
|
|
186
186
|
// Always show trace logs in non-production environments
|
|
187
|
-
if (!this.isProd) {
|
|
187
|
+
if (!this.isProd || this.shouldLog("trace")) {
|
|
188
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
188
189
|
if (args.length > 0) {
|
|
189
|
-
this.
|
|
190
|
+
this.logInstance.trace(formattedMessage, ...args);
|
|
190
191
|
} else {
|
|
191
|
-
this.
|
|
192
|
+
this.logInstance.trace(formattedMessage);
|
|
192
193
|
}
|
|
193
194
|
}
|
|
194
195
|
}
|
|
195
196
|
|
|
196
197
|
/**
|
|
197
|
-
*
|
|
198
|
+
* Check if a log level should be logged based on current level
|
|
199
|
+
*/
|
|
200
|
+
private shouldLog(level: LogLevel): boolean {
|
|
201
|
+
const levels: LogLevel[] = ["trace", "debug", "info", "warn", "error", "silent"];
|
|
202
|
+
const currentLevelIndex = levels.indexOf(this.currentLevel);
|
|
203
|
+
const messageLevelIndex = levels.indexOf(level);
|
|
204
|
+
|
|
205
|
+
return messageLevelIndex >= currentLevelIndex;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Get the underlying loglevel logger instance
|
|
210
|
+
*/
|
|
211
|
+
getLoglevelLogger(): log.Logger {
|
|
212
|
+
return this.logInstance;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Set the log level dynamically
|
|
217
|
+
*/
|
|
218
|
+
setLevel(level: LogLevel): void {
|
|
219
|
+
this.currentLevel = level;
|
|
220
|
+
this.logInstance.setLevel(LOG_LEVEL_MAP[level] as log.LogLevelDesc);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Get the current log level
|
|
198
225
|
*/
|
|
199
|
-
|
|
200
|
-
|
|
226
|
+
getLevel(): LogLevel {
|
|
227
|
+
const numericLevel = this.logInstance.getLevel();
|
|
228
|
+
return NUMERIC_TO_LEVEL[numericLevel] || this.currentLevel;
|
|
201
229
|
}
|
|
202
230
|
}
|
|
203
231
|
|
|
@@ -230,6 +258,6 @@ export function createLoggerWithConfig(config: LoggerConfig): Logger {
|
|
|
230
258
|
return new WSXLogger(config);
|
|
231
259
|
}
|
|
232
260
|
|
|
233
|
-
// Export
|
|
234
|
-
export type { Logger as
|
|
235
|
-
export {
|
|
261
|
+
// Export loglevel types for advanced usage
|
|
262
|
+
export type { Logger as LoglevelLogger } from "loglevel";
|
|
263
|
+
export { log as loglevel } from "loglevel";
|