feat: add SLS toggle and sanitize logs
This commit is contained in:
27
logger.js
27
logger.js
@@ -1,28 +1,29 @@
|
||||
import { isDevMode } from './config.js';
|
||||
import { sanitizeForLog, sanitizeLogMessage } from './log-sanitizer.js';
|
||||
|
||||
export function logInfo(message, data = null) {
|
||||
console.log(`[INFO] ${message}`);
|
||||
console.log(`[INFO] ${sanitizeLogMessage(message)}`);
|
||||
if (data && isDevMode()) {
|
||||
console.log(JSON.stringify(data, null, 2));
|
||||
console.log(JSON.stringify(sanitizeForLog(data), null, 2));
|
||||
}
|
||||
}
|
||||
|
||||
export function logDebug(message, data = null) {
|
||||
if (isDevMode()) {
|
||||
console.log(`[DEBUG] ${message}`);
|
||||
console.log(`[DEBUG] ${sanitizeLogMessage(message)}`);
|
||||
if (data) {
|
||||
console.log(JSON.stringify(data, null, 2));
|
||||
console.log(JSON.stringify(sanitizeForLog(data), null, 2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function logError(message, error = null) {
|
||||
console.error(`[ERROR] ${message}`);
|
||||
console.error(`[ERROR] ${sanitizeLogMessage(message)}`);
|
||||
if (error) {
|
||||
if (isDevMode()) {
|
||||
console.error(error);
|
||||
console.error(sanitizeForLog(error));
|
||||
} else {
|
||||
console.error(error.message || error);
|
||||
console.error(sanitizeLogMessage(error.message || String(error)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,16 +31,16 @@ export function logError(message, error = null) {
|
||||
export function logRequest(method, url, headers = null, body = null) {
|
||||
if (isDevMode()) {
|
||||
console.log(`\n${'='.repeat(80)}`);
|
||||
console.log(`[REQUEST] ${method} ${url}`);
|
||||
console.log(`[REQUEST] ${sanitizeLogMessage(method)} ${sanitizeLogMessage(url)}`);
|
||||
if (headers) {
|
||||
console.log('[HEADERS]', JSON.stringify(headers, null, 2));
|
||||
console.log('[HEADERS]', JSON.stringify(sanitizeForLog(headers), null, 2));
|
||||
}
|
||||
if (body) {
|
||||
console.log('[BODY]', JSON.stringify(body, null, 2));
|
||||
console.log('[BODY]', JSON.stringify(sanitizeForLog(body), null, 2));
|
||||
}
|
||||
console.log('='.repeat(80) + '\n');
|
||||
} else {
|
||||
console.log(`[REQUEST] ${method} ${url}`);
|
||||
console.log(`[REQUEST] ${sanitizeLogMessage(method)} ${sanitizeLogMessage(url)}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,10 +49,10 @@ export function logResponse(status, headers = null, body = null) {
|
||||
console.log(`\n${'-'.repeat(80)}`);
|
||||
console.log(`[RESPONSE] Status: ${status}`);
|
||||
if (headers) {
|
||||
console.log('[HEADERS]', JSON.stringify(headers, null, 2));
|
||||
console.log('[HEADERS]', JSON.stringify(sanitizeForLog(headers), null, 2));
|
||||
}
|
||||
if (body) {
|
||||
console.log('[BODY]', JSON.stringify(body, null, 2));
|
||||
console.log('[BODY]', JSON.stringify(sanitizeForLog(body), null, 2));
|
||||
}
|
||||
console.log('-'.repeat(80) + '\n');
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user