pixelpinch-proxy / src /logger.js
Sacredeye's picture
Upload 10 files
8de1fe2 verified
Raw History Blame Contribute Delete
3.78 kB
// ============================================
// WINSTON LOGGER CONFIGURATION
// ============================================
import winston from 'winston';
// Simple, clean console format
const consoleFormat = winston.format.combine(
winston.format.timestamp({ format: 'HH:mm:ss' }),
winston.format.errors({ stack: true }),
winston.format.printf(({ timestamp, level, message, component, stack, ...meta }) => {
// Clean level indicators
const indicators = {
error: '✗',
warn: '⚠',
info: '●',
debug: '○'
};
const indicator = indicators[level] || '•';
// Build log line
let log = `${indicator} ${timestamp}`;
if (component) log += ` [${component}]`;
log += ` ${message}`;
// Add metadata on same line if small, otherwise pretty print
const metaKeys = Object.keys(meta);
if (metaKeys.length > 0) {
let metaStr;
try {
metaStr = JSON.stringify(meta, (key, value) => {
if (typeof value === 'bigint') return value.toString() + 'n';
if (typeof value === 'function') return '[Function]';
if (value && typeof value === 'object' && value.constructor === Object) {
const result = {};
for (const k in value) {
if (value.hasOwnProperty(k)) {
result[k] = value[k];
}
}
return result;
}
return value;
});
} catch (error) {
metaStr = '[Circular or unstringifiable object]';
}
if (metaStr.length < 80) {
log += ` ${metaStr}`;
} else {
log += `\n ${metaStr.split('\n').join('\n ')}`;
}
}
// Add stack trace for errors
if (stack) {
log += `\n ${stack.split('\n').join('\n ')}`;
}
return log;
})
);
// Create logger instance
const logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'debug',
format: consoleFormat,
transports: [
new winston.transports.Console({
stderrLevels: ['error', 'warn'],
// Add clear functionality
handleExceptions: true,
handleRejections: true
})
],
exceptionHandlers: [
new winston.transports.Console()
],
rejectionHandlers: [
new winston.transports.Console()
]
});
// Add clear console functionality
logger.clearConsole = function() {
try {
if (typeof process !== 'undefined' && process.stdout && process.stdout.clearLine) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
}
// Try to clear the console in different environments
if (typeof console !== 'undefined' && console.clear) {
console.clear();
}
// Alternative clear method
if (typeof process !== 'undefined') {
process.stdout.write('\x1B[2J\x1B[0f');
}
} catch (error) {
// Silent fail for console clearing
}
};
// Create specialized loggers
export const requestLogger = logger.child({ component: 'request' });
export const compressionLogger = logger.child({ component: 'compression' });
export const cacheLogger = logger.child({ component: 'cache' });
export const securityLogger = logger.child({ component: 'security' });
export default logger;