@octaviaflow/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.
@@ -0,0 +1,494 @@
1
+ // ============================================
2
+ // Production-Ready Logger Types
3
+ // ============================================
4
+
5
+ // ============================================
6
+ // CORE TYPES
7
+ // ============================================
8
+
9
+ export type LogLevel = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
10
+ export type LogService = 'engine' | 'backend' | 'ui' | 'worker' | 'scheduler';
11
+ export type LogFormat = 'json' | 'pretty' | 'compact' | 'ecs'; // ECS = Elastic Common Schema
12
+
13
+ // ============================================
14
+ // CONTEXT & CORRELATION
15
+ // ============================================
16
+
17
+ export interface CorrelationContext {
18
+ traceId: string; // Distributed trace ID
19
+ spanId: string; // Current span ID
20
+ parentSpanId?: string; // Parent span ID
21
+ correlationId: string; // Business correlation ID
22
+ sessionId?: string; // User session ID
23
+ }
24
+
25
+ export interface RequestContext {
26
+ requestId: string;
27
+ method: string;
28
+ url: string;
29
+ path: string;
30
+ query?: Record<string, unknown>;
31
+ headers?: Record<string, string>;
32
+ body?: unknown;
33
+ ip: string;
34
+ userAgent?: string;
35
+ referer?: string;
36
+ protocol: string;
37
+ hostname: string;
38
+ }
39
+
40
+ export interface UserContext {
41
+ userId: string;
42
+ username?: string;
43
+ email?: string;
44
+ organizationId: string;
45
+ projectId?: string;
46
+ roles?: string[];
47
+ subscription?: 'free' | 'starter' | 'growth' | 'business' | 'enterprise';
48
+ }
49
+
50
+ export interface WorkflowContext {
51
+ workflowId: string;
52
+ workflowName?: string;
53
+ executionId?: string;
54
+ stepId?: string;
55
+ stepName?: string;
56
+ triggeredBy?: string;
57
+ environment?: 'development' | 'staging' | 'production';
58
+ }
59
+
60
+ // ============================================
61
+ // LOG METADATA
62
+ // ============================================
63
+
64
+ export interface BaseLogMeta {
65
+ timestamp: string; // ISO 8601
66
+ level: LogLevel;
67
+ service: LogService;
68
+ hostname: string;
69
+ pid: number;
70
+ version: string; // App version
71
+ environment: 'development' | 'staging' | 'production';
72
+
73
+ // Contexts
74
+ correlation?: CorrelationContext;
75
+ request?: RequestContext;
76
+ user?: UserContext;
77
+ workflow?: WorkflowContext;
78
+
79
+ // Additional metadata
80
+ [key: string]: unknown;
81
+ }
82
+
83
+ // ============================================
84
+ // SPECIALIZED LOG TYPES
85
+ // ============================================
86
+
87
+ export interface SystemLogMeta extends BaseLogMeta {
88
+ type: 'startup' | 'shutdown' | 'health' | 'config' | 'dependency';
89
+ component: string;
90
+ operation: string;
91
+ details?: {
92
+ configLoaded?: boolean;
93
+ connectionsEstablished?: string[];
94
+ initDuration?: number;
95
+ memoryUsage?: number;
96
+ cpuUsage?: number;
97
+ };
98
+ }
99
+
100
+ export interface PerformanceLogMeta extends BaseLogMeta {
101
+ type: 'performance';
102
+ operation: string;
103
+ duration: {
104
+ ms: number;
105
+ human: string; // "1.23s", "45ms"
106
+ };
107
+ metrics?: {
108
+ cpu?: number;
109
+ memory?: number;
110
+ dbQueries?: number;
111
+ apiCalls?: number;
112
+ cacheHits?: number;
113
+ cacheMisses?: number;
114
+ };
115
+ slow?: boolean; // Flag if exceeds threshold
116
+ }
117
+
118
+ export interface DatabaseLogMeta extends BaseLogMeta {
119
+ type: 'database';
120
+ operation: 'query' | 'transaction' | 'migration' | 'connection';
121
+ database: string;
122
+ table?: string;
123
+ query?: string; // Sanitized query
124
+ duration?: number;
125
+ rowsAffected?: number;
126
+ transaction?: {
127
+ id: string;
128
+ status: 'begin' | 'commit' | 'rollback';
129
+ };
130
+ }
131
+
132
+ export interface APILogMeta extends BaseLogMeta {
133
+ type: 'api';
134
+ direction: 'inbound' | 'outbound';
135
+ statusCode?: number;
136
+ duration?: number;
137
+
138
+ // For outbound
139
+ target?: {
140
+ service: string;
141
+ endpoint: string;
142
+ method: string;
143
+ };
144
+
145
+ // Rate limiting
146
+ rateLimit?: {
147
+ limit: number;
148
+ remaining: number;
149
+ reset: string;
150
+ };
151
+ }
152
+
153
+ export interface SecurityLogMeta extends BaseLogMeta {
154
+ type: 'security';
155
+ event: 'auth_success' | 'auth_failure' | 'permission_denied' | 'suspicious_activity' | 'rate_limit';
156
+ severity: 'low' | 'medium' | 'high' | 'critical';
157
+ threat?: {
158
+ type: string;
159
+ description: string;
160
+ ipAddress?: string;
161
+ userAgent?: string;
162
+ attemptCount?: number;
163
+ };
164
+ action?: 'blocked' | 'allowed' | 'flagged';
165
+ }
166
+
167
+ export interface AuditLogMeta extends BaseLogMeta {
168
+ type: 'audit';
169
+ action: string; // 'user.created', 'workflow.deleted', etc.
170
+ actor: {
171
+ id: string;
172
+ type: 'user' | 'system' | 'api';
173
+ name?: string;
174
+ };
175
+ resource: {
176
+ type: string;
177
+ id: string;
178
+ name?: string;
179
+ };
180
+ changes?: Array<{
181
+ field: string;
182
+ oldValue?: unknown;
183
+ newValue?: unknown;
184
+ }>;
185
+ result: 'success' | 'failure';
186
+ }
187
+
188
+ export interface ErrorLogMeta extends BaseLogMeta {
189
+ type: 'error';
190
+ error: {
191
+ name: string;
192
+ message: string;
193
+ stack?: string;
194
+ code?: string;
195
+ statusCode?: number;
196
+ originalError?: unknown;
197
+ };
198
+ context?: {
199
+ operation?: string;
200
+ input?: unknown;
201
+ component?: string;
202
+ recoverable?: boolean;
203
+ retryable?: boolean;
204
+ retryCount?: number;
205
+ };
206
+ handled: boolean;
207
+ }
208
+
209
+ export interface WorkflowLogMeta extends BaseLogMeta {
210
+ type: 'workflow';
211
+ event: 'started' | 'completed' | 'failed' | 'step_started' | 'step_completed' | 'step_failed';
212
+ workflow: {
213
+ id: string;
214
+ name: string;
215
+ version: string;
216
+ };
217
+ execution?: {
218
+ id: string;
219
+ attempt: number;
220
+ trigger: string;
221
+ };
222
+ step?: {
223
+ id: string;
224
+ name: string;
225
+ type: string;
226
+ index: number;
227
+ };
228
+ performance?: {
229
+ duration?: number;
230
+ recordsProcessed?: number;
231
+ bytesProcessed?: number;
232
+ };
233
+ cost?: {
234
+ estimated?: number;
235
+ actual?: number;
236
+ credits?: number;
237
+ };
238
+ }
239
+
240
+ export interface MetricsLogMeta extends BaseLogMeta {
241
+ type: 'metrics';
242
+ metrics: {
243
+ counters?: Record<string, number>;
244
+ gauges?: Record<string, number>;
245
+ histograms?: Record<string, number[]>;
246
+ timers?: Record<string, number>;
247
+ };
248
+ aggregation?: {
249
+ period: string; // '1m', '5m', '1h'
250
+ method: 'sum' | 'avg' | 'max' | 'min' | 'p95' | 'p99';
251
+ };
252
+ }
253
+
254
+ // ============================================
255
+ // LOGGER CONFIGURATION
256
+ // ============================================
257
+
258
+ export interface TransportConfig {
259
+ type: 'console' | 'file' | 'stream' | 'http' | 'syslog' | 'datadog' | 'elasticsearch';
260
+ enabled: boolean;
261
+ level?: LogLevel;
262
+ format?: LogFormat;
263
+
264
+ // File transport
265
+ file?: {
266
+ path: string;
267
+ maxSize: string; // '10m', '100m', '1g'
268
+ maxFiles: number;
269
+ compress?: boolean;
270
+ datePattern?: string; // 'YYYY-MM-DD'
271
+ };
272
+
273
+ // Stream transport
274
+ stream?: {
275
+ stream: NodeJS.WritableStream;
276
+ };
277
+
278
+ // HTTP transport
279
+ http?: {
280
+ url: string;
281
+ method?: 'POST' | 'PUT';
282
+ headers?: Record<string, string>;
283
+ batchSize?: number;
284
+ flushInterval?: number;
285
+ retry?: {
286
+ attempts: number;
287
+ delay: number;
288
+ };
289
+ };
290
+
291
+ // External services
292
+ datadog?: {
293
+ apiKey: string;
294
+ site: string;
295
+ service: string;
296
+ source: string;
297
+ tags?: string[];
298
+ };
299
+
300
+ elasticsearch?: {
301
+ node: string | string[];
302
+ index: string;
303
+ type?: string;
304
+ auth?: {
305
+ username: string;
306
+ password: string;
307
+ };
308
+ };
309
+ }
310
+
311
+ export interface SamplingConfig {
312
+ enabled: boolean;
313
+ rules: Array<{
314
+ level?: LogLevel;
315
+ service?: LogService;
316
+ rate: number; // 0.0 to 1.0
317
+ condition?: (meta: BaseLogMeta) => boolean;
318
+ }>;
319
+ }
320
+
321
+ export interface FilterConfig {
322
+ enabled: boolean;
323
+ rules: Array<{
324
+ type: 'include' | 'exclude';
325
+ field: string;
326
+ pattern: string | RegExp;
327
+ action?: 'drop' | 'redact' | 'mask';
328
+ }>;
329
+ }
330
+
331
+ export interface BufferConfig {
332
+ enabled: boolean;
333
+ size: number; // Max buffer size
334
+ flushInterval: number; // Flush interval in ms
335
+ dropOnOverflow?: boolean;
336
+ }
337
+
338
+ export interface LoggerConfig {
339
+ // Basic
340
+ service: LogService;
341
+ environment: 'development' | 'staging' | 'production';
342
+ version: string;
343
+ hostname?: string;
344
+
345
+ // Levels
346
+ level: LogLevel;
347
+ levels?: Record<string, LogLevel>; // Component-specific levels
348
+
349
+ // Format
350
+ format: LogFormat;
351
+ prettyPrint?: boolean;
352
+ colorize?: boolean;
353
+ timestamps?: boolean;
354
+
355
+ // Transports
356
+ transports: TransportConfig[];
357
+
358
+ // Performance
359
+ buffer?: BufferConfig;
360
+ sampling?: SamplingConfig;
361
+
362
+ // Security
363
+ filters?: FilterConfig;
364
+ sanitize?: {
365
+ enabled: boolean;
366
+ fields: string[]; // Fields to sanitize
367
+ replacement?: string;
368
+ };
369
+
370
+ // Error handling
371
+ exitOnError?: boolean;
372
+ handleExceptions?: boolean;
373
+ handleRejections?: boolean;
374
+
375
+ // Metadata
376
+ defaultMeta?: Record<string, unknown>;
377
+ contextStorage?: 'asynclocal' | 'cls-hooked' | 'none';
378
+ }
379
+
380
+ // ============================================
381
+ // LOGGER INTERFACE
382
+ // ============================================
383
+
384
+ export interface Logger {
385
+ // Basic logging
386
+ fatal(message: string, meta?: Partial<BaseLogMeta>): void;
387
+ error(message: string, meta?: Partial<ErrorLogMeta>): void;
388
+ warn(message: string, meta?: Partial<BaseLogMeta>): void;
389
+ info(message: string, meta?: Partial<BaseLogMeta>): void;
390
+ debug(message: string, meta?: Partial<BaseLogMeta>): void;
391
+ trace(message: string, meta?: Partial<BaseLogMeta>): void;
392
+
393
+ // Specialized logging
394
+ audit(action: string, meta: Omit<AuditLogMeta, 'type' | 'level'>): void;
395
+ security(event: SecurityLogMeta['event'], meta: Omit<SecurityLogMeta, 'type' | 'level'>): void;
396
+ performance(operation: string, duration: number, meta?: Partial<PerformanceLogMeta>): void;
397
+ metric(name: string, value: number, meta?: Partial<MetricsLogMeta>): void;
398
+
399
+ // Context management
400
+ child(context: Partial<BaseLogMeta>): Logger;
401
+ setContext(context: Partial<BaseLogMeta>): void;
402
+ clearContext(): void;
403
+
404
+ // Workflow logging
405
+ startWorkflow(workflow: WorkflowContext): void;
406
+ endWorkflow(success: boolean, meta?: Partial<WorkflowLogMeta>): void;
407
+
408
+ // Request logging
409
+ startRequest(request: RequestContext): void;
410
+ endRequest(statusCode: number, duration: number): void;
411
+
412
+ // Utilities
413
+ profile(id: string): void;
414
+ startTimer(): () => void;
415
+ flush(): Promise<void>;
416
+ close(): Promise<void>;
417
+ }
418
+
419
+ // ============================================
420
+ // QUERY & SEARCH
421
+ // ============================================
422
+
423
+ export interface LogQuery {
424
+ startTime?: Date;
425
+ endTime?: Date;
426
+ level?: LogLevel | LogLevel[];
427
+ service?: LogService | LogService[];
428
+ userId?: string;
429
+ requestId?: string;
430
+ traceId?: string;
431
+ workflowId?: string;
432
+ executionId?: string;
433
+ searchText?: string;
434
+ limit?: number;
435
+ offset?: number;
436
+ sort?: 'asc' | 'desc';
437
+ }
438
+
439
+ export interface LogEntry {
440
+ id: string;
441
+ timestamp: Date;
442
+ message: string;
443
+ meta: BaseLogMeta;
444
+ raw?: string;
445
+ }
446
+
447
+ export interface LogSearchResult {
448
+ entries: LogEntry[];
449
+ total: number;
450
+ hasMore: boolean;
451
+ query: LogQuery;
452
+ }
453
+
454
+ // ============================================
455
+ // ALERTS & NOTIFICATIONS
456
+ // ============================================
457
+
458
+ export interface LogAlert {
459
+ id: string;
460
+ name: string;
461
+ enabled: boolean;
462
+ condition: {
463
+ level?: LogLevel;
464
+ service?: LogService;
465
+ pattern?: string | RegExp;
466
+ threshold?: {
467
+ count: number;
468
+ window: string; // '5m', '1h', '24h'
469
+ };
470
+ };
471
+ notification: {
472
+ channels: Array<'email' | 'slack' | 'webhook' | 'pagerduty'>;
473
+ throttle?: string; // '5m', '1h'
474
+ recipients?: string[];
475
+ webhookUrl?: string;
476
+ };
477
+ }
478
+
479
+ // ============================================
480
+ // EXPORT UTILITIES
481
+ // ============================================
482
+
483
+ export type AnyLogMeta =
484
+ | SystemLogMeta
485
+ | PerformanceLogMeta
486
+ | DatabaseLogMeta
487
+ | APILogMeta
488
+ | SecurityLogMeta
489
+ | AuditLogMeta
490
+ | ErrorLogMeta
491
+ | WorkflowLogMeta
492
+ | MetricsLogMeta;
493
+
494
+ export type LogFunction = (message: string, meta?: Partial<BaseLogMeta>) => void;