logixia 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/README.md +503 -0
- package/dist/index.d.mts +575 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +575 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1151 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1115 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +97 -0
package/README.md
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
# Logixia ๐
|
|
2
|
+
|
|
3
|
+
**A next-generation TypeScript logging library with advanced features, custom levels, and intelligent IntelliSense support**
|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/js/logixia)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
[](https://nodejs.org/)
|
|
9
|
+
|
|
10
|
+
## โจ Features
|
|
11
|
+
|
|
12
|
+
- ๐ฏ **TypeScript-First**: Full type safety with intelligent IntelliSense
|
|
13
|
+
- ๐ง **Custom Log Levels**: Define your own levels with custom colors and priorities
|
|
14
|
+
- ๐ **Trace ID Support**: Built-in request tracing with async context tracking
|
|
15
|
+
- โก **Performance Monitoring**: Built-in timing utilities and performance metrics
|
|
16
|
+
- ๐จ **Flexible Formatting**: Multiple output formats (console, JSON, custom)
|
|
17
|
+
- ๐๏ธ **NestJS Integration**: First-class support for NestJS applications
|
|
18
|
+
- ๐ **Express Middleware**: Ready-to-use Express middleware for request tracking
|
|
19
|
+
- ๐ถ **Child Loggers**: Create contextual child loggers for better organization
|
|
20
|
+
- ๐๏ธ **Field Configuration**: Enable/disable and customize log fields
|
|
21
|
+
- ๐ **Async Support**: Full async/await support with proper error handling
|
|
22
|
+
- ๐ **Structured Logging**: Rich metadata support for better log analysis
|
|
23
|
+
|
|
24
|
+
## ๐ฆ Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
# Using npm
|
|
28
|
+
npm install logixia
|
|
29
|
+
|
|
30
|
+
# Using yarn
|
|
31
|
+
yarn add logixia
|
|
32
|
+
|
|
33
|
+
# Using pnpm
|
|
34
|
+
pnpm add logixia
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## ๐ Quick Start
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { createLogger, LogLevel } from 'logixia';
|
|
41
|
+
|
|
42
|
+
// Create a logger instance
|
|
43
|
+
const logger = createLogger({
|
|
44
|
+
appName: 'MyApp',
|
|
45
|
+
environment: 'development',
|
|
46
|
+
levelOptions: {
|
|
47
|
+
level: LogLevel.INFO,
|
|
48
|
+
colors: {
|
|
49
|
+
error: 'red',
|
|
50
|
+
warn: 'yellow',
|
|
51
|
+
info: 'green',
|
|
52
|
+
debug: 'blue'
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Basic logging
|
|
58
|
+
await logger.info('Application started', { version: '1.0.0' });
|
|
59
|
+
await logger.warn('High memory usage', { memory: '85%' });
|
|
60
|
+
await logger.error(new Error('Something went wrong'));
|
|
61
|
+
|
|
62
|
+
// Performance timing
|
|
63
|
+
logger.time('database-query');
|
|
64
|
+
// ... your code ...
|
|
65
|
+
await logger.timeEnd('database-query');
|
|
66
|
+
|
|
67
|
+
// Async timing
|
|
68
|
+
const result = await logger.timeAsync('api-call', async () => {
|
|
69
|
+
return await fetch('/api/data');
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## ๐ฏ Custom Log Levels
|
|
74
|
+
|
|
75
|
+
Define your own log levels with custom priorities and colors:
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { createLogger } from 'logixia';
|
|
79
|
+
|
|
80
|
+
const logger = createLogger({
|
|
81
|
+
appName: 'EcommerceApp',
|
|
82
|
+
levelOptions: {
|
|
83
|
+
level: 'info',
|
|
84
|
+
levels: {
|
|
85
|
+
// Standard levels
|
|
86
|
+
error: 0,
|
|
87
|
+
warn: 1,
|
|
88
|
+
info: 2,
|
|
89
|
+
debug: 3,
|
|
90
|
+
// Custom business levels
|
|
91
|
+
order: 2, // Order processing
|
|
92
|
+
payment: 1, // Payment processing (high priority)
|
|
93
|
+
inventory: 2, // Inventory management
|
|
94
|
+
customer: 3, // Customer interactions
|
|
95
|
+
},
|
|
96
|
+
colors: {
|
|
97
|
+
error: 'red',
|
|
98
|
+
warn: 'yellow',
|
|
99
|
+
info: 'blue',
|
|
100
|
+
debug: 'green',
|
|
101
|
+
order: 'brightBlue',
|
|
102
|
+
payment: 'brightYellow',
|
|
103
|
+
inventory: 'cyan',
|
|
104
|
+
customer: 'brightGreen',
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// Use custom levels with full TypeScript support
|
|
110
|
+
await logger.order('Order processing started', { orderId: '12345' });
|
|
111
|
+
await logger.payment('Payment processed', { amount: 99.99 });
|
|
112
|
+
await logger.inventory('Stock updated', { productId: 'ABC123', quantity: 50 });
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## ๐๏ธ NestJS Integration
|
|
116
|
+
|
|
117
|
+
### Module Setup
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import { Module } from '@nestjs/common';
|
|
121
|
+
import { LogixiaLoggerModule } from 'logixia';
|
|
122
|
+
|
|
123
|
+
@Module({
|
|
124
|
+
imports: [
|
|
125
|
+
LogixiaLoggerModule.forRoot({
|
|
126
|
+
appName: 'NestJS-App',
|
|
127
|
+
environment: 'development',
|
|
128
|
+
traceId: true,
|
|
129
|
+
levelOptions: {
|
|
130
|
+
level: 'debug',
|
|
131
|
+
levels: {
|
|
132
|
+
error: 0,
|
|
133
|
+
warn: 1,
|
|
134
|
+
info: 2,
|
|
135
|
+
debug: 3,
|
|
136
|
+
verbose: 4
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
],
|
|
141
|
+
})
|
|
142
|
+
export class AppModule {}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Service Usage
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
import { Injectable } from '@nestjs/common';
|
|
149
|
+
import { LogixiaLoggerService } from 'logixia';
|
|
150
|
+
|
|
151
|
+
@Injectable()
|
|
152
|
+
export class UserService {
|
|
153
|
+
constructor(private readonly logger: LogixiaLoggerService) {
|
|
154
|
+
this.logger.setContext('UserService');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async findUser(id: string) {
|
|
158
|
+
await this.logger.info('Fetching user', { userId: id });
|
|
159
|
+
|
|
160
|
+
try {
|
|
161
|
+
const user = await this.userRepository.findById(id);
|
|
162
|
+
await this.logger.info('User found', { userId: id });
|
|
163
|
+
return user;
|
|
164
|
+
} catch (error) {
|
|
165
|
+
await this.logger.error('User not found', { userId: id, error });
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async createUser(userData: any) {
|
|
171
|
+
const childLogger = this.logger.child('createUser', { operation: 'create' });
|
|
172
|
+
|
|
173
|
+
return await childLogger.timeAsync('user-creation', async () => {
|
|
174
|
+
await childLogger.info('Creating user', { userData });
|
|
175
|
+
const user = await this.userRepository.create(userData);
|
|
176
|
+
await childLogger.info('User created', { userId: user.id });
|
|
177
|
+
return user;
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## ๐ Express Integration
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
import express from 'express';
|
|
187
|
+
import { createLogger, traceMiddleware, getCurrentTraceId } from 'logixia';
|
|
188
|
+
|
|
189
|
+
const app = express();
|
|
190
|
+
const logger = createLogger({ appName: 'ExpressApp' });
|
|
191
|
+
|
|
192
|
+
// Add trace middleware for request tracking
|
|
193
|
+
app.use(traceMiddleware({
|
|
194
|
+
enabled: true,
|
|
195
|
+
extractor: {
|
|
196
|
+
header: ['x-trace-id', 'x-request-id'],
|
|
197
|
+
query: ['traceId']
|
|
198
|
+
}
|
|
199
|
+
}));
|
|
200
|
+
|
|
201
|
+
// Request logging middleware
|
|
202
|
+
app.use((req, res, next) => {
|
|
203
|
+
logger.info(`${req.method} ${req.path}`, {
|
|
204
|
+
method: req.method,
|
|
205
|
+
path: req.path,
|
|
206
|
+
traceId: getCurrentTraceId(),
|
|
207
|
+
userAgent: req.get('User-Agent')
|
|
208
|
+
});
|
|
209
|
+
next();
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
app.get('/users/:id', async (req, res) => {
|
|
213
|
+
const { id } = req.params;
|
|
214
|
+
const userLogger = logger.child('UserRoute', { userId: id });
|
|
215
|
+
|
|
216
|
+
const userData = await logger.timeAsync('fetch-user', async () => {
|
|
217
|
+
// Your database logic here
|
|
218
|
+
return { id, name: 'John Doe' };
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
res.json(userData);
|
|
222
|
+
});
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## ๐ Performance Monitoring
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
import { createLogger } from 'logixia';
|
|
229
|
+
|
|
230
|
+
const logger = createLogger({
|
|
231
|
+
appName: 'PerformanceApp',
|
|
232
|
+
format: { json: true } // Better for parsing in production
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
class DatabaseService {
|
|
236
|
+
async findUser(id: string) {
|
|
237
|
+
// Simple timing
|
|
238
|
+
logger.time(`db-find-user-${id}`);
|
|
239
|
+
const user = await this.db.findById(id);
|
|
240
|
+
const timeTaken = await logger.timeEnd(`db-find-user-${id}`);
|
|
241
|
+
|
|
242
|
+
await logger.info('Database query completed', {
|
|
243
|
+
operation: 'findUser',
|
|
244
|
+
userId: id,
|
|
245
|
+
timeTaken: `${timeTaken}ms`
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
return user;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async createUser(userData: any) {
|
|
252
|
+
// Async timing with automatic logging
|
|
253
|
+
return await logger.timeAsync('db-create-user', async () => {
|
|
254
|
+
const user = await this.db.create(userData);
|
|
255
|
+
await logger.info('User created', { userId: user.id });
|
|
256
|
+
return user;
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## ๐๏ธ Field Configuration
|
|
263
|
+
|
|
264
|
+
Customize which fields appear in your logs:
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
const logger = createLogger({
|
|
268
|
+
appName: 'CustomApp',
|
|
269
|
+
fields: {
|
|
270
|
+
timestamp: '[yyyy-mm-dd HH:MM:ss.MS]', // Custom format
|
|
271
|
+
level: true, // Enable with default
|
|
272
|
+
appName: false, // Disable
|
|
273
|
+
traceId: true, // Enable
|
|
274
|
+
message: true, // Enable
|
|
275
|
+
payload: true, // Enable
|
|
276
|
+
timeTaken: '[duration_ms]', // Custom format
|
|
277
|
+
context: '[CTX]', // Custom format
|
|
278
|
+
environment: false // Disable
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## ๐ถ Child Loggers
|
|
284
|
+
|
|
285
|
+
Create contextual child loggers for better organization:
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
const mainLogger = createLogger({ appName: 'MainApp' });
|
|
289
|
+
|
|
290
|
+
// Create child logger with additional context
|
|
291
|
+
const userLogger = mainLogger.child('UserService', {
|
|
292
|
+
module: 'user-management',
|
|
293
|
+
version: '2.0'
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
// Child logger inherits parent config but adds its own context
|
|
297
|
+
await userLogger.info('Processing user request'); // Includes UserService context
|
|
298
|
+
|
|
299
|
+
// Create nested child loggers
|
|
300
|
+
const operationLogger = userLogger.child('CreateUser', {
|
|
301
|
+
operation: 'create',
|
|
302
|
+
requestId: 'req-123'
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
await operationLogger.info('Validation started'); // Includes all parent contexts
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## ๐ Trace ID Support
|
|
309
|
+
|
|
310
|
+
Built-in request tracing across your application:
|
|
311
|
+
|
|
312
|
+
```typescript
|
|
313
|
+
import { createLogger, runWithTraceId, getCurrentTraceId } from 'logixia';
|
|
314
|
+
|
|
315
|
+
const logger = createLogger({
|
|
316
|
+
appName: 'TracedApp',
|
|
317
|
+
traceId: true
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
// Manual trace ID management
|
|
321
|
+
runWithTraceId('custom-trace-123', async () => {
|
|
322
|
+
await logger.info('Operation started'); // Automatically includes trace ID
|
|
323
|
+
|
|
324
|
+
// Get current trace ID
|
|
325
|
+
const traceId = getCurrentTraceId();
|
|
326
|
+
console.log('Current trace:', traceId); // 'custom-trace-123'
|
|
327
|
+
|
|
328
|
+
await someAsyncOperation();
|
|
329
|
+
await logger.info('Operation completed'); // Same trace ID
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
// Automatic trace ID generation
|
|
333
|
+
runWithTraceId(async () => {
|
|
334
|
+
await logger.info('Auto-generated trace ID');
|
|
335
|
+
});
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
## ๐จ Custom Formatters
|
|
339
|
+
|
|
340
|
+
Create your own log formatters:
|
|
341
|
+
|
|
342
|
+
```typescript
|
|
343
|
+
import { ILogFormatter, LogEntry } from 'logixia';
|
|
344
|
+
|
|
345
|
+
class CustomFormatter implements ILogFormatter {
|
|
346
|
+
format(entry: LogEntry): string {
|
|
347
|
+
return `๐ [${entry.level.toUpperCase()}] ${entry.message} ${JSON.stringify(entry.payload || {})}`;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const logger = createLogger({
|
|
352
|
+
appName: 'CustomApp',
|
|
353
|
+
formatters: [new CustomFormatter()]
|
|
354
|
+
});
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
## โ๏ธ Configuration Options
|
|
358
|
+
|
|
359
|
+
```typescript
|
|
360
|
+
interface LoggerConfig {
|
|
361
|
+
appName?: string; // Application name
|
|
362
|
+
environment?: 'development' | 'production';
|
|
363
|
+
traceId?: boolean | TraceIdConfig; // Enable trace ID tracking
|
|
364
|
+
format?: {
|
|
365
|
+
timestamp?: boolean; // Include timestamps
|
|
366
|
+
colorize?: boolean; // Colorize output
|
|
367
|
+
json?: boolean; // JSON format output
|
|
368
|
+
};
|
|
369
|
+
silent?: boolean; // Disable all output
|
|
370
|
+
levelOptions?: {
|
|
371
|
+
level?: string; // Current log level
|
|
372
|
+
levels?: Record<string, number>; // Custom levels with priorities
|
|
373
|
+
colors?: Record<string, LogColor>; // Custom colors for levels
|
|
374
|
+
};
|
|
375
|
+
fields?: Partial<Record<LogFieldKey, string | boolean>>; // Field configuration
|
|
376
|
+
}
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
## ๐ API Reference
|
|
380
|
+
|
|
381
|
+
### Core Logger Methods
|
|
382
|
+
|
|
383
|
+
```typescript
|
|
384
|
+
// Standard log levels
|
|
385
|
+
await logger.error(message: string | Error, data?: Record<string, any>);
|
|
386
|
+
await logger.warn(message: string, data?: Record<string, any>);
|
|
387
|
+
await logger.info(message: string, data?: Record<string, any>);
|
|
388
|
+
await logger.debug(message: string, data?: Record<string, any>);
|
|
389
|
+
await logger.trace(message: string, data?: Record<string, any>);
|
|
390
|
+
await logger.verbose(message: string, data?: Record<string, any>);
|
|
391
|
+
|
|
392
|
+
// Custom level logging
|
|
393
|
+
await logger.logLevel(level: string, message: string, data?: Record<string, any>);
|
|
394
|
+
|
|
395
|
+
// Timing methods
|
|
396
|
+
logger.time(label: string): void;
|
|
397
|
+
await logger.timeEnd(label: string): Promise<number | undefined>;
|
|
398
|
+
await logger.timeAsync<T>(label: string, fn: () => Promise<T>): Promise<T>;
|
|
399
|
+
|
|
400
|
+
// Context management
|
|
401
|
+
logger.setContext(context: string): void;
|
|
402
|
+
logger.getContext(): string | undefined;
|
|
403
|
+
logger.setLevel(level: string): void;
|
|
404
|
+
logger.getLevel(): string;
|
|
405
|
+
|
|
406
|
+
// Child loggers
|
|
407
|
+
logger.child(context: string, data?: Record<string, any>): ILogger;
|
|
408
|
+
|
|
409
|
+
// Cleanup
|
|
410
|
+
await logger.close(): Promise<void>;
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### NestJS Service Methods
|
|
414
|
+
|
|
415
|
+
```typescript
|
|
416
|
+
// NestJS LoggerService interface
|
|
417
|
+
log(message: any, context?: string): void;
|
|
418
|
+
error(message: any, trace?: string, context?: string): void;
|
|
419
|
+
warn(message: any, context?: string): void;
|
|
420
|
+
debug(message: any, context?: string): void;
|
|
421
|
+
verbose(message: any, context?: string): void;
|
|
422
|
+
|
|
423
|
+
// Extended Logixia methods
|
|
424
|
+
await info(message: string, data?: Record<string, any>): Promise<void>;
|
|
425
|
+
await trace(message: string, data?: Record<string, any>): Promise<void>;
|
|
426
|
+
getCurrentTraceId(): string | undefined;
|
|
427
|
+
child(context: string, data?: Record<string, any>): LogixiaLoggerService;
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
## ๐งช Examples
|
|
431
|
+
|
|
432
|
+
Check out the `/examples` directory for comprehensive usage examples:
|
|
433
|
+
|
|
434
|
+
- **Basic Usage** (`examples/basic-usage.ts`) - Simple logging setup
|
|
435
|
+
- **Custom Levels** (`examples/custom-levels.ts`) - Business-specific log levels
|
|
436
|
+
- **NestJS Integration** (`examples/nestjs-example.ts`) - Full NestJS setup
|
|
437
|
+
- **Express Integration** (`examples/express-example.ts`) - Express middleware
|
|
438
|
+
- **Performance Monitoring** (`examples/performance-monitoring.ts`) - Timing and metrics
|
|
439
|
+
- **Field Configuration** (`examples/field-configuration.ts`) - Custom field setup
|
|
440
|
+
|
|
441
|
+
## ๐โโ๏ธ Running Examples
|
|
442
|
+
|
|
443
|
+
```bash
|
|
444
|
+
# Run basic usage example
|
|
445
|
+
npm run dev:basic-usage
|
|
446
|
+
|
|
447
|
+
# Run custom levels example
|
|
448
|
+
npm run dev:custom-levels
|
|
449
|
+
|
|
450
|
+
# Run NestJS example
|
|
451
|
+
npm run dev:nestjs
|
|
452
|
+
|
|
453
|
+
# Run Express example
|
|
454
|
+
npm run dev:express
|
|
455
|
+
|
|
456
|
+
# Run performance monitoring example
|
|
457
|
+
npm run dev:performance
|
|
458
|
+
|
|
459
|
+
# Run field configuration example
|
|
460
|
+
npm run dev:fields
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
## ๐ง Development
|
|
464
|
+
|
|
465
|
+
```bash
|
|
466
|
+
# Install dependencies
|
|
467
|
+
npm install
|
|
468
|
+
|
|
469
|
+
# Build the project
|
|
470
|
+
npm run build
|
|
471
|
+
|
|
472
|
+
# Run tests
|
|
473
|
+
npm test
|
|
474
|
+
|
|
475
|
+
# Run linting
|
|
476
|
+
npm run lint
|
|
477
|
+
|
|
478
|
+
# Format code
|
|
479
|
+
npm run format
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
## ๐ Requirements
|
|
483
|
+
|
|
484
|
+
- **Node.js**: 16.0.0 or higher
|
|
485
|
+
- **TypeScript**: 5.0.0 or higher (for development)
|
|
486
|
+
|
|
487
|
+
## ๐ค Contributing
|
|
488
|
+
|
|
489
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
490
|
+
|
|
491
|
+
## ๐ License
|
|
492
|
+
|
|
493
|
+
MIT License - see the [LICENSE](LICENSE) file for details.
|
|
494
|
+
|
|
495
|
+
## ๐ Acknowledgments
|
|
496
|
+
|
|
497
|
+
- Built with TypeScript for maximum type safety
|
|
498
|
+
- Inspired by modern logging best practices
|
|
499
|
+
- Designed for scalable applications
|
|
500
|
+
|
|
501
|
+
---
|
|
502
|
+
|
|
503
|
+
**Made with โค๏ธ for the TypeScript community**
|