@wsxjs/wsx-logger 0.0.19 → 0.0.21
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 +92 -73
- 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 +92 -73
- 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,123 +1,142 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const
|
|
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
|
+
};
|
|
4
20
|
function isProduction() {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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;
|
|
13
34
|
}
|
|
14
35
|
const DEFAULT_CONFIG = {
|
|
15
36
|
name: "WSX",
|
|
16
37
|
level: isProduction() ? "info" : "debug",
|
|
17
38
|
pretty: !isProduction()
|
|
18
39
|
};
|
|
19
|
-
function
|
|
20
|
-
const { name, level
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
// Use console methods directly (default behavior)
|
|
30
|
-
write: void 0,
|
|
31
|
-
// Use default console write
|
|
32
|
-
...(pinoOptions == null ? void 0 : pinoOptions.browser) || {}
|
|
33
|
-
// Allow override via pinoOptions
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
if (pretty && isNodeEnvironment() && !isProduction()) {
|
|
37
|
-
try {
|
|
38
|
-
return pino(
|
|
39
|
-
options,
|
|
40
|
-
pino.transport({
|
|
41
|
-
target: "pino-pretty",
|
|
42
|
-
options: {
|
|
43
|
-
colorize: true,
|
|
44
|
-
translateTime: "HH:MM:ss.l",
|
|
45
|
-
ignore: "pid,hostname",
|
|
46
|
-
singleLine: false
|
|
47
|
-
}
|
|
48
|
-
})
|
|
49
|
-
);
|
|
50
|
-
} catch {
|
|
51
|
-
console.warn("[wsx-logger] pino-pretty not available, using default formatter");
|
|
52
|
-
return pino(options);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return pino(options);
|
|
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;
|
|
56
50
|
}
|
|
57
51
|
class WSXLogger {
|
|
58
52
|
constructor(config = {}) {
|
|
59
53
|
this.isProd = isProduction();
|
|
60
|
-
this.
|
|
54
|
+
this.name = config.name || DEFAULT_CONFIG.name;
|
|
55
|
+
this.currentLevel = config.level || DEFAULT_CONFIG.level;
|
|
56
|
+
this.logInstance = createLoglevelLogger(config);
|
|
61
57
|
}
|
|
62
58
|
debug(message, ...args) {
|
|
63
|
-
if (!this.isProd) {
|
|
59
|
+
if (!this.isProd || this.shouldLog("debug")) {
|
|
60
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
64
61
|
if (args.length > 0) {
|
|
65
|
-
this.
|
|
62
|
+
this.logInstance.debug(formattedMessage, ...args);
|
|
66
63
|
} else {
|
|
67
|
-
this.
|
|
64
|
+
this.logInstance.debug(formattedMessage);
|
|
68
65
|
}
|
|
69
66
|
}
|
|
70
67
|
}
|
|
71
68
|
info(message, ...args) {
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
this.pinoLogger.info({ args }, message);
|
|
75
|
-
} else {
|
|
76
|
-
this.pinoLogger.info(message);
|
|
77
|
-
}
|
|
78
|
-
} else {
|
|
69
|
+
if (this.shouldLog("info")) {
|
|
70
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
79
71
|
if (args.length > 0) {
|
|
80
|
-
this.
|
|
72
|
+
this.logInstance.info(formattedMessage, ...args);
|
|
81
73
|
} else {
|
|
82
|
-
this.
|
|
74
|
+
this.logInstance.info(formattedMessage);
|
|
83
75
|
}
|
|
84
76
|
}
|
|
85
77
|
}
|
|
86
78
|
warn(message, ...args) {
|
|
79
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
87
80
|
if (args.length > 0) {
|
|
88
|
-
this.
|
|
81
|
+
this.logInstance.warn(formattedMessage, ...args);
|
|
89
82
|
} else {
|
|
90
|
-
this.
|
|
83
|
+
this.logInstance.warn(formattedMessage);
|
|
91
84
|
}
|
|
92
85
|
}
|
|
93
86
|
error(message, ...args) {
|
|
87
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
94
88
|
if (args.length > 0) {
|
|
95
|
-
this.
|
|
89
|
+
this.logInstance.error(formattedMessage, ...args);
|
|
96
90
|
} else {
|
|
97
|
-
this.
|
|
91
|
+
this.logInstance.error(formattedMessage);
|
|
98
92
|
}
|
|
99
93
|
}
|
|
100
94
|
fatal(message, ...args) {
|
|
95
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
101
96
|
if (args.length > 0) {
|
|
102
|
-
this.
|
|
97
|
+
this.logInstance.error(`[FATAL] ${formattedMessage}`, ...args);
|
|
103
98
|
} else {
|
|
104
|
-
this.
|
|
99
|
+
this.logInstance.error(`[FATAL] ${formattedMessage}`);
|
|
105
100
|
}
|
|
106
101
|
}
|
|
107
102
|
trace(message, ...args) {
|
|
108
|
-
if (!this.isProd) {
|
|
103
|
+
if (!this.isProd || this.shouldLog("trace")) {
|
|
104
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
109
105
|
if (args.length > 0) {
|
|
110
|
-
this.
|
|
106
|
+
this.logInstance.trace(formattedMessage, ...args);
|
|
111
107
|
} else {
|
|
112
|
-
this.
|
|
108
|
+
this.logInstance.trace(formattedMessage);
|
|
113
109
|
}
|
|
114
110
|
}
|
|
115
111
|
}
|
|
116
112
|
/**
|
|
117
|
-
*
|
|
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
|
|
118
136
|
*/
|
|
119
|
-
|
|
120
|
-
|
|
137
|
+
getLevel() {
|
|
138
|
+
const numericLevel = this.logInstance.getLevel();
|
|
139
|
+
return NUMERIC_TO_LEVEL[numericLevel] || this.currentLevel;
|
|
121
140
|
}
|
|
122
141
|
}
|
|
123
142
|
const logger = new WSXLogger();
|
|
@@ -130,9 +149,9 @@ function createLogger(componentName, config = {}) {
|
|
|
130
149
|
function createLoggerWithConfig(config) {
|
|
131
150
|
return new WSXLogger(config);
|
|
132
151
|
}
|
|
133
|
-
Object.defineProperty(exports, "
|
|
152
|
+
Object.defineProperty(exports, "loglevel", {
|
|
134
153
|
enumerable: true,
|
|
135
|
-
get: () =>
|
|
154
|
+
get: () => log.log
|
|
136
155
|
});
|
|
137
156
|
exports.WSXLogger = WSXLogger;
|
|
138
157
|
exports.createLogger = createLogger;
|
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":[],"mappings":";;;AAuCA,SAAS,eAAwB;AAC7B,SAAO,OAAO,YAAY,eAAe,QAAQ,IAAI,aAAa;AACtE;AAKA,SAAS,oBAA6B;;AAClC,SAAO,OAAO,YAAY,iBAAe,aA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AAA,EACJ;AAAA;AAAA;AAAA;AAAA,EAKA,gBAAgC;AAC5B,WAAO,KAAK;AAAA,EAChB;AACJ;AAKO,MAAM,SAAS,IAAI,UAAA;AASnB,SAAS,aAAa,eAAuB,SAAuB,IAAY;AACnF,SAAO,IAAI,UAAU;AAAA,IACjB,GAAG;AAAA,IACH,MAAM,OAAO,QAAQ,OAAO,aAAa;AAAA,EAAA,CAC5C;AACL;AAQO,SAAS,uBAAuB,QAA8B;AACjE,SAAO,IAAI,UAAU,MAAM;AAC/B;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","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,iBAAi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AA;AAAA;AAAA,EAKA,SAAS,OAAuB;AAC5B,SAAK,eAAe;AACpB,SAAK,YAAY,SAAS,cAAc,KAAK,CAAqB;AAAA,EACtE;AAAA;AAAA;AAAA;AAAA,EAKA,WAAqB;AACjB,UAAM,eAAe,KAAK,YAAY,SAAA;AACtC,WAAO,iBAAiB,YAAY,KAAK,KAAK;AAAA,EAClD;AACJ;AAKO,MAAM,SAAS,IAAI,UAAA;AASnB,SAAS,aAAa,eAAuB,SAAuB,IAAY;AACnF,SAAO,IAAI,UAAU;AAAA,IACjB,GAAG;AAAA,IACH,MAAM,OAAO,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;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
|
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
|
+
};
|
|
3
19
|
function isProduction() {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
34
|
const DEFAULT_CONFIG = {
|
|
14
35
|
name: "WSX",
|
|
15
36
|
level: isProduction() ? "info" : "debug",
|
|
16
37
|
pretty: !isProduction()
|
|
17
38
|
};
|
|
18
|
-
function
|
|
19
|
-
const { name, level
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
// Use console methods directly (default behavior)
|
|
29
|
-
write: void 0,
|
|
30
|
-
// Use default console write
|
|
31
|
-
...(pinoOptions == null ? void 0 : pinoOptions.browser) || {}
|
|
32
|
-
// Allow override via pinoOptions
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
if (pretty && isNodeEnvironment() && !isProduction()) {
|
|
36
|
-
try {
|
|
37
|
-
return pino(
|
|
38
|
-
options,
|
|
39
|
-
pino.transport({
|
|
40
|
-
target: "pino-pretty",
|
|
41
|
-
options: {
|
|
42
|
-
colorize: true,
|
|
43
|
-
translateTime: "HH:MM:ss.l",
|
|
44
|
-
ignore: "pid,hostname",
|
|
45
|
-
singleLine: false
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
);
|
|
49
|
-
} catch {
|
|
50
|
-
console.warn("[wsx-logger] pino-pretty not available, using default formatter");
|
|
51
|
-
return pino(options);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return pino(options);
|
|
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;
|
|
55
49
|
}
|
|
56
50
|
class WSXLogger {
|
|
57
51
|
constructor(config = {}) {
|
|
58
52
|
this.isProd = isProduction();
|
|
59
|
-
this.
|
|
53
|
+
this.name = config.name || DEFAULT_CONFIG.name;
|
|
54
|
+
this.currentLevel = config.level || DEFAULT_CONFIG.level;
|
|
55
|
+
this.logInstance = createLoglevelLogger(config);
|
|
60
56
|
}
|
|
61
57
|
debug(message, ...args) {
|
|
62
|
-
if (!this.isProd) {
|
|
58
|
+
if (!this.isProd || this.shouldLog("debug")) {
|
|
59
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
63
60
|
if (args.length > 0) {
|
|
64
|
-
this.
|
|
61
|
+
this.logInstance.debug(formattedMessage, ...args);
|
|
65
62
|
} else {
|
|
66
|
-
this.
|
|
63
|
+
this.logInstance.debug(formattedMessage);
|
|
67
64
|
}
|
|
68
65
|
}
|
|
69
66
|
}
|
|
70
67
|
info(message, ...args) {
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
this.pinoLogger.info({ args }, message);
|
|
74
|
-
} else {
|
|
75
|
-
this.pinoLogger.info(message);
|
|
76
|
-
}
|
|
77
|
-
} else {
|
|
68
|
+
if (this.shouldLog("info")) {
|
|
69
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
78
70
|
if (args.length > 0) {
|
|
79
|
-
this.
|
|
71
|
+
this.logInstance.info(formattedMessage, ...args);
|
|
80
72
|
} else {
|
|
81
|
-
this.
|
|
73
|
+
this.logInstance.info(formattedMessage);
|
|
82
74
|
}
|
|
83
75
|
}
|
|
84
76
|
}
|
|
85
77
|
warn(message, ...args) {
|
|
78
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
86
79
|
if (args.length > 0) {
|
|
87
|
-
this.
|
|
80
|
+
this.logInstance.warn(formattedMessage, ...args);
|
|
88
81
|
} else {
|
|
89
|
-
this.
|
|
82
|
+
this.logInstance.warn(formattedMessage);
|
|
90
83
|
}
|
|
91
84
|
}
|
|
92
85
|
error(message, ...args) {
|
|
86
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
93
87
|
if (args.length > 0) {
|
|
94
|
-
this.
|
|
88
|
+
this.logInstance.error(formattedMessage, ...args);
|
|
95
89
|
} else {
|
|
96
|
-
this.
|
|
90
|
+
this.logInstance.error(formattedMessage);
|
|
97
91
|
}
|
|
98
92
|
}
|
|
99
93
|
fatal(message, ...args) {
|
|
94
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
100
95
|
if (args.length > 0) {
|
|
101
|
-
this.
|
|
96
|
+
this.logInstance.error(`[FATAL] ${formattedMessage}`, ...args);
|
|
102
97
|
} else {
|
|
103
|
-
this.
|
|
98
|
+
this.logInstance.error(`[FATAL] ${formattedMessage}`);
|
|
104
99
|
}
|
|
105
100
|
}
|
|
106
101
|
trace(message, ...args) {
|
|
107
|
-
if (!this.isProd) {
|
|
102
|
+
if (!this.isProd || this.shouldLog("trace")) {
|
|
103
|
+
const formattedMessage = formatMessage(this.name, message);
|
|
108
104
|
if (args.length > 0) {
|
|
109
|
-
this.
|
|
105
|
+
this.logInstance.trace(formattedMessage, ...args);
|
|
110
106
|
} else {
|
|
111
|
-
this.
|
|
107
|
+
this.logInstance.trace(formattedMessage);
|
|
112
108
|
}
|
|
113
109
|
}
|
|
114
110
|
}
|
|
115
111
|
/**
|
|
116
|
-
*
|
|
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;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get the underlying loglevel logger instance
|
|
122
|
+
*/
|
|
123
|
+
getLoglevelLogger() {
|
|
124
|
+
return this.logInstance;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Set the log level dynamically
|
|
128
|
+
*/
|
|
129
|
+
setLevel(level) {
|
|
130
|
+
this.currentLevel = level;
|
|
131
|
+
this.logInstance.setLevel(LOG_LEVEL_MAP[level]);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Get the current log level
|
|
117
135
|
*/
|
|
118
|
-
|
|
119
|
-
|
|
136
|
+
getLevel() {
|
|
137
|
+
const numericLevel = this.logInstance.getLevel();
|
|
138
|
+
return NUMERIC_TO_LEVEL[numericLevel] || this.currentLevel;
|
|
120
139
|
}
|
|
121
140
|
}
|
|
122
141
|
const logger = new WSXLogger();
|
|
@@ -134,6 +153,6 @@ export {
|
|
|
134
153
|
createLogger,
|
|
135
154
|
createLoggerWithConfig,
|
|
136
155
|
logger,
|
|
137
|
-
|
|
156
|
+
log2 as loglevel
|
|
138
157
|
};
|
|
139
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":[],"mappings":";;AAuCA,SAAS,eAAwB;AAC7B,SAAO,OAAO,YAAY,eAAe,QAAQ,IAAI,aAAa;AACtE;AAKA,SAAS,oBAA6B;;AAClC,SAAO,OAAO,YAAY,iBAAe,aAAQ,aAAR,mBAAkB,UAAS;AACxE;AAKA,SAAS,uBAAgC;AACrC,SAAO,OAAO,WAAW,eAAe,OAAO,aAAa;AAChE;AAOA,MAAM,iBAA+B;AAAA,EACjC,MAAM;AAAA,EACN,OAAO,iBAAiB,SAAS;AAAA,EACjC,QAAQ,CAAC,aAAA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AA,IACH,MAAM,OAAO,QAAQ,OAAO,aAAa;AAAA,EAAA,CAC5C;AACL;AAQO,SAAS,uBAAuB,QAA8B;AACjE,SAAO,IAAI,UAAU,MAAM;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;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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K,KAAK;AAAA,EAClD;AACJ;AAKO,MAAM,SAAS,IAAI,UAAA;AASnB,SAAS,aAAa,eAAuB,SAAuB,IAAY;AACnF,SAAO,IAAI,UAAU;AAAA,IACjB,GAAG;AAAA,IACH,MAAM,OAAO,QAAQ,OAAO,aAAa;AAAA,EAAA,CAC5C;AACL;AAQO,SAAS,uBAAuB,QAA8B;AACjE,SAAO,IAAI,UAAU,MAAM;AAC/B;"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wsxjs/wsx-logger",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "WSXJS Logger -
|
|
3
|
+
"version": "0.0.21",
|
|
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";
|