refactor(logging): replace console.* with structured logger across server

Add server/logger.js - zero-dependency, level-based logger that outputs
JSON in production and human-readable format in development. Controlled
via LOG_LEVEL env var (debug/info/warn/error, default: info).

Replaces all 100 console.log/warn/error calls in 14 server files.
This commit is contained in:
Ulas
2026-04-03 22:05:22 +02:00
parent 5b1e6915ac
commit 3b90074723
16 changed files with 185 additions and 100 deletions
+40
View File
@@ -0,0 +1,40 @@
/**
* Modul: Logger
* Zweck: Levelbasiertes strukturiertes Logging ohne externe Dependencies.
* Ausgabe als JSON in Production, lesbar in Development.
* Steuerung: LOG_LEVEL env var (debug, info, warn, error). Default: info.
*/
'use strict';
const LEVELS = { debug: 0, info: 1, warn: 2, error: 3 };
const currentLevel = LEVELS[process.env.LOG_LEVEL] ?? LEVELS.info;
const isProduction = process.env.NODE_ENV === 'production';
function emit(level, mod, msg, extra) {
if (LEVELS[level] < currentLevel) return;
if (isProduction) {
const entry = { ts: new Date().toISOString(), level, mod, msg };
if (extra !== undefined) entry.extra = extra;
process.stdout.write(JSON.stringify(entry) + '\n');
} else {
const prefix = `[${mod}]`;
if (extra !== undefined) {
console[level === 'debug' ? 'log' : level](prefix, msg, extra);
} else {
console[level === 'debug' ? 'log' : level](prefix, msg);
}
}
}
function createLogger(mod) {
return {
debug: (msg, extra) => emit('debug', mod, msg, extra),
info: (msg, extra) => emit('info', mod, msg, extra),
warn: (msg, extra) => emit('warn', mod, msg, extra),
error: (msg, extra) => emit('error', mod, msg, extra),
};
}
module.exports = { createLogger };