-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-simple-logger.js
More file actions
132 lines (113 loc) · 5.22 KB
/
Copy pathtest-simple-logger.js
File metadata and controls
132 lines (113 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Comprehensive test script for Simple Logger
const SimpleLogger = require('./simple-logger.js');
console.log('=== Simple Logger Comprehensive Test ===\n');
// Test 1: Basic functionality with new formatting
console.log('1. Basic Functionality & New Formatting Features:');
const logger = new SimpleLogger({
appName: 'TestApp',
level: 'HIGH',
enableFileLogging: true,
logDirectory: 'logs'
});
console.log(' File logging enabled:', logger.isFileLoggingEnabled());
console.log(' Log file path:', logger.getLogFilePath());
logger.error('Payment processing failed', { userId: 123, errorCode: 'CARD_DECLINED', amount: 99.99 });
logger.warn('API rate limit approaching', { remaining: 10, resetTime: '14:30:00' });
logger.info('User login successful', { userId: 123, method: 'oauth', ip: '192.168.1.1' });
logger.debug('Database query executed', { query: 'SELECT * FROM users', duration: '45ms' });
logger.dev('Development debug info', { memoryUsage: '512MB', cacheHits: 95 });
// Test 2: Level filtering
console.log('\n2. Log Level Filtering:');
logger.setLevel('LOW');
console.log(' Set to LOW level - should only show ERROR:');
logger.error('Critical system failure', { service: 'database' });
logger.warn('This warning should NOT appear');
logger.info('This info should NOT appear');
logger.setLevel('MED');
console.log('\n Set to MED level - should show ERROR and WARN:');
logger.error('Authentication failed', { attempts: 3 });
logger.warn('Memory usage high', { percent: 85 });
logger.info('This info should NOT appear');
logger.setLevel('HIGH');
console.log('\n Set to HIGH level - should show ERROR, WARN, INFO, DEBUG:');
logger.error('Service unavailable', { status: 503 });
logger.warn('SSL certificate expiring', { days: 7 });
logger.info('User session created', { sessionId: 'abc123' });
logger.debug('Cache hit', { key: 'user:123' });
// Test 3: Formatting variations
console.log('\n3. Different Formatting Options:');
console.log('\n Compact Mode:');
const compactLogger = new SimpleLogger({
appName: 'Compact',
level: 'HIGH',
compactMode: true,
enableFileLogging: false
});
compactLogger.error('Database timeout', { host: 'db-server', timeout: '5s' });
compactLogger.info('Background job completed', { jobId: 'backup-001' });
console.log('\n Key-Value Data Format:');
const kvLogger = new SimpleLogger({
appName: 'KeyVal',
level: 'HIGH',
dataFormat: 'keyvalue',
enableFileLogging: false
});
kvLogger.warn('Disk space low', { partition: '/var/log', available: '2GB', used: '95%' });
console.log('\n Without Process ID:');
const cleanLogger = new SimpleLogger({
appName: 'Clean',
level: 'HIGH',
showProcessId: false,
enableFileLogging: false
});
cleanLogger.info('Application started', { version: '1.2.3', port: 3000 });
// Test 4: Console replacement
console.log('\n4. Console Replacement Feature:');
logger.setLevel('DEV');
logger.enableConsoleReplacement();
console.log('This console.log should be captured by the logger');
console.warn('This console.warn should be captured by the logger');
logger.disableConsoleReplacement();
console.log(' Console restored - this appears normally');
// Test 5: NONE level
console.log('\n5. NONE Level (silent mode):');
logger.setLevel('NONE');
console.log(' Set to NONE - the following should NOT appear:');
logger.error('This should be silent');
logger.warn('This should be silent');
logger.info('This should be silent');
// Test 6: File logging verification
console.log('\n6. File Logging Verification:');
logger.setLevel('HIGH');
logger.info('Final verification message', { test: 'complete', timestamp: new Date().toISOString() });
const fs = require('fs');
if (logger.getLogFilePath() && fs.existsSync(logger.getLogFilePath())) {
console.log(' ✓ Log file created at:', logger.getLogFilePath());
const logContent = fs.readFileSync(logger.getLogFilePath(), 'utf8');
const lineCount = logContent.split('\n').filter(line => line.trim()).length;
console.log(' ✓ Log file contains', lineCount, 'entries');
// Show sample of file content (first 3 lines)
const lines = logContent.split('\n').filter(line => line.trim());
console.log(' 📄 Sample file content (plain text, no colors):');
lines.slice(0, 3).forEach((line, i) => {
console.log(` ${i + 1}. ${line}`);
});
} else {
console.log(' ✗ Log file was not created');
}
// Test 7: API methods
console.log('\n7. API Methods:');
console.log(' Current log level:', logger.getLevel());
console.log(' File logging enabled:', logger.isFileLoggingEnabled());
console.log(' Console replacement enabled:', logger.isConsoleReplacementEnabled());
console.log('\n=== All Tests Complete ===');
console.log('Features tested:');
console.log(' ✓ Basic logging (ERROR, WARN, INFO, DEBUG, DEV)');
console.log(' ✓ Log level filtering (NONE, LOW, MED, HIGH, DEV)');
console.log(' ✓ Multiple formatting options (compact, key-value, clean)');
console.log(' ✓ Console replacement and restoration');
console.log(' ✓ File logging with automatic directory creation');
console.log(' ✓ Color-coded console output');
console.log(' ✓ Human-readable timestamps');
console.log(' ✓ API methods for status checking');
console.log('\nCheck the logs/ directory for generated log files!');