@sprqvntrs/logger 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts ADDED
@@ -0,0 +1,198 @@
1
+ import type { Logger as PinoLogger, Level } from 'pino';
2
+
3
+ /**
4
+ * Log levels supported by the logger
5
+ */
6
+ export type LogLevel = Level;
7
+
8
+ /**
9
+ * Context object passed to logging methods
10
+ * Supports any additional properties for structured logging
11
+ */
12
+ export interface LogContext {
13
+ [key: string]: unknown;
14
+ /** Optional error object or message */
15
+ error?: Error | string;
16
+ /** Optional operation/request duration in milliseconds */
17
+ duration?: number;
18
+ /** Optional request ID for tracing */
19
+ requestId?: string;
20
+ }
21
+
22
+ /**
23
+ * Options for creating a new logger instance
24
+ */
25
+ export interface CreateLoggerOptions {
26
+ /** Service name to include in all logs */
27
+ serviceName: string;
28
+ /** Log level (defaults to 'info' or LOG_LEVEL env var) */
29
+ level?: LogLevel;
30
+ /** Service version (defaults to npm_package_version env var) */
31
+ version?: string;
32
+ /** Enable pretty printing — requires `pino-pretty` to be installed by the consumer (default: false) */
33
+ pretty?: boolean;
34
+ /** Additional paths to redact from logs */
35
+ redactPaths?: string[];
36
+ /** Additional base fields to include in all logs */
37
+ base?: Record<string, unknown>;
38
+ /** Enable timestamp in logs (default: true) */
39
+ timestamp?: boolean;
40
+ }
41
+
42
+ /**
43
+ * Logger interface that wraps Pino with a simpler API
44
+ */
45
+ export interface Logger {
46
+ /** Log at trace level */
47
+ trace(message: string, context?: LogContext): void;
48
+ /** Log at debug level */
49
+ debug(message: string, context?: LogContext): void;
50
+ /** Log at info level */
51
+ info(message: string, context?: LogContext): void;
52
+ /** Log at warn level */
53
+ warn(message: string, context?: LogContext): void;
54
+ /** Log at error level */
55
+ error(message: string, context?: LogContext): void;
56
+ /** Log at fatal level */
57
+ fatal(message: string, context?: LogContext): void;
58
+
59
+ /**
60
+ * Create a child logger with additional bound context
61
+ * All logs from the child will include the bound context
62
+ */
63
+ child(bindings: Record<string, unknown>): Logger;
64
+
65
+ /** Access the underlying Pino logger instance for advanced use */
66
+ readonly pino: PinoLogger;
67
+ }
68
+
69
+ /**
70
+ * Options for configuring the static Logger class
71
+ */
72
+ export interface LoggerConfigureOptions {
73
+ /** Service name for the static logger */
74
+ serviceName: string;
75
+ /** Log level */
76
+ level?: LogLevel;
77
+ /** Enable pretty printing */
78
+ pretty?: boolean;
79
+ /** Additional base fields */
80
+ base?: Record<string, unknown>;
81
+ }
82
+
83
+ /**
84
+ * Options for HTTP logging middleware
85
+ */
86
+ export interface HttpLoggerOptions {
87
+ /** Logger instance to use */
88
+ logger?: Logger;
89
+ /** Paths to exclude from logging (e.g., ['/health', '/metrics']) */
90
+ excludePaths?: string[];
91
+ /** File extensions to exclude (e.g., ['.js', '.css', '.png']) */
92
+ excludeExtensions?: string[];
93
+ /** Whether to log request body (default: false for privacy) */
94
+ logRequestBody?: boolean;
95
+ /** Whether to log response body (default: false for privacy) */
96
+ logResponseBody?: boolean;
97
+ /** Custom properties to add to each log */
98
+ customProps?: (req: unknown, res: unknown) => Record<string, unknown>;
99
+ /** Custom log level function based on status code */
100
+ customLogLevel?: (req: unknown, res: unknown, err?: Error) => LogLevel;
101
+ }
102
+
103
+ /**
104
+ * Log entry structure used by mock logger
105
+ */
106
+ export interface LogEntry {
107
+ level: LogLevel;
108
+ message: string;
109
+ context?: LogContext;
110
+ timestamp: Date;
111
+ }
112
+
113
+ /**
114
+ * Mock logger interface for testing
115
+ */
116
+ export interface MockLogger extends Logger {
117
+ /** Get all captured log entries */
118
+ getLogs(): LogEntry[];
119
+ /** Clear all captured logs */
120
+ clear(): void;
121
+ /** Check if any log matches the predicate */
122
+ hasLog(predicate: (log: LogEntry) => boolean): boolean;
123
+ /** Get logs filtered by level */
124
+ getLogsByLevel(level: LogLevel): LogEntry[];
125
+ /** Get logs filtered by message pattern */
126
+ getLogsByMessage(pattern: string | RegExp): LogEntry[];
127
+ }
128
+
129
+ /**
130
+ * Request context stored in AsyncLocalStorage
131
+ */
132
+ export interface RequestContext {
133
+ requestId: string;
134
+ [key: string]: unknown;
135
+ }
136
+
137
+ /**
138
+ * Server logger instance with lifecycle methods
139
+ */
140
+ export interface ServerLogger {
141
+ /** The underlying logger instance */
142
+ logger: Logger;
143
+ /** Log server start event */
144
+ logServerStart(port: number, metadata?: Record<string, unknown>): void;
145
+ /** Log shutdown signal received */
146
+ logShutdown(signal: string): void;
147
+ /** Log server closed */
148
+ logServerClosed(): void;
149
+ /** Log uncaught exception */
150
+ logUncaughtException(error: Error): void;
151
+ /** Log unhandled rejection */
152
+ logUnhandledRejection(reason: unknown): void;
153
+ }
154
+
155
+ /**
156
+ * Serialized log entry with timestamp as ISO string
157
+ * Used for JSON output and external storage
158
+ */
159
+ export interface SerializedLogEntry {
160
+ level: LogLevel;
161
+ message: string;
162
+ context?: LogContext;
163
+ timestamp: string;
164
+ }
165
+
166
+ /**
167
+ * Destination for flushing buffered log entries
168
+ */
169
+ export interface FlushDestination<T> {
170
+ flush(entries: LogEntry[]): Promise<T>;
171
+ }
172
+
173
+ /**
174
+ * Options for creating a buffered logger
175
+ */
176
+ export interface BufferedLoggerOptions {
177
+ /** Base logger to wrap (logs go here AND to buffer) */
178
+ logger: Logger;
179
+ /** Maximum buffer size before auto-flush (optional) */
180
+ maxBufferSize?: number;
181
+ /** Auto-flush callback when buffer is full (optional) */
182
+ onBufferFull?: (entries: LogEntry[]) => Promise<void>;
183
+ }
184
+
185
+ /**
186
+ * Logger that buffers entries while also logging normally
187
+ * Useful for capturing logs during a request/operation for later analysis
188
+ */
189
+ export interface BufferedLogger extends Logger {
190
+ /** Get copy of buffered entries */
191
+ getBuffer(): LogEntry[];
192
+ /** Clear the buffer */
193
+ clearBuffer(): void;
194
+ /** Flush buffer to a destination and clear */
195
+ flush<T>(destination: FlushDestination<T>): Promise<T>;
196
+ /** Get current buffer size */
197
+ readonly bufferSize: number;
198
+ }