monten 0.1.2 → 0.1.4
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 +0 -4
- package/dist/logger/format.js +110 -1
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/logger/format.js
CHANGED
|
@@ -1,6 +1,115 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.formatLog = formatLog;
|
|
4
|
+
// ANSI color codes
|
|
5
|
+
const colors = {
|
|
6
|
+
reset: '\x1b[0m',
|
|
7
|
+
red: '\x1b[31m',
|
|
8
|
+
green: '\x1b[32m',
|
|
9
|
+
yellow: '\x1b[33m',
|
|
10
|
+
blue: '\x1b[34m',
|
|
11
|
+
magenta: '\x1b[35m',
|
|
12
|
+
cyan: '\x1b[36m',
|
|
13
|
+
gray: '\x1b[90m',
|
|
14
|
+
bright: '\x1b[1m',
|
|
15
|
+
};
|
|
16
|
+
function colorize(text, color) {
|
|
17
|
+
return `${colors[color]}${text}${colors.reset}`;
|
|
18
|
+
}
|
|
19
|
+
function getLevelColor(level) {
|
|
20
|
+
switch (level) {
|
|
21
|
+
case 'error':
|
|
22
|
+
return 'red';
|
|
23
|
+
case 'warn':
|
|
24
|
+
return 'yellow';
|
|
25
|
+
case 'info':
|
|
26
|
+
return 'green';
|
|
27
|
+
case 'debug':
|
|
28
|
+
return 'cyan';
|
|
29
|
+
default:
|
|
30
|
+
return 'reset';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function getLatencyColor(latencyMs) {
|
|
34
|
+
if (!latencyMs)
|
|
35
|
+
return 'reset';
|
|
36
|
+
if (latencyMs > 1000)
|
|
37
|
+
return 'red'; // > 1s = too slow
|
|
38
|
+
if (latencyMs > 300)
|
|
39
|
+
return 'yellow'; // > 300ms = moderate
|
|
40
|
+
return 'green'; // fast
|
|
41
|
+
}
|
|
42
|
+
function formatTimestamp(timestamp) {
|
|
43
|
+
const date = new Date(timestamp);
|
|
44
|
+
return date.toISOString();
|
|
45
|
+
}
|
|
4
46
|
function formatLog(entry) {
|
|
5
|
-
|
|
47
|
+
const timestamp = colorize(formatTimestamp(entry.timestamp), 'gray');
|
|
48
|
+
const level = colorize(entry.level.toUpperCase().padEnd(5), getLevelColor(entry.level));
|
|
49
|
+
const service = colorize(`[${entry.serviceName}]`, 'cyan');
|
|
50
|
+
const message = colorize(entry.message, 'bright');
|
|
51
|
+
let output = `${timestamp} ${level} ${service}`;
|
|
52
|
+
if (entry.requestId) {
|
|
53
|
+
output += ` ${colorize(`reqId:${entry.requestId.slice(0, 8)}`, 'magenta')}`;
|
|
54
|
+
}
|
|
55
|
+
output += ` ${message}`;
|
|
56
|
+
if (entry.fields) {
|
|
57
|
+
const fields = entry.fields;
|
|
58
|
+
// Special handling for HTTP request logs
|
|
59
|
+
if (fields.method && fields.path) {
|
|
60
|
+
output += `\n ${colorize('→', 'gray')} ${colorize(fields.method, 'blue')} ${fields.path}`;
|
|
61
|
+
if (fields.statusCode) {
|
|
62
|
+
const statusColor = fields.statusCode >= 500
|
|
63
|
+
? 'red'
|
|
64
|
+
: fields.statusCode >= 400
|
|
65
|
+
? 'yellow'
|
|
66
|
+
: 'green';
|
|
67
|
+
output += ` ${colorize(`[${fields.statusCode}]`, statusColor)}`;
|
|
68
|
+
}
|
|
69
|
+
if (fields.latencyMs !== undefined) {
|
|
70
|
+
const latencyColor = getLatencyColor(fields.latencyMs);
|
|
71
|
+
output += ` ${colorize(`${fields.latencyMs}ms`, latencyColor)}`;
|
|
72
|
+
}
|
|
73
|
+
if (fields.dbTimeMs !== undefined) {
|
|
74
|
+
const dbColor = getLatencyColor(fields.dbTimeMs);
|
|
75
|
+
output += ` ${colorize(`(db: ${fields.dbTimeMs}ms)`, dbColor)}`;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Special handling for metrics flush
|
|
79
|
+
else if (fields.metrics && Array.isArray(fields.metrics)) {
|
|
80
|
+
output += `\n ${colorize('→', 'gray')} Flushed ${colorize(String(fields.count), 'yellow')} metrics`;
|
|
81
|
+
fields.metrics.forEach((metric, idx) => {
|
|
82
|
+
if (metric.type === 'http_request') {
|
|
83
|
+
const latencyColor = getLatencyColor(metric.latencyMs);
|
|
84
|
+
const statusColor = metric.statusCode >= 500
|
|
85
|
+
? 'red'
|
|
86
|
+
: metric.statusCode >= 400
|
|
87
|
+
? 'yellow'
|
|
88
|
+
: 'green';
|
|
89
|
+
output += `\n ${colorize(`${idx + 1}.`, 'gray')} ${colorize(metric.method, 'blue')} ${metric.path} `;
|
|
90
|
+
output += `${colorize(`[${metric.statusCode}]`, statusColor)} `;
|
|
91
|
+
output += `${colorize(`${metric.latencyMs}ms`, latencyColor)}`;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
// Generic field display
|
|
96
|
+
else {
|
|
97
|
+
const fieldEntries = Object.entries(fields);
|
|
98
|
+
if (fieldEntries.length > 0) {
|
|
99
|
+
output +=
|
|
100
|
+
'\n ' +
|
|
101
|
+
fieldEntries
|
|
102
|
+
.map(([key, value]) => {
|
|
103
|
+
let valueColor = 'reset';
|
|
104
|
+
// Color numeric values based on size
|
|
105
|
+
if (typeof value === 'number') {
|
|
106
|
+
valueColor = getLatencyColor(value);
|
|
107
|
+
}
|
|
108
|
+
return `${colorize(key, 'gray')}: ${colorize(String(value), valueColor)}`;
|
|
109
|
+
})
|
|
110
|
+
.join(', ');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return output;
|
|
6
115
|
}
|