logger-node
logger-node copied to clipboard
Logdna logger + serverless functions
Love the LogDNA platform so far! I have a question about some pitfalls and best practices when using the logger in combination with the serverless functions - in my particular case it's the Google Cloud Functions platform + Express.js.
The main issue that worries me is that the RE of a Cloud Function can be terminated before the background tasks are finished (see this and this, also here's a little bit more elaborate version). As per the logdna/logger-node
's best practices example in the docs, the client is optimised for high throughput, and it is advised to send a bunch of logs together instead of sending them individually. But I don't think I should rely on SIGTERM
or SIGINT
events in a serverless environment, or at least I'm not sure if it's a good practice.
Here's the best I could come up with in order to make sure the logs are getting sent to the LogDNA servers:
// utility function for sending the response
const sendJson = async (res: express.Response, json: any): Promise<void> => {
logdnaLogger.flush()
await once(logdnaLogger, 'cleared')
res.json(json) // everything after this line has no guarantee to be executed
}
// somewhere far, far away...
const router = Router()
router.get(
'/helloworld',
async (req, res) => {
const { uid } = getUser(req)
const groups = await groupsRepo.findByUserId(uid)
const sortedGroups = _(groups).sortBy(g => g.name)
// use this helper function in order to send the logs instead of res.json(sortedGroups)
await sendJson(res, sortedGroups)
})
TBF I didn't test this solution yet, but still I'd be glad to hear other opinions on the topic.