This commit is contained in:
seb
2026-08-01 01:36:54 +02:00
parent c77337507f
commit 60d623f7df
2 changed files with 23 additions and 8 deletions

View File

@@ -1,7 +1,29 @@
import chalk from 'chalk';
const BINARY_PREVIEW_BYTES = 16;
function summarizeBuffer(buf) {
const head = buf.subarray(0, BINARY_PREVIEW_BYTES);
const hex = [...head].map((b) => b.toString(16).padStart(2, '0')).join(' ');
return {
type: 'Buffer',
length: buf.length,
headHex: hex,
};
}
function safeJson(value) {
try {
if (Buffer.isBuffer(value)) {
return JSON.stringify(summarizeBuffer(value), null, 2);
}
if (
Array.isArray(value) &&
value.length > 64 &&
value.every((n) => Number.isInteger(n) && n >= 0 && n <= 255)
) {
return JSON.stringify(summarizeBuffer(Buffer.from(value)), null, 2);
}
return JSON.stringify(value, null, 2);
} catch {
return String(value);
@@ -46,6 +68,7 @@ export const loggerMiddleware = (req, res, next) => {
console.log(`${method} ${url} ${status} ${chalk.gray(`(${duration}ms)`)}`);
console.log(chalk.gray(' params:'), safeJson(req.params ?? {}));
console.log(chalk.gray(' query:'), safeJson(req.query ?? {}));
console.log(chalk.gray(' content-type:'), req.headers['content-type'] ?? '(none)');
console.log(chalk.gray(' body:'), safeJson(req.body ?? {}));
console.log(chalk.gray(' response:'), safeJson(getBody()));
});