mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 11:06:28 +00:00
35 lines
1.0 KiB
JavaScript
35 lines
1.0 KiB
JavaScript
const { Counter } = require('prom-client');
|
|
const createAuditArchive = require('./audit-archive');
|
|
const { formatLog, writeStdoutLine } = require('./stdout-log');
|
|
require('dotenv').config();
|
|
|
|
const auditErrors = new Counter({
|
|
name: 'topuponline_audit_write_errors_total',
|
|
help: 'Failed local audit archive writes',
|
|
});
|
|
const archive = createAuditArchive(process.env.TOPUP_AUDIT_DIR || './lokiLog/audit');
|
|
let archiveUnavailable = false;
|
|
|
|
function record(event) {
|
|
const time = Date.now();
|
|
const line = formatLog(event, time);
|
|
try {
|
|
archive.append(line, time);
|
|
archiveUnavailable = false;
|
|
} catch (error) {
|
|
auditErrors.inc();
|
|
if (!archiveUnavailable) {
|
|
writeStdoutLine(formatLog({
|
|
level: 'error',
|
|
event: 'audit_archive_error',
|
|
message: 'Unable to write the local audit archive',
|
|
error_code: error.code || 'UNKNOWN',
|
|
}, time));
|
|
}
|
|
archiveUnavailable = true;
|
|
}
|
|
writeStdoutLine(line);
|
|
}
|
|
|
|
module.exports = { record, auditMetrics: [auditErrors] };
|