Files
Online-Topup-V1/application/helpers/stdout-log.js
T
Mohamad Ihsan 3902a74d6f Align transaction and log timestamps with Jakarta time
Keep recovered Load Balance writes and card audit atomic, and isolate Payment Recon tests.
2026-11-24 22:57:32 +07:00

56 lines
1.7 KiB
JavaScript

const { Counter } = require('prom-client');
const WIB_OFFSET_MS = 7 * 60 * 60 * 1000;
const skipped = new Counter({
name: 'topuponline_stdout_copies_skipped_total',
help: 'Log copies skipped while stdout was unavailable or under backpressure',
labelNames: ['reason'],
});
function createStdoutWriter(stream, onSkip) {
let failed = false;
stream.on('error', () => { failed = true; });
return (line) => {
// Arsip atau log file sudah menjadi salinan lokal. Jangan menambah antrean stdout.
if (failed || !stream.writable) {
onSkip('unavailable');
return;
}
if (stream.writableNeedDrain) {
onSkip('backpressure');
return;
}
try {
stream.write(`${line}\n`, (error) => {
if (error) onSkip('unavailable');
});
} catch {
failed = true;
onSkip('unavailable');
}
};
}
const writeStdoutLine = createStdoutWriter(process.stdout, (reason) => skipped.inc({ reason }));
function formatLog(event, time = Date.now()) {
const safeEvent = { ...event };
if (typeof safeEvent.message === 'string') {
safeEvent.message = safeEvent.message
.replace(/\bBearer\s+[\w.+/=-]+/gi, 'Bearer [redacted]')
.replace(/(["']?(?:password|passwd|pin|token|authorization|card[_ ]?number|uid|cvv|nik|requestData|responseData)["']?\s*[:=]\s*)(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[^\s,}\]]+)/gi, '$1[redacted]')
.slice(0, 512);
}
return JSON.stringify({
// Bentuk timestamp WIB dari epoch supaya hasilnya sama di semua host.
timestamp: new Date(time + WIB_OFFSET_MS).toISOString().replace(/Z$/, '+07:00'),
app: 'topuponline',
pid: process.pid,
...safeEvent,
});
}
module.exports = { createStdoutWriter, writeStdoutLine, formatLog, stdoutMetrics: [skipped] };