From 9ae650c42eb14ee4127ce955fe0af8fa59eccb9f Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sat, 16 Nov 2024 01:30:58 +0100 Subject: [PATCH] with $HAFAS_REQ_RES_LOG_FILE, log HAFAS requests/responses to a file --- api.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/api.js b/api.js index 66fb83e..0d34dd2 100644 --- a/api.js +++ b/api.js @@ -11,6 +11,7 @@ import { createVbbHafas as createHafas, defaults as vbbHafasDefaults, } from 'vbb-hafas' +import {createWriteStream} from 'node:fs' import createHealthCheck from 'hafas-client-health-check' import Redis from 'ioredis' import {createCachedHafasClient as withCache} from 'cached-hafas-client' @@ -46,6 +47,21 @@ if (process.env.RANDOM_LOCAL_ADDRESSES_RANGE) { } } +if (process.env.HAFAS_REQ_RES_LOG_FILE) { + const hafasLogPath = process.env.HAFAS_REQ_RES_LOG_FILE + const hafasLog = createWriteStream(hafasLogPath, {flags: 'a'}) // append-only + hafasLog.on('error', (err) => console.error('hafasLog error', err)) + + customVbbProfile.logRequest = (ctx, req, reqId) => { + console.error(reqId, 'req', req.body + '') // todo: remove + hafasLog.write(JSON.stringify([reqId, 'req', req.body + '']) + '\n') + } + customVbbProfile.logResponse = (ctx, res, body, reqId) => { + console.error(reqId, 'res', body + '') // todo: remove + hafasLog.write(JSON.stringify([reqId, 'res', body + '']) + '\n') + } +} + let hafas = createHafas( // seems like `vbb-rest` is being redirected // pkg.name,