Skip to content

Commit

Permalink
with $HAFAS_REQ_RES_LOG_FILE, log HAFAS requests/responses to a file
Browse files Browse the repository at this point in the history
vbb-rest@6 equivalent: derhuerst/vbb-rest@9ae650c
  • Loading branch information
derhuerst committed Nov 16, 2024
1 parent 832b85e commit e6383df
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createDbHafas as createHafas,
defaults as dbHafasDefaults,
} from 'db-hafas'
import {createWriteStream} from 'node:fs'
import {createHafasRestApi} from 'hafas-rest-api'
import createHealthCheck from 'hafas-client-health-check'
import Redis from 'ioredis'
Expand Down Expand Up @@ -42,6 +43,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))

customDbProfile.logRequest = (ctx, req, reqId) => {
console.error(reqId, 'req', req.body + '') // todo: remove
hafasLog.write(JSON.stringify([reqId, 'req', req.body + '']) + '\n')
}
customDbProfile.logResponse = (ctx, res, body, reqId) => {
console.error(reqId, 'res', body + '') // todo: remove
hafasLog.write(JSON.stringify([reqId, 'res', body + '']) + '\n')
}
}

// todo: use process.env.HAFAS_USER_AGENT if defined
let hafas = createHafas(pkg.name, {
profile: customDbProfile,
Expand Down

0 comments on commit e6383df

Please sign in to comment.