@wsxjs/wsx-logger 0.0.18
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/LICENSE +21 -0
- package/README.md +86 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +72 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +96 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -0
- package/src/index.ts +235 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 WSXJS Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# @wsxjs/wsx-logger
|
|
2
|
+
|
|
3
|
+
Pino-based logging utility for WSXJS.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @wsxjs/wsx-logger
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Basic Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { createLogger } from "@wsxjs/wsx-logger";
|
|
17
|
+
|
|
18
|
+
const logger = createLogger("MyComponent");
|
|
19
|
+
|
|
20
|
+
logger.info("Component initialized");
|
|
21
|
+
logger.debug("Debug information");
|
|
22
|
+
logger.warn("Warning message");
|
|
23
|
+
logger.error("Error occurred", error);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Default Logger
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { logger } from "@wsxjs/wsx-logger";
|
|
30
|
+
|
|
31
|
+
logger.info("Application started");
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Custom Configuration
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { createLoggerWithConfig } from "@wsxjs/wsx-logger";
|
|
38
|
+
|
|
39
|
+
const logger = createLoggerWithConfig({
|
|
40
|
+
name: "MyApp",
|
|
41
|
+
level: "debug",
|
|
42
|
+
pretty: true, // Enable pretty printing in development
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Advanced Usage with Pino
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { createLogger, type PinoLogger } from "@wsxjs/wsx-logger";
|
|
50
|
+
|
|
51
|
+
const wsxLogger = createLogger("MyComponent");
|
|
52
|
+
const pinoLogger = wsxLogger.getPinoLogger(); // Access underlying pino logger
|
|
53
|
+
|
|
54
|
+
// Use pino's advanced features
|
|
55
|
+
pinoLogger.child({ component: "MyComponent" }).info("Child logger");
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Configuration
|
|
59
|
+
|
|
60
|
+
### Environment Variables
|
|
61
|
+
|
|
62
|
+
- `NODE_ENV=production`: Automatically sets log level to `info` and disables pretty printing
|
|
63
|
+
- `NODE_ENV=development`: Automatically sets log level to `debug` and enables pretty printing
|
|
64
|
+
|
|
65
|
+
### Log Levels
|
|
66
|
+
|
|
67
|
+
- `trace` - Most verbose
|
|
68
|
+
- `debug` - Debug information
|
|
69
|
+
- `info` - General information (default in production)
|
|
70
|
+
- `warn` - Warnings
|
|
71
|
+
- `error` - Errors
|
|
72
|
+
- `fatal` - Fatal errors
|
|
73
|
+
|
|
74
|
+
## Features
|
|
75
|
+
|
|
76
|
+
- ✅ Pino-based high-performance logging
|
|
77
|
+
- ✅ Pretty printing in development
|
|
78
|
+
- ✅ Compatible with WSXJS core logger interface
|
|
79
|
+
- ✅ TypeScript support
|
|
80
|
+
- ✅ Environment-aware configuration
|
|
81
|
+
- ✅ Component-specific loggers
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
MIT
|
|
86
|
+
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("pino");function g(){return typeof process<"u"&&process.env.NODE_ENV==="production"}function p(){var n;return typeof process<"u"&&((n=process.versions)==null?void 0:n.node)!==void 0}function c(){return typeof window<"u"&&typeof document<"u"}const l={name:"WSX",level:g()?"info":"debug",pretty:!g()};function u(n={}){const{name:e,level:o,pretty:f,pinoOptions:r}={...l,...n},i={name:e||l.name,level:o||l.level,...r};if(c()&&(i.browser={asObject:!1,write:void 0,...(r==null?void 0:r.browser)||{}}),f&&p()&&!g())try{return t(i,t.transport({target:"pino-pretty",options:{colorize:!0,translateTime:"HH:MM:ss.l",ignore:"pid,hostname",singleLine:!1}}))}catch{return console.warn("[wsx-logger] pino-pretty not available, using default formatter"),t(i)}return t(i)}class s{constructor(e={}){this.isProd=g(),this.pinoLogger=u(e)}debug(e,...o){this.isProd||(o.length>0?this.pinoLogger.debug({args:o},e):this.pinoLogger.debug(e))}info(e,...o){this.isProd?o.length>0?this.pinoLogger.info({args:o},e):this.pinoLogger.info(e):o.length>0?this.pinoLogger.info({args:o},e):this.pinoLogger.info(e)}warn(e,...o){o.length>0?this.pinoLogger.warn({args:o},e):this.pinoLogger.warn(e)}error(e,...o){o.length>0?this.pinoLogger.error({args:o},e):this.pinoLogger.error(e)}fatal(e,...o){o.length>0?this.pinoLogger.fatal({args:o},e):this.pinoLogger.fatal(e)}trace(e,...o){this.isProd||(o.length>0?this.pinoLogger.trace({args:o},e):this.pinoLogger.trace(e))}getPinoLogger(){return this.pinoLogger}}const h=new s;function d(n,e={}){return new s({...e,name:e.name||`WSX:${n}`})}function a(n){return new s(n)}Object.defineProperty(exports,"pino",{enumerable:!0,get:()=>t.pino});exports.WSXLogger=s;exports.createLogger=d;exports.createLoggerWithConfig=a;exports.logger=h;
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +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,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I,EAAaC,EAAuBZ,EAAuB,GAAY,CACnF,OAAO,IAAIO,EAAU,CACjB,GAAGP,EACH,KAAMA,EAAO,MAAQ,OAAOY,CAAa,EAAA,CAC5C,CACL,CAQO,SAASC,EAAuBb,EAA8B,CACjE,OAAO,IAAIO,EAAUP,CAAM,CAC/B"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @wsxjs/wsx-logger
|
|
3
|
+
* Pino-based logging utility for WSXJS
|
|
4
|
+
*/
|
|
5
|
+
import { type LoggerOptions } from "pino";
|
|
6
|
+
import type { Logger as PinoLoggerType } from "pino";
|
|
7
|
+
export type LogLevel = "debug" | "info" | "warn" | "error" | "fatal" | "trace";
|
|
8
|
+
/**
|
|
9
|
+
* Logger interface compatible with WSXJS core logger
|
|
10
|
+
*/
|
|
11
|
+
export interface Logger {
|
|
12
|
+
debug(message: string, ...args: unknown[]): void;
|
|
13
|
+
info(message: string, ...args: unknown[]): void;
|
|
14
|
+
warn(message: string, ...args: unknown[]): void;
|
|
15
|
+
error(message: string, ...args: unknown[]): void;
|
|
16
|
+
fatal?(message: string, ...args: unknown[]): void;
|
|
17
|
+
trace?(message: string, ...args: unknown[]): void;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Logger configuration options
|
|
21
|
+
*/
|
|
22
|
+
export interface LoggerConfig {
|
|
23
|
+
/** Logger name/prefix */
|
|
24
|
+
name?: string;
|
|
25
|
+
/** Minimum log level */
|
|
26
|
+
level?: LogLevel;
|
|
27
|
+
/** Enable pretty printing (for development) */
|
|
28
|
+
pretty?: boolean;
|
|
29
|
+
/** Additional pino options */
|
|
30
|
+
pinoOptions?: LoggerOptions;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* WSX Logger wrapper that implements the Logger interface
|
|
34
|
+
* and uses pino under the hood
|
|
35
|
+
*/
|
|
36
|
+
export declare class WSXLogger implements Logger {
|
|
37
|
+
private pinoLogger;
|
|
38
|
+
private isProd;
|
|
39
|
+
constructor(config?: LoggerConfig);
|
|
40
|
+
debug(message: string, ...args: unknown[]): void;
|
|
41
|
+
info(message: string, ...args: unknown[]): void;
|
|
42
|
+
warn(message: string, ...args: unknown[]): void;
|
|
43
|
+
error(message: string, ...args: unknown[]): void;
|
|
44
|
+
fatal(message: string, ...args: unknown[]): void;
|
|
45
|
+
trace(message: string, ...args: unknown[]): void;
|
|
46
|
+
/**
|
|
47
|
+
* Get the underlying pino logger instance
|
|
48
|
+
*/
|
|
49
|
+
getPinoLogger(): PinoLoggerType;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Default logger instance
|
|
53
|
+
*/
|
|
54
|
+
export declare const logger: WSXLogger;
|
|
55
|
+
/**
|
|
56
|
+
* Create a component-specific logger
|
|
57
|
+
*
|
|
58
|
+
* @param componentName - Name of the component/module
|
|
59
|
+
* @param config - Optional logger configuration
|
|
60
|
+
* @returns Logger instance
|
|
61
|
+
*/
|
|
62
|
+
export declare function createLogger(componentName: string, config?: LoggerConfig): Logger;
|
|
63
|
+
/**
|
|
64
|
+
* Create a logger with custom configuration
|
|
65
|
+
*
|
|
66
|
+
* @param config - Logger configuration
|
|
67
|
+
* @returns Logger instance
|
|
68
|
+
*/
|
|
69
|
+
export declare function createLoggerWithConfig(config: LoggerConfig): Logger;
|
|
70
|
+
export type { Logger as PinoLogger, LoggerOptions } from "pino";
|
|
71
|
+
export { pino } from "pino";
|
|
72
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAa,EAAE,KAAK,aAAa,EAAE,MAAM,MAAM,CAAC;AAChD,OAAO,KAAK,EAAE,MAAM,IAAI,cAAc,EAAE,MAAM,MAAM,CAAC;AAErD,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,M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M,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI;IAS/C,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI;IAShD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI;IAQhD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI;IAWhD;;OAEG;IACH,aAAa,IAAI,cAAc;CAGlC;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,UAAU,EAAE,aAAa,EAAE,MAAM,MAAM,CAAC;AAChE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import r from "pino";
|
|
2
|
+
import { pino as y } from "pino";
|
|
3
|
+
function g() {
|
|
4
|
+
return typeof process < "u" && process.env.NODE_ENV === "production";
|
|
5
|
+
}
|
|
6
|
+
function p() {
|
|
7
|
+
var n;
|
|
8
|
+
return typeof process < "u" && ((n = process.versions) == null ? void 0 : n.node) !== void 0;
|
|
9
|
+
}
|
|
10
|
+
function c() {
|
|
11
|
+
return typeof window < "u" && typeof document < "u";
|
|
12
|
+
}
|
|
13
|
+
const s = {
|
|
14
|
+
name: "WSX",
|
|
15
|
+
level: g() ? "info" : "debug",
|
|
16
|
+
pretty: !g()
|
|
17
|
+
};
|
|
18
|
+
function h(n = {}) {
|
|
19
|
+
const { name: e, level: o, pretty: l, pinoOptions: i } = { ...s, ...n }, t = {
|
|
20
|
+
name: e || s.name,
|
|
21
|
+
level: o || s.level,
|
|
22
|
+
...i
|
|
23
|
+
};
|
|
24
|
+
if (c() && (t.browser = {
|
|
25
|
+
asObject: !1,
|
|
26
|
+
// Use console methods directly (default behavior)
|
|
27
|
+
write: void 0,
|
|
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);
|
|
49
|
+
}
|
|
50
|
+
class f {
|
|
51
|
+
constructor(e = {}) {
|
|
52
|
+
this.isProd = g(), this.pinoLogger = h(e);
|
|
53
|
+
}
|
|
54
|
+
debug(e, ...o) {
|
|
55
|
+
this.isProd || (o.length > 0 ? this.pinoLogger.debug({ args: o }, e) : this.pinoLogger.debug(e));
|
|
56
|
+
}
|
|
57
|
+
info(e, ...o) {
|
|
58
|
+
this.isProd ? o.length > 0 ? this.pinoLogger.info({ args: o }, e) : this.pinoLogger.info(e) : o.length > 0 ? this.pinoLogger.info({ args: o }, e) : this.pinoLogger.info(e);
|
|
59
|
+
}
|
|
60
|
+
warn(e, ...o) {
|
|
61
|
+
o.length > 0 ? this.pinoLogger.warn({ args: o }, e) : this.pinoLogger.warn(e);
|
|
62
|
+
}
|
|
63
|
+
error(e, ...o) {
|
|
64
|
+
o.length > 0 ? this.pinoLogger.error({ args: o }, e) : this.pinoLogger.error(e);
|
|
65
|
+
}
|
|
66
|
+
fatal(e, ...o) {
|
|
67
|
+
o.length > 0 ? this.pinoLogger.fatal({ args: o }, e) : this.pinoLogger.fatal(e);
|
|
68
|
+
}
|
|
69
|
+
trace(e, ...o) {
|
|
70
|
+
this.isProd || (o.length > 0 ? this.pinoLogger.trace({ args: o }, e) : this.pinoLogger.trace(e));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Get the underlying pino logger instance
|
|
74
|
+
*/
|
|
75
|
+
getPinoLogger() {
|
|
76
|
+
return this.pinoLogger;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const d = new f();
|
|
80
|
+
function L(n, e = {}) {
|
|
81
|
+
return new f({
|
|
82
|
+
...e,
|
|
83
|
+
name: e.name || `WSX:${n}`
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function a(n) {
|
|
87
|
+
return new f(n);
|
|
88
|
+
}
|
|
89
|
+
export {
|
|
90
|
+
f as WSXLogger,
|
|
91
|
+
L as createLogger,
|
|
92
|
+
a as createLoggerWithConfig,
|
|
93
|
+
d as logger,
|
|
94
|
+
y as pino
|
|
95
|
+
};
|
|
96
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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,GAAgB,GAAGE,EAAA,GAE/DK,IAAyB;AAAA,IAC3B,MAAMJ,KAAQH,EAAe;AAAA,IAC7B,OAAOI,KAASJ,EAAe;AAAA,IAC/B,GAAGM;AAAA,EAAA;AAeP,MAXIP,QAGAQ,EAAQ,UAAU;AAAA,IACd,UAAU;AAAA;AAAA,IACV,OAAO;AAAA;AAAA,IACP,IAAID,KAAA,gBAAAA,EAAa,YAAW,CAAA;AAAA;AAAA,EAAC,IAKjCD,KAAUR,OAAuB,CAACD;AAClC,QAAI;AACA,aA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AAuB;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;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wsxjs/wsx-logger",
|
|
3
|
+
"version": "0.0.18",
|
|
4
|
+
"description": "WSXJS Logger - Pino-based logging utility for WSXJS",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src",
|
|
19
|
+
"!**/__tests__",
|
|
20
|
+
"!**/test"
|
|
21
|
+
],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"pino": "^9.6.0",
|
|
24
|
+
"pino-pretty": "^13.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^20.0.0",
|
|
28
|
+
"@typescript-eslint/eslint-plugin": "^8.37.0",
|
|
29
|
+
"@typescript-eslint/parser": "^8.37.0",
|
|
30
|
+
"eslint": "^9.31.0",
|
|
31
|
+
"typescript": "^5.0.0",
|
|
32
|
+
"vite": "^5.4.19"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"wsx",
|
|
36
|
+
"logger",
|
|
37
|
+
"pino",
|
|
38
|
+
"logging",
|
|
39
|
+
"web-components"
|
|
40
|
+
],
|
|
41
|
+
"author": "WSXJS Team",
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "https://github.com/wsxjs/wsxjs.git",
|
|
46
|
+
"directory": "packages/logger"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "vite build && tsc --emitDeclarationOnly --declaration --declarationMap --outDir dist src/index.ts --moduleResolution node --esModuleInterop",
|
|
53
|
+
"build:dev": "NODE_ENV=development vite build && tsc --emitDeclarationOnly --declaration --declarationMap --outDir dist src/index.ts --moduleResolution node --esModuleInterop",
|
|
54
|
+
"dev": "NODE_ENV=development vite build --watch",
|
|
55
|
+
"clean": "rm -rf dist",
|
|
56
|
+
"typecheck": "tsc --noEmit",
|
|
57
|
+
"lint": "eslint .",
|
|
58
|
+
"lint:fix": "eslint . --fix"
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @wsxjs/wsx-logger
|
|
3
|
+
* Pino-based logging utility for WSXJS
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import pino, { type LoggerOptions } from "pino";
|
|
7
|
+
import type { Logger as PinoLoggerType } from "pino";
|
|
8
|
+
|
|
9
|
+
export type LogLevel = "debug" | "info" | "warn" | "error" | "fatal" | "trace";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Logger interface compatible with WSXJS core logger
|
|
13
|
+
*/
|
|
14
|
+
export interface Logger {
|
|
15
|
+
debug(message: string, ...args: unknown[]): void;
|
|
16
|
+
info(message: string, ...args: unknown[]): void;
|
|
17
|
+
warn(message: string, ...args: unknown[]): void;
|
|
18
|
+
error(message: string, ...args: unknown[]): void;
|
|
19
|
+
fatal?(message: string, ...args: unknown[]): void;
|
|
20
|
+
trace?(message: string, ...args: unknown[]): void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Logger configuration options
|
|
25
|
+
*/
|
|
26
|
+
export interface LoggerConfig {
|
|
27
|
+
/** Logger name/prefix */
|
|
28
|
+
name?: string;
|
|
29
|
+
/** Minimum log level */
|
|
30
|
+
level?: LogLevel;
|
|
31
|
+
/** Enable pretty printing (for development) */
|
|
32
|
+
pretty?: boolean;
|
|
33
|
+
/** Additional pino options */
|
|
34
|
+
pinoOptions?: LoggerOptions;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Check if we're in production environment
|
|
39
|
+
*/
|
|
40
|
+
function isProduction(): boolean {
|
|
41
|
+
return typeof process !== "undefined" && process.env.NODE_ENV === "production";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Check if we're in a Node.js environment
|
|
46
|
+
*/
|
|
47
|
+
function isNodeEnvironment(): boolean {
|
|
48
|
+
return typeof process !== "undefined" && process.versions?.node !== undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Check if we're in a browser environment
|
|
53
|
+
*/
|
|
54
|
+
function isBrowserEnvironment(): boolean {
|
|
55
|
+
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Default logger configuration
|
|
60
|
+
* - Production: info level, no pretty printing
|
|
61
|
+
* - Development: debug level, pretty printing enabled
|
|
62
|
+
*/
|
|
63
|
+
const DEFAULT_CONFIG: LoggerConfig = {
|
|
64
|
+
name: "WSX",
|
|
65
|
+
level: isProduction() ? "info" : "debug",
|
|
66
|
+
pretty: !isProduction(),
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Create a pino logger instance
|
|
71
|
+
*/
|
|
72
|
+
function createPinoLogger(config: LoggerConfig = {}): PinoLoggerType {
|
|
73
|
+
const { name, level, pretty, pinoOptions } = { ...DEFAULT_CONFIG, ...config };
|
|
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
|
+
}
|
|
91
|
+
|
|
92
|
+
// In development and Node.js environment, use pino-pretty for better readability
|
|
93
|
+
if (pretty && isNodeEnvironment() && !isProduction()) {
|
|
94
|
+
try {
|
|
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
|
+
}
|
|
113
|
+
|
|
114
|
+
return pino(options);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* WSX Logger wrapper that implements the Logger interface
|
|
119
|
+
* and uses pino under the hood
|
|
120
|
+
*/
|
|
121
|
+
export class WSXLogger implements Logger {
|
|
122
|
+
private pinoLogger: PinoLoggerType;
|
|
123
|
+
private isProd: boolean;
|
|
124
|
+
|
|
125
|
+
constructor(config: LoggerConfig = {}) {
|
|
126
|
+
this.isProd = isProduction();
|
|
127
|
+
this.pinoLogger = createPinoLogger(config);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
debug(message: string, ...args: unknown[]): void {
|
|
131
|
+
// Always show debug logs in non-production environments
|
|
132
|
+
if (!this.isProd) {
|
|
133
|
+
if (args.length > 0) {
|
|
134
|
+
this.pinoLogger.debug({ args }, message);
|
|
135
|
+
} else {
|
|
136
|
+
this.pinoLogger.debug(message);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
info(message: string, ...args: unknown[]): void {
|
|
142
|
+
// Always show info logs in non-production environments
|
|
143
|
+
if (!this.isProd) {
|
|
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
|
|
151
|
+
if (args.length > 0) {
|
|
152
|
+
this.pinoLogger.info({ args }, message);
|
|
153
|
+
} else {
|
|
154
|
+
this.pinoLogger.info(message);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
warn(message: string, ...args: unknown[]): void {
|
|
160
|
+
// Always show warnings (in both production and development)
|
|
161
|
+
if (args.length > 0) {
|
|
162
|
+
this.pinoLogger.warn({ args }, message);
|
|
163
|
+
} else {
|
|
164
|
+
this.pinoLogger.warn(message);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
error(message: string, ...args: unknown[]): void {
|
|
169
|
+
// Always show errors (in both production and development)
|
|
170
|
+
if (args.length > 0) {
|
|
171
|
+
this.pinoLogger.error({ args }, message);
|
|
172
|
+
} else {
|
|
173
|
+
this.pinoLogger.error(message);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
fatal(message: string, ...args: unknown[]): void {
|
|
178
|
+
if (args.length > 0) {
|
|
179
|
+
this.pinoLogger.fatal({ args }, message);
|
|
180
|
+
} else {
|
|
181
|
+
this.pinoLogger.fatal(message);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
trace(message: string, ...args: unknown[]): void {
|
|
186
|
+
// Always show trace logs in non-production environments
|
|
187
|
+
if (!this.isProd) {
|
|
188
|
+
if (args.length > 0) {
|
|
189
|
+
this.pinoLogger.trace({ args }, message);
|
|
190
|
+
} else {
|
|
191
|
+
this.pinoLogger.trace(message);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Get the underlying pino logger instance
|
|
198
|
+
*/
|
|
199
|
+
getPinoLogger(): PinoLoggerType {
|
|
200
|
+
return this.pinoLogger;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Default logger instance
|
|
206
|
+
*/
|
|
207
|
+
export const logger = new WSXLogger();
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Create a component-specific logger
|
|
211
|
+
*
|
|
212
|
+
* @param componentName - Name of the component/module
|
|
213
|
+
* @param config - Optional logger configuration
|
|
214
|
+
* @returns Logger instance
|
|
215
|
+
*/
|
|
216
|
+
export function createLogger(componentName: string, config: LoggerConfig = {}): Logger {
|
|
217
|
+
return new WSXLogger({
|
|
218
|
+
...config,
|
|
219
|
+
name: config.name || `WSX:${componentName}`,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Create a logger with custom configuration
|
|
225
|
+
*
|
|
226
|
+
* @param config - Logger configuration
|
|
227
|
+
* @returns Logger instance
|
|
228
|
+
*/
|
|
229
|
+
export function createLoggerWithConfig(config: LoggerConfig): Logger {
|
|
230
|
+
return new WSXLogger(config);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Export pino types for advanced usage
|
|
234
|
+
export type { Logger as PinoLogger, LoggerOptions } from "pino";
|
|
235
|
+
export { pino } from "pino";
|