wa-dev-tools/lib/logger.js
treamz 5b528d243a Refactor: modular architecture, security, config
- Split monolithic server.js (1111 lines) into route modules
- Add .env config (port, session secret, quality, limits)
- Add SSRF protection for parser/proxy/redirect endpoints
- Add optional password auth middleware
- Add structured logger (replaces raw fs.appendFileSync)
- Add graceful shutdown with timeout
- Add extended /health endpoint (uptime, memory, pid)
- Add ecosystem.config.js for PM2 (memory limit, restart policy)
- Make compress quality configurable (was hardcoded 60)
- Expand SVG AI icons library (7 -> 20 icons)
- Add dotenv dependency
2026-03-21 22:18:54 +03:00

32 lines
963 B
JavaScript

const fs = require('fs');
const path = require('path');
const LOG_FILE = path.join(__dirname, '..', 'compress.log');
const LEVEL_PRIORITY = { error: 0, warn: 1, info: 2, debug: 3 };
const currentLevel = LEVEL_PRIORITY[process.env.LOG_LEVEL || 'info'] ?? 2;
function formatTimestamp() {
return new Date().toISOString();
}
function log(level, msg, meta) {
if ((LEVEL_PRIORITY[level] ?? 2) > currentLevel) return;
const ts = formatTimestamp();
const metaStr = meta ? ' ' + JSON.stringify(meta) : '';
const line = `[${ts}] [${level.toUpperCase()}] ${msg}${metaStr}`;
process.stdout.write(line + '\n');
}
function logToFile(text) {
const ts = formatTimestamp();
fs.appendFile(LOG_FILE, `[${ts}] ${text}\n`, () => {});
}
module.exports = {
info: (msg, meta) => log('info', msg, meta),
warn: (msg, meta) => log('warn', msg, meta),
error: (msg, meta) => log('error', msg, meta),
debug: (msg, meta) => log('debug', msg, meta),
logToFile,
};