@geekmidas/logger 0.0.1
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 +315 -0
- package/dist/console.cjs +94 -0
- package/dist/console.cjs.map +1 -0
- package/dist/console.d.cts +76 -0
- package/dist/console.d.mts +76 -0
- package/dist/console.mjs +92 -0
- package/dist/console.mjs.map +1 -0
- package/dist/index.cjs +3 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +3 -0
- package/dist/pino.cjs +48 -0
- package/dist/pino.cjs.map +1 -0
- package/dist/pino.d.cts +8 -0
- package/dist/pino.d.mts +8 -0
- package/dist/pino.mjs +25 -0
- package/dist/pino.mjs.map +1 -0
- package/dist/types-C1RfRbo6.d.mts +81 -0
- package/dist/types-DXdmn7h5.d.cts +81 -0
- package/dist/types-ag_0Cvbg.cjs +38 -0
- package/dist/types-ag_0Cvbg.cjs.map +1 -0
- package/dist/types-yQ6XOihF.mjs +32 -0
- package/dist/types-yQ6XOihF.mjs.map +1 -0
- package/dist/types.cjs +3 -0
- package/dist/types.d.cts +2 -0
- package/dist/types.d.mts +2 -0
- package/dist/types.mjs +3 -0
- package/package.json +34 -0
- package/src/__tests__/console.spec.ts +526 -0
- package/src/__tests__/pino.spec.ts +156 -0
- package/src/console.ts +93 -0
- package/src/index.ts +6 -0
- package/src/pino.ts +26 -0
- package/src/types.ts +80 -0
- package/tsdown.config.ts +3 -0
package/src/console.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { LogFn, Logger } from './types';
|
|
2
|
+
|
|
3
|
+
export class ConsoleLogger implements Logger {
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new ConsoleLogger instance.
|
|
6
|
+
*
|
|
7
|
+
* @param data - Initial context data to include in all log messages
|
|
8
|
+
*/
|
|
9
|
+
constructor(readonly data: object = {}) {}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Creates a logging function that merges context data and adds timestamps.
|
|
13
|
+
*
|
|
14
|
+
* @param logMethod - The console method to use (e.g., console.log, console.error)
|
|
15
|
+
* @returns A LogFn that handles both structured and simple logging
|
|
16
|
+
* @private
|
|
17
|
+
*/
|
|
18
|
+
private createLogFn(logMethod: (...args: any[]) => void): LogFn {
|
|
19
|
+
return <T extends object>(obj: T, msg?: string, ...args: any[]): void => {
|
|
20
|
+
// Merge the logger's context data with the provided object
|
|
21
|
+
const ts = Date.now();
|
|
22
|
+
const mergedData = { ...this.data, ...obj, ts };
|
|
23
|
+
|
|
24
|
+
if (msg) {
|
|
25
|
+
logMethod(mergedData, msg, ...args);
|
|
26
|
+
} else {
|
|
27
|
+
logMethod(mergedData, ...args);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Debug level logging function */
|
|
33
|
+
debug: LogFn = this.createLogFn(console.debug.bind(console));
|
|
34
|
+
/** Info level logging function */
|
|
35
|
+
info: LogFn = this.createLogFn(console.info.bind(console));
|
|
36
|
+
/** Warning level logging function */
|
|
37
|
+
warn: LogFn = this.createLogFn(console.warn.bind(console));
|
|
38
|
+
/** Error level logging function */
|
|
39
|
+
error: LogFn = this.createLogFn(console.error.bind(console));
|
|
40
|
+
/** Fatal level logging function (uses console.error) */
|
|
41
|
+
fatal: LogFn = this.createLogFn(console.error.bind(console));
|
|
42
|
+
/** Trace level logging function */
|
|
43
|
+
trace: LogFn = this.createLogFn(console.trace.bind(console));
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Creates a child logger with additional context data.
|
|
47
|
+
* The child logger inherits all context from the parent and adds its own.
|
|
48
|
+
*
|
|
49
|
+
* @param obj - Additional context data for the child logger
|
|
50
|
+
* @returns A new ConsoleLogger instance with merged context
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const parentLogger = new ConsoleLogger({ app: 'myApp' });
|
|
55
|
+
* const childLogger = parentLogger.child({ module: 'database' });
|
|
56
|
+
* childLogger.info({ query: 'SELECT * FROM users' }, 'Query executed');
|
|
57
|
+
* // Context includes both { app: 'myApp' } and { module: 'database' }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
child(obj: object): Logger {
|
|
61
|
+
return new ConsoleLogger({
|
|
62
|
+
...this.data,
|
|
63
|
+
...obj,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @example Basic usage
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const logger = new ConsoleLogger({ app: 'myApp' });
|
|
72
|
+
* logger.info({ action: 'start' }, 'Application starting');
|
|
73
|
+
* // Logs: { app: 'myApp', action: 'start', ts: 1234567890 } Application starting
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @example Child logger usage
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const childLogger = logger.child({ module: 'auth' });
|
|
79
|
+
* childLogger.debug({ userId: 123 }, 'User authenticated');
|
|
80
|
+
* // Logs: { app: 'myApp', module: 'auth', userId: 123, ts: 1234567891 } User authenticated
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @example Error logging with context
|
|
84
|
+
* ```typescript
|
|
85
|
+
* try {
|
|
86
|
+
* await someOperation();
|
|
87
|
+
* } catch (error) {
|
|
88
|
+
* logger.error({ error, operation: 'someOperation' }, 'Operation failed');
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
export const DEFAULT_LOGGER = new ConsoleLogger() as any;
|
package/src/index.ts
ADDED
package/src/pino.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { pino } from 'pino';
|
|
2
|
+
import type { CreateLoggerOptions } from './types';
|
|
3
|
+
|
|
4
|
+
export function createLogger(options: CreateLoggerOptions) {
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
const pretty = options?.pretty && process.NODE_ENV !== 'production';
|
|
7
|
+
const baseOptions = pretty
|
|
8
|
+
? {
|
|
9
|
+
transport: {
|
|
10
|
+
target: 'pino-pretty',
|
|
11
|
+
options: { colorize: true },
|
|
12
|
+
},
|
|
13
|
+
}
|
|
14
|
+
: {};
|
|
15
|
+
return pino({
|
|
16
|
+
...baseOptions,
|
|
17
|
+
formatters: {
|
|
18
|
+
bindings() {
|
|
19
|
+
return { nodeVersion: process.version };
|
|
20
|
+
},
|
|
21
|
+
level: (label) => {
|
|
22
|
+
return { level: label.toUpperCase() };
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logging function type that supports both structured and simple logging.
|
|
3
|
+
* Can be called with an object for structured logging or just a message string.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* // Structured logging with context object
|
|
8
|
+
* logger.info({ userId: 123, action: 'login' }, 'User logged in');
|
|
9
|
+
*
|
|
10
|
+
* // Simple string logging
|
|
11
|
+
* logger.info('Application started');
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export type LogFn = {
|
|
15
|
+
/** Structured logging with context object, optional message, and additional arguments */
|
|
16
|
+
<T extends object>(obj: T, msg?: string, ...args: any[]): void;
|
|
17
|
+
/** Simple string logging */
|
|
18
|
+
(msg: string): void;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Standard logger interface with multiple log levels and child logger support.
|
|
23
|
+
* Follows common logging patterns with structured logging capabilities.
|
|
24
|
+
*
|
|
25
|
+
* @interface Logger
|
|
26
|
+
*/
|
|
27
|
+
export interface Logger {
|
|
28
|
+
/** Debug level logging - verbose information for debugging */
|
|
29
|
+
debug: LogFn;
|
|
30
|
+
/** Info level logging - general informational messages */
|
|
31
|
+
info: LogFn;
|
|
32
|
+
/** Warning level logging - potentially harmful situations */
|
|
33
|
+
warn: LogFn;
|
|
34
|
+
/** Error level logging - error events that might still allow the application to continue */
|
|
35
|
+
error: LogFn;
|
|
36
|
+
/** Fatal level logging - severe errors that will likely cause the application to abort */
|
|
37
|
+
fatal: LogFn;
|
|
38
|
+
/** Trace level logging - most detailed information */
|
|
39
|
+
trace: LogFn;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a child logger with additional context.
|
|
42
|
+
* Child loggers inherit parent context and add their own.
|
|
43
|
+
*
|
|
44
|
+
* @param obj - Additional context to include in all child logger calls
|
|
45
|
+
* @returns A new Logger instance with merged context
|
|
46
|
+
*/
|
|
47
|
+
child: (obj: object) => Logger;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Console-based logger implementation that outputs to standard console methods.
|
|
52
|
+
* Supports structured logging with automatic timestamp injection and context inheritance.
|
|
53
|
+
*
|
|
54
|
+
* @implements {Logger}
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const logger = new ConsoleLogger({ app: 'myApp', version: '1.0.0' });
|
|
59
|
+
* logger.info({ userId: 123 }, 'User action performed');
|
|
60
|
+
* // Output: { app: 'myApp', version: '1.0.0', userId: 123, ts: 1234567890 } User action performed
|
|
61
|
+
*
|
|
62
|
+
* const childLogger = logger.child({ module: 'auth' });
|
|
63
|
+
* childLogger.debug({ action: 'validate' }, 'Validating token');
|
|
64
|
+
* // Output: { app: 'myApp', version: '1.0.0', module: 'auth', action: 'validate', ts: 1234567891 } Validating token
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export enum LogLevel {
|
|
68
|
+
Trace = 'trace',
|
|
69
|
+
Debug = 'debug',
|
|
70
|
+
Info = 'info',
|
|
71
|
+
Warn = 'warn',
|
|
72
|
+
Error = 'error',
|
|
73
|
+
Fatal = 'fatal',
|
|
74
|
+
Silent = 'silent',
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type CreateLoggerOptions = {
|
|
78
|
+
pretty?: boolean;
|
|
79
|
+
level?: LogLevel;
|
|
80
|
+
};
|
package/tsdown.config.ts
ADDED