-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog-server.ts
More file actions
457 lines (383 loc) · 12 KB
/
log-server.ts
File metadata and controls
457 lines (383 loc) · 12 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/usr/bin/env node
/**
* Simple log server for TabAgent extension
* Receives logs from extension and exposes them via HTTP
*/
import express from 'express';
import cors from 'cors';
const app = express() as any;
const PORT = 3333;
// Store logs in memory (last 1000)
const logs: any[] = [];
const MAX_LOGS = 1000;
// Middleware
app.use(cors());
app.use(express.json());
// Web UI - Terminal-style log viewer
app.get('/', (req, res) => {
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>TabAgent Logs - Terminal</title>
<meta charset="UTF-8">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
background: #0a0a0a;
color: #00ff00;
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
}
.terminal-header {
background: #1a1a1a;
padding: 12px 20px;
border-bottom: 2px solid #00ff00;
display: flex;
align-items: center;
justify-content: space-between;
}
.terminal-title {
color: #00ff00;
font-size: 16px;
font-weight: bold;
display: flex;
align-items: center;
gap: 8px;
}
.terminal-controls {
display: flex;
gap: 8px;
align-items: center;
flex-wrap: wrap;
}
.terminal-controls label {
color: #888;
font-size: 12px;
display: flex;
align-items: center;
gap: 6px;
}
select, input[type="text"], input[type="number"] {
padding: 4px 8px;
background: #0a0a0a;
color: #00ff00;
border: 1px solid #333;
border-radius: 3px;
font-family: inherit;
font-size: 12px;
}
input[type="text"] { width: 200px; }
input[type="number"] { width: 70px; }
select:focus, input:focus {
outline: none;
border-color: #00ff00;
}
button {
padding: 5px 12px;
background: #1a1a1a;
color: #00ff00;
border: 1px solid #00ff00;
border-radius: 3px;
cursor: pointer;
font-family: inherit;
font-size: 12px;
transition: all 0.2s;
}
button:hover {
background: #00ff00;
color: #0a0a0a;
}
button.clear {
border-color: #ff3333;
color: #ff3333;
}
button.clear:hover {
background: #ff3333;
color: #0a0a0a;
}
.stats {
color: #666;
font-size: 11px;
padding: 0 8px;
}
.terminal-body {
flex: 1;
overflow-y: auto;
padding: 12px 20px;
background: #0a0a0a;
font-size: 13px;
line-height: 1.4;
}
.log-line {
margin-bottom: 8px;
padding: 6px 0;
border-left: 3px solid transparent;
padding-left: 8px;
}
.log-line:hover {
background: #111;
border-left-color: #00ff00;
}
.log-meta {
display: inline-block;
margin-right: 8px;
}
.timestamp { color: #666; }
.level-error { color: #ff3333; font-weight: bold; }
.level-warn { color: #ffaa00; font-weight: bold; }
.level-log { color: #00ff00; }
.level-info { color: #00aaff; }
.level-debug { color: #aa00ff; }
.context {
color: #00aaff;
padding: 1px 6px;
border-radius: 2px;
font-size: 11px;
}
.message {
color: #ddd;
word-break: break-word;
white-space: pre-wrap;
margin-left: 4px;
}
.no-logs {
text-align: center;
padding: 60px 20px;
color: #444;
font-size: 14px;
}
.terminal-body::-webkit-scrollbar { width: 10px; }
.terminal-body::-webkit-scrollbar-track { background: #0a0a0a; }
.terminal-body::-webkit-scrollbar-thumb { background: #333; border-radius: 5px; }
.terminal-body::-webkit-scrollbar-thumb:hover { background: #555; }
input[type="checkbox"] {
cursor: pointer;
width: 14px;
height: 14px;
}
</style>
</head>
<body>
<div class="terminal-header">
<div class="terminal-title">
<span>⚡ TabAgent Logger</span>
</div>
<div class="terminal-controls">
<button onclick="clearLogs()" class="clear">⌫ Clear</button>
<label>
Context:
<select id="contextFilter" onchange="applyFilters()">
<option value="">All</option>
</select>
</label>
<label>
Level:
<select id="levelFilter" onchange="applyFilters()">
<option value="">All</option>
<option value="error">Error</option>
<option value="warn">Warn</option>
<option value="log">Log</option>
<option value="info">Info</option>
<option value="debug">Debug</option>
</select>
</label>
<label>
Search:
<input type="text" id="searchFilter" placeholder="Filter messages..." oninput="applyFilters()">
</label>
<label>
Show:
<input type="number" id="limitInput" value="100" min="10" max="1000" step="50" onchange="applyFilters()">
</label>
<label>
<input type="checkbox" id="autoRefresh" checked>
Live
</label>
<span class="stats" id="stats">─</span>
</div>
</div>
<div class="terminal-body" id="logContainer">
<div class="no-logs">Waiting for logs...</div>
</div>
<script>
let autoRefreshInterval = null;
let allContexts = new Set();
async function applyFilters() {
const level = document.getElementById('levelFilter').value;
const context = document.getElementById('contextFilter').value;
const search = document.getElementById('searchFilter').value.toLowerCase();
const limit = document.getElementById('limitInput').value;
let url = '/logs?limit=' + limit;
if (level) url += '&level=' + level;
if (context) url += '&context=' + context;
try {
const response = await fetch(url);
const data = await response.json();
// Update context filter options
updateContextFilter(data.logs);
// Apply client-side search filter
let filtered = data.logs;
if (search) {
filtered = filtered.filter(log =>
log.message.toLowerCase().includes(search) ||
log.context.toLowerCase().includes(search)
);
}
const total = data.count;
const shown = filtered.length;
document.getElementById('stats').textContent =
shown === total ? \`\${total} logs\` : \`\${shown}/\${total} logs\`;
renderLogs(filtered);
} catch (err) {
document.getElementById('stats').textContent = '⚠ Error: ' + err.message;
}
}
function updateContextFilter(logs) {
const contextFilter = document.getElementById('contextFilter');
const currentValue = contextFilter.value;
// Collect all unique contexts
logs.forEach(log => allContexts.add(log.context));
// Rebuild options
const sortedContexts = Array.from(allContexts).sort();
contextFilter.innerHTML = '<option value="">All</option>' +
sortedContexts.map(ctx => \`<option value="\${ctx}">\${ctx}</option>\`).join('');
// Restore selection
if (currentValue && sortedContexts.includes(currentValue)) {
contextFilter.value = currentValue;
}
}
function renderLogs(logs) {
const container = document.getElementById('logContainer');
if (logs.length === 0) {
container.innerHTML = '<div class="no-logs">No logs match your filters.</div>';
return;
}
container.innerHTML = logs.map(log => {
const time = new Date(log.timestamp).toLocaleTimeString('en-US', {
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
return \`
<div class="log-line">
<span class="log-meta timestamp">\${time}</span>
<span class="log-meta level-\${log.level}">[\${log.level.toUpperCase()}]</span>
<span class="log-meta context">\${log.context}</span>
<span class="message">\${escapeHtml(log.message)}</span>
</div>
\`;
}).join('');
// Auto-scroll to bottom
container.scrollTop = container.scrollHeight;
}
async function clearLogs() {
try {
await fetch('/clear', { method: 'POST' });
allContexts.clear();
document.getElementById('searchFilter').value = '';
applyFilters();
} catch (err) {
alert('Error clearing logs: ' + err.message);
}
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Auto-refresh toggle
document.getElementById('autoRefresh').addEventListener('change', (e) => {
if (e.target.checked) {
autoRefreshInterval = setInterval(applyFilters, 2000);
} else {
if (autoRefreshInterval) clearInterval(autoRefreshInterval);
}
});
// Keyboard shortcuts
document.addEventListener('keydown', (e) => {
// Ctrl+L to clear
if (e.ctrlKey && e.key === 'l') {
e.preventDefault();
clearLogs();
}
// Ctrl+F to focus search
if (e.ctrlKey && e.key === 'f') {
e.preventDefault();
document.getElementById('searchFilter').focus();
}
});
// Initial load
applyFilters();
autoRefreshInterval = setInterval(applyFilters, 2000);
</script>
</body>
</html>
`);
});
// POST endpoint - extension sends logs here
app.post('/log', (req, res) => {
const { level, message, timestamp, context, data } = req.body;
const logEntry = {
timestamp: timestamp || new Date().toISOString(),
level: level || 'log',
message: message || '',
context: context || 'unknown',
data: data || null
};
logs.push(logEntry);
// Keep only last MAX_LOGS
if (logs.length > MAX_LOGS) {
logs.shift();
}
res.json({ success: true, logsStored: logs.length });
});
// GET endpoint - fetch logs
app.get('/logs', (req, res) => {
const { level, context, since, limit } = req.query;
let filtered = [...logs];
// Filter by level
if (level) {
filtered = filtered.filter(log => log.level === level);
}
// Filter by context
if (context) {
filtered = filtered.filter(log => log.context === context);
}
// Filter by timestamp
if (since) {
const sinceDate = new Date(since as string);
filtered = filtered.filter(log => new Date(log.timestamp) > sinceDate);
}
// Limit results
const maxLimit = parseInt(limit as string) || 100;
filtered = filtered.slice(-maxLimit);
res.json({
count: filtered.length,
logs: filtered
});
});
// Clear logs (DELETE method)
app.delete('/logs', (req, res) => {
const count = logs.length;
logs.length = 0;
res.json({ success: true, cleared: count });
});
// Clear logs (POST method for convenience)
app.post('/clear', (req, res) => {
const count = logs.length;
logs.length = 0;
res.json({ success: true, cleared: count });
});
// Start server
app.listen(PORT, () => {
console.log(`📝 TabAgent Log Server running on http://localhost:${PORT}`);
console.log(` POST /log - Extension sends logs here`);
console.log(` GET /logs - Fetch logs (supports ?level=error&limit=50)`);
console.log(` POST /clear - Clear all logs`);
console.log(` DELETE /logs - Clear all logs`);
});