@heavybit/logger-ts 3.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/README.md +458 -0
- package/dist/config/index.d.ts +34 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +204 -0
- package/dist/config/index.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +47 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/aopLogger.d.ts +44 -0
- package/dist/lib/aopLogger.d.ts.map +1 -0
- package/dist/lib/aopLogger.js +269 -0
- package/dist/lib/aopLogger.js.map +1 -0
- package/dist/lib/aopLoggerUtils.d.ts +54 -0
- package/dist/lib/aopLoggerUtils.d.ts.map +1 -0
- package/dist/lib/aopLoggerUtils.js +195 -0
- package/dist/lib/aopLoggerUtils.js.map +1 -0
- package/dist/lib/logger.d.ts +22 -0
- package/dist/lib/logger.d.ts.map +1 -0
- package/dist/lib/logger.js +253 -0
- package/dist/lib/logger.js.map +1 -0
- package/dist/middleware/loggingMiddleware.d.ts +27 -0
- package/dist/middleware/loggingMiddleware.d.ts.map +1 -0
- package/dist/middleware/loggingMiddleware.js +225 -0
- package/dist/middleware/loggingMiddleware.js.map +1 -0
- package/dist/types/index.d.ts +208 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/sanitize.d.ts +26 -0
- package/dist/utils/sanitize.d.ts.map +1 -0
- package/dist/utils/sanitize.js +132 -0
- package/dist/utils/sanitize.js.map +1 -0
- package/package.json +63 -0
package/README.md
ADDED
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
# @pesalink/logger-ts
|
|
2
|
+
|
|
3
|
+
A comprehensive TypeScript logging package for Express.js applications with AOP (Aspect-Oriented Programming) support, request/response logging middleware, stdout and file logging based on environment configuration.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🚀 **TypeScript First**: Full TypeScript support with comprehensive type definitions
|
|
8
|
+
- 📝 **Winston-based**: Built on the robust Winston logging library
|
|
9
|
+
- 🔄 **Dual Output**: Log to stdout (console), files, or both based on configuration
|
|
10
|
+
- 🗂️ **Log Rotation**: Automatic daily log rotation with compression
|
|
11
|
+
- 🎯 **AOP Logging**: Aspect-oriented programming support with decorators and utilities
|
|
12
|
+
- 🛡️ **Data Sanitization**: Automatic redaction of sensitive fields (passwords, tokens, etc.)
|
|
13
|
+
- 🏷️ **Class-based Log Levels**: Different log levels for controllers, services, repositories, etc.
|
|
14
|
+
- 🔧 **Environment Config**: Full configuration via environment variables
|
|
15
|
+
- 🧩 **Express Middleware**: Ready-to-use logging middleware for Express.js
|
|
16
|
+
- 📊 **Structured Logging**: JSON-formatted logs for easy parsing and analysis
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @pesalink/logger-ts
|
|
22
|
+
|
|
23
|
+
# Peer dependency
|
|
24
|
+
npm install express
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
### Basic Usage
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import express from 'express';
|
|
33
|
+
import { initLogger, getLogger, loggingMiddleware } from '@pesalink/logger-ts';
|
|
34
|
+
|
|
35
|
+
const app = express();
|
|
36
|
+
|
|
37
|
+
// Initialize logger (reads from environment variables)
|
|
38
|
+
const logger = initLogger();
|
|
39
|
+
|
|
40
|
+
// Add middleware
|
|
41
|
+
app.use(express.json());
|
|
42
|
+
app.use(loggingMiddleware);
|
|
43
|
+
|
|
44
|
+
// Use the logger
|
|
45
|
+
app.get('/api/users', async (req, res) => {
|
|
46
|
+
logger.info('Fetching users');
|
|
47
|
+
res.json({ users: [] });
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
app.listen(3000, () => {
|
|
51
|
+
logger.info('Server started on port 3000');
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Custom Configuration
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { initLogger, LoggerConfig } from '@pesalink/logger-ts';
|
|
59
|
+
|
|
60
|
+
const config: Partial<LoggerConfig> = {
|
|
61
|
+
target: 'both', // 'stdout' | 'file' | 'both'
|
|
62
|
+
defaultLevel: 'debug',
|
|
63
|
+
appName: 'my-service',
|
|
64
|
+
console: {
|
|
65
|
+
colorize: true,
|
|
66
|
+
level: 'debug',
|
|
67
|
+
},
|
|
68
|
+
file: {
|
|
69
|
+
directory: 'logs',
|
|
70
|
+
maxSize: '100m',
|
|
71
|
+
maxFiles: '30d',
|
|
72
|
+
zippedArchive: true,
|
|
73
|
+
datePattern: 'YYYY-MM-DD',
|
|
74
|
+
level: 'info',
|
|
75
|
+
},
|
|
76
|
+
classLevels: {
|
|
77
|
+
controllers: 'info',
|
|
78
|
+
services: 'debug',
|
|
79
|
+
repositories: 'error',
|
|
80
|
+
},
|
|
81
|
+
sensitiveFields: ['customSecret'], // Additional fields to redact
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const logger = initLogger(config);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Environment Variables
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
# Log target: 'stdout', 'file', or 'both'
|
|
91
|
+
LOG_TARGET=both
|
|
92
|
+
|
|
93
|
+
# Default log level
|
|
94
|
+
LOG_LEVEL=debug
|
|
95
|
+
|
|
96
|
+
# Console configuration
|
|
97
|
+
LOG_COLORIZE=true
|
|
98
|
+
|
|
99
|
+
# File configuration
|
|
100
|
+
LOG_DIR=logs
|
|
101
|
+
LOG_MAX_SIZE=50m
|
|
102
|
+
LOG_MAX_FILES=30d
|
|
103
|
+
LOG_DATE_PATTERN=YYYY-MM-DD
|
|
104
|
+
LOG_ZIPPED_ARCHIVE=true
|
|
105
|
+
|
|
106
|
+
# Class-specific log levels
|
|
107
|
+
LOG_LEVEL_CONTROLLERS=info
|
|
108
|
+
LOG_LEVEL_SERVICES=debug
|
|
109
|
+
LOG_LEVEL_REPOSITORIES=error
|
|
110
|
+
LOG_LEVEL_UTILS=warn
|
|
111
|
+
LOG_LEVEL_MIDDLEWARE=info
|
|
112
|
+
LOG_LEVEL_ROUTES=info
|
|
113
|
+
|
|
114
|
+
# Application info
|
|
115
|
+
APP_NAME=my-app
|
|
116
|
+
NODE_ENV=development
|
|
117
|
+
HEALTHCHECK_ENDPOINT=/health
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## API Reference
|
|
121
|
+
|
|
122
|
+
### Logger Functions
|
|
123
|
+
|
|
124
|
+
#### `initLogger(config?: Partial<LoggerConfig>): ExtendedLogger`
|
|
125
|
+
|
|
126
|
+
Initialize the logger singleton. Call once at application startup.
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
const logger = initLogger({
|
|
130
|
+
target: 'both',
|
|
131
|
+
defaultLevel: 'info',
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
#### `getLogger(): ExtendedLogger`
|
|
136
|
+
|
|
137
|
+
Get the logger instance (creates default if not initialized).
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
const logger = getLogger();
|
|
141
|
+
logger.info('Hello world');
|
|
142
|
+
logger.error('An error occurred', { error: err.message });
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### `createLogger(config?: Partial<LoggerConfig>): ExtendedLogger`
|
|
146
|
+
|
|
147
|
+
Create a new logger instance with custom config (does not replace singleton).
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
const customLogger = createLogger({
|
|
151
|
+
target: 'file',
|
|
152
|
+
file: { directory: 'custom-logs' },
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Logging Methods
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
const logger = getLogger();
|
|
160
|
+
|
|
161
|
+
// Standard Winston methods
|
|
162
|
+
logger.error('Error message', { metadata: 'value' });
|
|
163
|
+
logger.warn('Warning message');
|
|
164
|
+
logger.info('Info message');
|
|
165
|
+
logger.debug('Debug message');
|
|
166
|
+
logger.verbose('Verbose message');
|
|
167
|
+
|
|
168
|
+
// Extended methods
|
|
169
|
+
logger.logWithMeta('info', 'Message', { custom: 'data' });
|
|
170
|
+
logger.logRequest({
|
|
171
|
+
message: 'Request completed',
|
|
172
|
+
method: 'GET',
|
|
173
|
+
url: '/api/users',
|
|
174
|
+
statusCode: 200,
|
|
175
|
+
responseTime: '50ms',
|
|
176
|
+
});
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Middleware
|
|
180
|
+
|
|
181
|
+
#### `loggingMiddleware`
|
|
182
|
+
|
|
183
|
+
Default logging middleware with standard configuration.
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
app.use(loggingMiddleware);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
#### `createLoggingMiddleware(options: MiddlewareOptions)`
|
|
190
|
+
|
|
191
|
+
Create custom logging middleware.
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
app.use(createLoggingMiddleware({
|
|
195
|
+
skipEndpoints: ['/health', '/metrics'],
|
|
196
|
+
logRequestBody: true,
|
|
197
|
+
logResponseBody: false,
|
|
198
|
+
requestIdGenerator: () => uuid(),
|
|
199
|
+
additionalFields: (req) => ({
|
|
200
|
+
userId: req.user?.id,
|
|
201
|
+
}),
|
|
202
|
+
}));
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
#### `errorLoggingMiddleware`
|
|
206
|
+
|
|
207
|
+
Error logging middleware for Express error handling.
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
app.use(errorLoggingMiddleware);
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
#### `morganStyleMiddleware(format)`
|
|
214
|
+
|
|
215
|
+
Morgan-style request logging.
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
app.use(morganStyleMiddleware('dev')); // 'combined' | 'common' | 'dev' | 'short' | 'tiny'
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### AOP (Aspect-Oriented Programming)
|
|
222
|
+
|
|
223
|
+
#### Using Decorators
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
import { LogClass, LogMethod } from '@pesalink/logger-ts';
|
|
227
|
+
|
|
228
|
+
@LogClass(['constructor'], ['getAll']) // Exclude constructor, only log errors for getAll
|
|
229
|
+
class UserController {
|
|
230
|
+
async getAll(req, res) {
|
|
231
|
+
// Only errors will be logged
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
@LogMethod('UserController', 'create', 'around', 'controllers')
|
|
235
|
+
async create(req, res) {
|
|
236
|
+
// Full before/after logging
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
#### Using `logMethodsWithAOP`
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
import { logMethodsWithAOP } from '@pesalink/logger-ts';
|
|
245
|
+
|
|
246
|
+
class ProductService {
|
|
247
|
+
async getAll(req, res) { /* ... */ }
|
|
248
|
+
async create(req, res) { /* ... */ }
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
logMethodsWithAOP(
|
|
252
|
+
ProductService.prototype,
|
|
253
|
+
['constructor'], // Excluded methods
|
|
254
|
+
['getAll'], // Only log errors for these
|
|
255
|
+
'ProductService', // Class name
|
|
256
|
+
'services', // Class type
|
|
257
|
+
'around' // Advice type
|
|
258
|
+
);
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
#### Using `withLogging` Wrapper
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
import { withLogging } from '@pesalink/logger-ts';
|
|
265
|
+
|
|
266
|
+
const handler = async (req, res) => {
|
|
267
|
+
res.json({ data: [] });
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
app.get('/api/items', withLogging(handler, 'getItems', 'ItemController'));
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
#### Using `wrapRoutesWithLogging`
|
|
274
|
+
|
|
275
|
+
```typescript
|
|
276
|
+
import { Router } from 'express';
|
|
277
|
+
import { wrapRoutesWithLogging } from '@pesalink/logger-ts';
|
|
278
|
+
|
|
279
|
+
const router = Router();
|
|
280
|
+
|
|
281
|
+
router.get('/', handler1);
|
|
282
|
+
router.post('/', handler2);
|
|
283
|
+
|
|
284
|
+
// Wrap all routes in the router
|
|
285
|
+
wrapRoutesWithLogging(router, 'ApiRouter', 'routes', ['GET /']);
|
|
286
|
+
|
|
287
|
+
app.use('/api', router);
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### Sanitization
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
import { sanitizeData, sanitizeHeaders, prepareForLogging } from '@pesalink/logger-ts';
|
|
294
|
+
|
|
295
|
+
// Sanitize object (redacts sensitive fields)
|
|
296
|
+
const safe = sanitizeData({
|
|
297
|
+
username: 'john',
|
|
298
|
+
password: 'secret123', // -> [REDACTED]
|
|
299
|
+
token: 'abc123', // -> [REDACTED]
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
// Sanitize headers
|
|
303
|
+
const safeHeaders = sanitizeHeaders(req.headers);
|
|
304
|
+
|
|
305
|
+
// Prepare for logging with truncation
|
|
306
|
+
const truncated = prepareForLogging(largeObject, {
|
|
307
|
+
truncateLength: 1000,
|
|
308
|
+
});
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### Configuration Functions
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
import {
|
|
315
|
+
initConfig,
|
|
316
|
+
getConfig,
|
|
317
|
+
resetConfig,
|
|
318
|
+
getLogLevelForClass,
|
|
319
|
+
} from '@pesalink/logger-ts';
|
|
320
|
+
|
|
321
|
+
// Initialize configuration
|
|
322
|
+
initConfig({ target: 'both' });
|
|
323
|
+
|
|
324
|
+
// Get current config
|
|
325
|
+
const config = getConfig();
|
|
326
|
+
|
|
327
|
+
// Get log level for a class type
|
|
328
|
+
const level = getLogLevelForClass(config, 'controllers'); // 'info'
|
|
329
|
+
|
|
330
|
+
// Reset configuration (useful for testing)
|
|
331
|
+
resetConfig();
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
## Log Output Format
|
|
335
|
+
|
|
336
|
+
### Console (Structured)
|
|
337
|
+
|
|
338
|
+
```
|
|
339
|
+
2024-01-15T10:30:45.123+0000 | app=my-service | className=UserController | OperationName=getUser | Method=GET | Endpoint=/api/users/1 | LogLevel=info | Payload={} | Message=Request completed | RequestId=abc-123 | ResponseTime=45ms | StatusCode=200
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### File (JSON)
|
|
343
|
+
|
|
344
|
+
```json
|
|
345
|
+
{
|
|
346
|
+
"timestamp": "2024-01-15T10:30:45.123+0000",
|
|
347
|
+
"level": "info",
|
|
348
|
+
"app": "my-service",
|
|
349
|
+
"environment": "production",
|
|
350
|
+
"className": "UserController",
|
|
351
|
+
"methodName": "getUser",
|
|
352
|
+
"method": "GET",
|
|
353
|
+
"url": "/api/users/1",
|
|
354
|
+
"requestId": "abc-123",
|
|
355
|
+
"statusCode": 200,
|
|
356
|
+
"responseTime": "45ms",
|
|
357
|
+
"message": "Request completed"
|
|
358
|
+
}
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
## Log Files
|
|
362
|
+
|
|
363
|
+
When file logging is enabled, logs are stored in the configured directory with the following structure:
|
|
364
|
+
|
|
365
|
+
```
|
|
366
|
+
logs/
|
|
367
|
+
├── 2024-01-15.log # All logs for the day
|
|
368
|
+
├── 2024-01-15-error.log # Error-only logs
|
|
369
|
+
├── 2024-01-14.log.gz # Compressed archived logs
|
|
370
|
+
└── 2024-01-14-error.log.gz
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
## Best Practices
|
|
374
|
+
|
|
375
|
+
### 1. Initialize Early
|
|
376
|
+
|
|
377
|
+
```typescript
|
|
378
|
+
// At the top of your entry file
|
|
379
|
+
import { initLogger } from '@pesalink/logger-ts';
|
|
380
|
+
const logger = initLogger();
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
### 2. Use Appropriate Log Levels
|
|
384
|
+
|
|
385
|
+
```typescript
|
|
386
|
+
logger.error('Database connection failed', { error: err.message }); // Errors
|
|
387
|
+
logger.warn('Rate limit approaching', { current: 90, max: 100 }); // Warnings
|
|
388
|
+
logger.info('User logged in', { userId: '123' }); // Info
|
|
389
|
+
logger.debug('Cache hit', { key: 'user:123' }); // Debug
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
### 3. Include Context
|
|
393
|
+
|
|
394
|
+
```typescript
|
|
395
|
+
logger.info('Processing payment', {
|
|
396
|
+
transactionId: 'txn-123',
|
|
397
|
+
userId: 'user-456',
|
|
398
|
+
amount: 100.00,
|
|
399
|
+
currency: 'USD',
|
|
400
|
+
});
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
### 4. Handle Errors Properly
|
|
404
|
+
|
|
405
|
+
```typescript
|
|
406
|
+
try {
|
|
407
|
+
await riskyOperation();
|
|
408
|
+
} catch (error) {
|
|
409
|
+
logger.error('Operation failed', {
|
|
410
|
+
error: error.message,
|
|
411
|
+
stack: error.stack,
|
|
412
|
+
context: { operationId: '123' },
|
|
413
|
+
});
|
|
414
|
+
throw error;
|
|
415
|
+
}
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
### 5. Use Class-Specific Levels
|
|
419
|
+
|
|
420
|
+
Configure verbose logging for debugging specific layers:
|
|
421
|
+
|
|
422
|
+
```bash
|
|
423
|
+
LOG_LEVEL_SERVICES=debug
|
|
424
|
+
LOG_LEVEL_REPOSITORIES=error # Only log database errors
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
## Examples
|
|
428
|
+
|
|
429
|
+
See the `examples/` directory for complete examples:
|
|
430
|
+
|
|
431
|
+
- [basic-usage.ts](./examples/basic-usage.ts) - Basic Express setup
|
|
432
|
+
- [aop-usage.ts](./examples/aop-usage.ts) - AOP decorators and utilities
|
|
433
|
+
- [external-service.ts](./examples/external-service.ts) - Microservice integration
|
|
434
|
+
|
|
435
|
+
## TypeScript Support
|
|
436
|
+
|
|
437
|
+
All types are exported from the package:
|
|
438
|
+
|
|
439
|
+
```typescript
|
|
440
|
+
import type {
|
|
441
|
+
LoggerConfig,
|
|
442
|
+
LogLevel,
|
|
443
|
+
ClassType,
|
|
444
|
+
LogTarget,
|
|
445
|
+
ExtendedLogger,
|
|
446
|
+
RequestLogEntry,
|
|
447
|
+
AOPContext,
|
|
448
|
+
MiddlewareOptions,
|
|
449
|
+
} from '@pesalink/logger-ts';
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
## License
|
|
453
|
+
|
|
454
|
+
MIT
|
|
455
|
+
|
|
456
|
+
## Author
|
|
457
|
+
|
|
458
|
+
Nicholas Kute
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { LoggerConfig, LogLevel, ClassType } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Default sensitive fields to redact from logs
|
|
4
|
+
*/
|
|
5
|
+
export declare const DEFAULT_SENSITIVE_FIELDS: string[];
|
|
6
|
+
/**
|
|
7
|
+
* Default log levels per class type
|
|
8
|
+
*/
|
|
9
|
+
export declare const DEFAULT_CLASS_LEVELS: Record<ClassType, LogLevel>;
|
|
10
|
+
/**
|
|
11
|
+
* Create default logger configuration from environment
|
|
12
|
+
*/
|
|
13
|
+
export declare const createDefaultConfig: () => LoggerConfig;
|
|
14
|
+
/**
|
|
15
|
+
* Merge user config with defaults
|
|
16
|
+
*/
|
|
17
|
+
export declare const mergeConfig: (userConfig?: Partial<LoggerConfig>) => LoggerConfig;
|
|
18
|
+
/**
|
|
19
|
+
* Get the log level for a specific class type
|
|
20
|
+
*/
|
|
21
|
+
export declare const getLogLevelForClass: (config: LoggerConfig, classType: ClassType) => LogLevel;
|
|
22
|
+
/**
|
|
23
|
+
* Initialize configuration (call once at app startup)
|
|
24
|
+
*/
|
|
25
|
+
export declare const initConfig: (userConfig?: Partial<LoggerConfig>) => LoggerConfig;
|
|
26
|
+
/**
|
|
27
|
+
* Get current configuration
|
|
28
|
+
*/
|
|
29
|
+
export declare const getConfig: () => LoggerConfig;
|
|
30
|
+
/**
|
|
31
|
+
* Reset configuration (useful for testing)
|
|
32
|
+
*/
|
|
33
|
+
export declare const resetConfig: () => void;
|
|
34
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/config/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAa,SAAS,EAAE,MAAM,UAAU,CAAC;AAExE;;GAEG;AACH,eAAO,MAAM,wBAAwB,EAAE,MAAM,EA2B5C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,EAAE,MAAM,CAAC,SAAS,EAAE,QAAQ,CAgB5D,CAAC;AA8DF;;GAEG;AACH,eAAO,MAAM,mBAAmB,QAAO,YA2BtC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAI,aAAY,OAAO,CAAC,YAAY,CAAM,KAAG,YAuBpE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAC9B,QAAQ,YAAY,EACpB,WAAW,SAAS,KACnB,QAEF,CAAC;AAOF;;GAEG;AACH,eAAO,MAAM,UAAU,GAAI,aAAa,OAAO,CAAC,YAAY,CAAC,KAAG,YAG/D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,SAAS,QAAO,YAK5B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,QAAO,IAE9B,CAAC"}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resetConfig = exports.getConfig = exports.initConfig = exports.getLogLevelForClass = exports.mergeConfig = exports.createDefaultConfig = exports.DEFAULT_CLASS_LEVELS = exports.DEFAULT_SENSITIVE_FIELDS = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Default sensitive fields to redact from logs
|
|
6
|
+
*/
|
|
7
|
+
exports.DEFAULT_SENSITIVE_FIELDS = [
|
|
8
|
+
'password',
|
|
9
|
+
'newPassword',
|
|
10
|
+
'new_password',
|
|
11
|
+
'repeatPassword',
|
|
12
|
+
'repeat_password',
|
|
13
|
+
'oldPassword',
|
|
14
|
+
'old_password',
|
|
15
|
+
'secret',
|
|
16
|
+
'token',
|
|
17
|
+
'apikey',
|
|
18
|
+
'apiKey',
|
|
19
|
+
'api_key',
|
|
20
|
+
'accesstoken',
|
|
21
|
+
'accessToken',
|
|
22
|
+
'access_token',
|
|
23
|
+
'refreshToken',
|
|
24
|
+
'refresh_token',
|
|
25
|
+
'authorization',
|
|
26
|
+
'phoneNumber',
|
|
27
|
+
'phone_number',
|
|
28
|
+
'ssn',
|
|
29
|
+
'socialSecurityNumber',
|
|
30
|
+
'creditCard',
|
|
31
|
+
'credit_card',
|
|
32
|
+
'cvv',
|
|
33
|
+
'pin',
|
|
34
|
+
];
|
|
35
|
+
/**
|
|
36
|
+
* Default log levels per class type
|
|
37
|
+
*/
|
|
38
|
+
exports.DEFAULT_CLASS_LEVELS = {
|
|
39
|
+
controllers: 'info',
|
|
40
|
+
services: 'error',
|
|
41
|
+
repositories: 'debug',
|
|
42
|
+
utils: 'warn',
|
|
43
|
+
middleware: 'info',
|
|
44
|
+
routes: 'info',
|
|
45
|
+
configuration: 'debug',
|
|
46
|
+
validators: 'info',
|
|
47
|
+
eventListeners: 'debug',
|
|
48
|
+
helpers: 'debug',
|
|
49
|
+
jobs: 'debug',
|
|
50
|
+
models: 'info',
|
|
51
|
+
transformers: 'debug',
|
|
52
|
+
default: 'info',
|
|
53
|
+
unknown: 'info',
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Parse boolean environment variable
|
|
57
|
+
*/
|
|
58
|
+
const parseBoolean = (value, defaultValue) => {
|
|
59
|
+
if (value === undefined)
|
|
60
|
+
return defaultValue;
|
|
61
|
+
return value.toLowerCase() === 'true';
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Get log target from environment
|
|
65
|
+
*/
|
|
66
|
+
const getLogTarget = () => {
|
|
67
|
+
const target = process.env.LOG_TARGET;
|
|
68
|
+
if (target && ['stdout', 'file', 'both'].includes(target)) {
|
|
69
|
+
return target;
|
|
70
|
+
}
|
|
71
|
+
// Legacy environment variable support
|
|
72
|
+
const logToFile = parseBoolean(process.env.LOG_TO_FILE, false);
|
|
73
|
+
const logToConsole = parseBoolean(process.env.LOG_TO_CONSOLE, true);
|
|
74
|
+
if (logToFile && logToConsole)
|
|
75
|
+
return 'both';
|
|
76
|
+
if (logToFile)
|
|
77
|
+
return 'file';
|
|
78
|
+
return 'stdout';
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Get class-specific log levels from environment
|
|
82
|
+
*/
|
|
83
|
+
const getClassLevelsFromEnv = () => {
|
|
84
|
+
const levels = {};
|
|
85
|
+
const envMappings = {
|
|
86
|
+
controllers: 'LOG_LEVEL_CONTROLLERS',
|
|
87
|
+
services: 'LOG_LEVEL_SERVICES',
|
|
88
|
+
repositories: 'LOG_LEVEL_REPOSITORIES',
|
|
89
|
+
utils: 'LOG_LEVEL_UTILS',
|
|
90
|
+
middleware: 'LOG_LEVEL_MIDDLEWARE',
|
|
91
|
+
routes: 'LOG_LEVEL_ROUTES',
|
|
92
|
+
configuration: 'LOG_LEVEL_CONFIGURATION',
|
|
93
|
+
validators: 'LOG_LEVEL_VALIDATORS',
|
|
94
|
+
eventListeners: 'LOG_LEVEL_EVENT_LISTENERS',
|
|
95
|
+
helpers: 'LOG_LEVEL_HELPERS',
|
|
96
|
+
jobs: 'LOG_LEVEL_JOBS',
|
|
97
|
+
models: 'LOG_LEVEL_MODELS',
|
|
98
|
+
transformers: 'LOG_LEVEL_TRANSFORMERS',
|
|
99
|
+
default: 'LOG_LEVEL_DEFAULT',
|
|
100
|
+
unknown: 'LOG_LEVEL_DEFAULT',
|
|
101
|
+
};
|
|
102
|
+
for (const [classType, envVar] of Object.entries(envMappings)) {
|
|
103
|
+
const level = process.env[envVar];
|
|
104
|
+
if (level) {
|
|
105
|
+
levels[classType] = level;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return levels;
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Create default logger configuration from environment
|
|
112
|
+
*/
|
|
113
|
+
const createDefaultConfig = () => {
|
|
114
|
+
const isDevelopment = process.env.NODE_ENV !== 'production';
|
|
115
|
+
return {
|
|
116
|
+
target: getLogTarget(),
|
|
117
|
+
defaultLevel: process.env.LOG_LEVEL || (isDevelopment ? 'debug' : 'info'),
|
|
118
|
+
console: {
|
|
119
|
+
colorize: parseBoolean(process.env.LOG_COLORIZE, isDevelopment),
|
|
120
|
+
level: process.env.LOG_LEVEL || 'debug',
|
|
121
|
+
},
|
|
122
|
+
file: {
|
|
123
|
+
directory: process.env.LOG_DIR || 'logs',
|
|
124
|
+
maxSize: process.env.LOG_MAX_SIZE || '50m',
|
|
125
|
+
maxFiles: process.env.LOG_MAX_FILES || '30d',
|
|
126
|
+
zippedArchive: parseBoolean(process.env.LOG_ZIPPED_ARCHIVE, true),
|
|
127
|
+
datePattern: process.env.LOG_DATE_PATTERN || 'YYYY-MM-DD',
|
|
128
|
+
level: process.env.LOG_LEVEL || 'info',
|
|
129
|
+
},
|
|
130
|
+
classLevels: {
|
|
131
|
+
...exports.DEFAULT_CLASS_LEVELS,
|
|
132
|
+
...getClassLevelsFromEnv(),
|
|
133
|
+
},
|
|
134
|
+
appName: process.env.APP_NAME || process.env.npm_package_name,
|
|
135
|
+
environment: process.env.NODE_ENV || 'development',
|
|
136
|
+
sensitiveFields: exports.DEFAULT_SENSITIVE_FIELDS,
|
|
137
|
+
healthCheckEndpoint: process.env.HEALTHCHECK_ENDPOINT || '/health',
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
exports.createDefaultConfig = createDefaultConfig;
|
|
141
|
+
/**
|
|
142
|
+
* Merge user config with defaults
|
|
143
|
+
*/
|
|
144
|
+
const mergeConfig = (userConfig = {}) => {
|
|
145
|
+
const defaultConfig = (0, exports.createDefaultConfig)();
|
|
146
|
+
return {
|
|
147
|
+
...defaultConfig,
|
|
148
|
+
...userConfig,
|
|
149
|
+
console: {
|
|
150
|
+
...defaultConfig.console,
|
|
151
|
+
...userConfig.console,
|
|
152
|
+
},
|
|
153
|
+
file: {
|
|
154
|
+
...defaultConfig.file,
|
|
155
|
+
...userConfig.file,
|
|
156
|
+
},
|
|
157
|
+
classLevels: {
|
|
158
|
+
...defaultConfig.classLevels,
|
|
159
|
+
...userConfig.classLevels,
|
|
160
|
+
},
|
|
161
|
+
sensitiveFields: [
|
|
162
|
+
...(defaultConfig.sensitiveFields || []),
|
|
163
|
+
...(userConfig.sensitiveFields || []),
|
|
164
|
+
],
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
exports.mergeConfig = mergeConfig;
|
|
168
|
+
/**
|
|
169
|
+
* Get the log level for a specific class type
|
|
170
|
+
*/
|
|
171
|
+
const getLogLevelForClass = (config, classType) => {
|
|
172
|
+
return config.classLevels[classType] || config.classLevels.default || config.defaultLevel;
|
|
173
|
+
};
|
|
174
|
+
exports.getLogLevelForClass = getLogLevelForClass;
|
|
175
|
+
/**
|
|
176
|
+
* Configuration singleton
|
|
177
|
+
*/
|
|
178
|
+
let currentConfig = null;
|
|
179
|
+
/**
|
|
180
|
+
* Initialize configuration (call once at app startup)
|
|
181
|
+
*/
|
|
182
|
+
const initConfig = (userConfig) => {
|
|
183
|
+
currentConfig = (0, exports.mergeConfig)(userConfig);
|
|
184
|
+
return currentConfig;
|
|
185
|
+
};
|
|
186
|
+
exports.initConfig = initConfig;
|
|
187
|
+
/**
|
|
188
|
+
* Get current configuration
|
|
189
|
+
*/
|
|
190
|
+
const getConfig = () => {
|
|
191
|
+
if (!currentConfig) {
|
|
192
|
+
currentConfig = (0, exports.createDefaultConfig)();
|
|
193
|
+
}
|
|
194
|
+
return currentConfig;
|
|
195
|
+
};
|
|
196
|
+
exports.getConfig = getConfig;
|
|
197
|
+
/**
|
|
198
|
+
* Reset configuration (useful for testing)
|
|
199
|
+
*/
|
|
200
|
+
const resetConfig = () => {
|
|
201
|
+
currentConfig = null;
|
|
202
|
+
};
|
|
203
|
+
exports.resetConfig = resetConfig;
|
|
204
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/config/index.ts"],"names":[],"mappings":";;;AAEA;;GAEG;AACU,QAAA,wBAAwB,GAAa;IAChD,UAAU;IACV,aAAa;IACb,cAAc;IACd,gBAAgB;IAChB,iBAAiB;IACjB,aAAa;IACb,cAAc;IACd,QAAQ;IACR,OAAO;IACP,QAAQ;IACR,QAAQ;IACR,SAAS;IACT,aAAa;IACb,aAAa;IACb,cAAc;IACd,cAAc;IACd,eAAe;IACf,eAAe;IACf,aAAa;IACb,cAAc;IACd,KAAK;IACL,sBAAsB;IACtB,YAAY;IACZ,aAAa;IACb,KAAK;IACL,KAAK;CACN,CAAC;AAEF;;GAEG;AACU,QAAA,oBAAoB,GAAgC;IAC/D,WAAW,EAAE,MAAM;IACnB,QAAQ,EAAE,OAAO;IACjB,YAAY,EAAE,OAAO;IACrB,KAAK,EAAE,MAAM;IACb,UAAU,EAAE,MAAM;IAClB,MAAM,EAAE,MAAM;IACd,aAAa,EAAE,OAAO;IACtB,UAAU,EAAE,MAAM;IAClB,cAAc,EAAE,OAAO;IACvB,OAAO,EAAE,OAAO;IAChB,IAAI,EAAE,OAAO;IACb,MAAM,EAAE,MAAM;IACd,YAAY,EAAE,OAAO;IACrB,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,MAAM;CAChB,CAAC;AAEF;;GAEG;AACH,MAAM,YAAY,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