BugFixes
This commit is contained in:
@@ -1,15 +1,20 @@
|
||||
/**
|
||||
* Structured logging for email worker with daily rotation
|
||||
* Structured logging for email worker with daily rotation AND retention
|
||||
*
|
||||
* Uses pino for high-performance JSON logging.
|
||||
* Console output is human-readable via pino-pretty in dev,
|
||||
* and JSON in production (for Docker json-file driver).
|
||||
*
|
||||
* File logging uses a simple daily rotation approach.
|
||||
* Includes logic to delete logs older than X days.
|
||||
*/
|
||||
|
||||
import pino from 'pino';
|
||||
import { existsSync, mkdirSync, createWriteStream, type WriteStream } from 'node:fs';
|
||||
import {
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
createWriteStream,
|
||||
type WriteStream,
|
||||
readdirSync,
|
||||
statSync,
|
||||
unlinkSync
|
||||
} from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -17,9 +22,10 @@ import { join } from 'node:path';
|
||||
// ---------------------------------------------------------------------------
|
||||
const LOG_DIR = '/var/log/email-worker';
|
||||
const LOG_FILE_PREFIX = 'worker';
|
||||
const RETENTION_DAYS = 14; // Logs älter als 14 Tage löschen
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// File stream (best-effort, never crashes the worker)
|
||||
// File stream & Retention Logic
|
||||
// ---------------------------------------------------------------------------
|
||||
let fileStream: WriteStream | null = null;
|
||||
let currentDateStr = '';
|
||||
@@ -28,15 +34,63 @@ function getDateStr(): string {
|
||||
return new Date().toISOString().slice(0, 10); // YYYY-MM-DD
|
||||
}
|
||||
|
||||
/**
|
||||
* Löscht alte Log-Dateien basierend auf RETENTION_DAYS
|
||||
*/
|
||||
function cleanUpOldLogs(): void {
|
||||
try {
|
||||
if (!existsSync(LOG_DIR)) return;
|
||||
|
||||
const files = readdirSync(LOG_DIR);
|
||||
const now = Date.now();
|
||||
const maxAgeMs = RETENTION_DAYS * 24 * 60 * 60 * 1000;
|
||||
|
||||
for (const file of files) {
|
||||
// Prüfen ob es eine unserer Log-Dateien ist
|
||||
if (!file.startsWith(LOG_FILE_PREFIX) || !file.endsWith('.log')) continue;
|
||||
|
||||
const filePath = join(LOG_DIR, file);
|
||||
try {
|
||||
const stats = statSync(filePath);
|
||||
const ageMs = now - stats.mtimeMs;
|
||||
|
||||
if (ageMs > maxAgeMs) {
|
||||
unlinkSync(filePath);
|
||||
// Einmalig auf stdout loggen, damit man sieht, dass aufgeräumt wurde
|
||||
process.stdout.write(`[INFO] Deleted old log file: ${file}\n`);
|
||||
}
|
||||
} catch (err) {
|
||||
// Ignorieren, falls Datei gerade gelöscht wurde oder Zugriff verweigert
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
process.stderr.write(`[WARN] Failed to clean up old logs: ${err}\n`);
|
||||
}
|
||||
}
|
||||
|
||||
function ensureFileStream(): WriteStream | null {
|
||||
const today = getDateStr();
|
||||
|
||||
// Wenn wir bereits einen Stream für heute haben, zurückgeben
|
||||
if (fileStream && currentDateStr === today) return fileStream;
|
||||
|
||||
try {
|
||||
if (!existsSync(LOG_DIR)) mkdirSync(LOG_DIR, { recursive: true });
|
||||
|
||||
// Wenn sich das Datum geändert hat (oder beim ersten Start): Aufräumen
|
||||
if (currentDateStr !== today) {
|
||||
cleanUpOldLogs();
|
||||
}
|
||||
|
||||
// Alten Stream schließen, falls vorhanden
|
||||
if (fileStream) {
|
||||
fileStream.end();
|
||||
}
|
||||
|
||||
const filePath = join(LOG_DIR, `${LOG_FILE_PREFIX}.${today}.log`);
|
||||
fileStream = createWriteStream(filePath, { flags: 'a' });
|
||||
currentDateStr = today;
|
||||
|
||||
return fileStream;
|
||||
} catch {
|
||||
// Silently continue without file logging (e.g. permission issue)
|
||||
@@ -55,12 +109,10 @@ const logger = pino({
|
||||
},
|
||||
},
|
||||
timestamp: pino.stdTimeFunctions.isoTime,
|
||||
// In production Docker we write plain JSON to stdout;
|
||||
// pino-pretty can be used during dev via `pino-pretty` pipe.
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Log level mapping (matches Python worker levels)
|
||||
// Log level mapping
|
||||
// ---------------------------------------------------------------------------
|
||||
type LogLevel = 'DEBUG' | 'INFO' | 'WARNING' | 'ERROR' | 'CRITICAL' | 'SUCCESS';
|
||||
|
||||
@@ -74,7 +126,7 @@ const LEVEL_MAP: Record<LogLevel, keyof pino.Logger> = {
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Public API – mirrors Python's log(message, level, worker_name)
|
||||
// Public API
|
||||
// ---------------------------------------------------------------------------
|
||||
export function log(
|
||||
message: string,
|
||||
@@ -84,15 +136,15 @@ export function log(
|
||||
const prefix = level === 'SUCCESS' ? '[SUCCESS] ' : '';
|
||||
const formatted = `[${workerName}] ${prefix}${message}`;
|
||||
|
||||
// Pino
|
||||
// Pino (stdout/json)
|
||||
const method = LEVEL_MAP[level] ?? 'info';
|
||||
(logger as any)[method](formatted);
|
||||
|
||||
// File (best-effort)
|
||||
// File (plain text)
|
||||
const stream = ensureFileStream();
|
||||
if (stream) {
|
||||
const ts = new Date().toISOString().replace('T', ' ').slice(0, 19);
|
||||
const line = `[${ts}] [${level}] [${workerName}] ${prefix}${message}\n`;
|
||||
stream.write(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user