Skip to content

Commit

Permalink
chore(context): add logger context
Browse files Browse the repository at this point in the history
  • Loading branch information
wisley7l committed Sep 5, 2024
1 parent 0b2bf09 commit af3f843
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
35 changes: 35 additions & 0 deletions functions/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const _logger = require('firebase-functions/logger')
const { AsyncLocalStorage } = require('node:async_hooks')

const asyncLocalStorage = new AsyncLocalStorage()

const createExecContext = (next) => {
return (...args) => asyncLocalStorage.run({ execId: `${Date.now() + Math.random()}` }, () => next(...args))
}

const log = (level, msg, d) => {
const execId = asyncLocalStorage.getStore()?.execId
if (execId) {
if (d) d.execId = execId
else d = { execId }
}
return _logger[level](msg, d)
}

const logger = {
info (msg, d) {
return log('info', msg, d)
},
warn (msg, d) {
return log('warn', msg, d)
},
error (msg, d) {
return log('error', msg, d)
}
}

module.exports = {
asyncLocalStorage,
createExecContext,
logger
}
3 changes: 2 additions & 1 deletion functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const { app, procedures } = require('./ecom.config')
// handle app authentication to Store API
// https://github.com/ecomplus/application-sdk
const { ecomServerIps, setup } = require('@ecomplus/application-sdk')
const { createExecContext } = require('./context')

server.use(bodyParser.urlencoded({ extended: false }))
server.use(bodyParser.json())
Expand Down Expand Up @@ -132,7 +133,7 @@ recursiveReadDir(routesDir).filter(filepath => filepath.endsWith('.js')).forEach

server.use(router)

exports[functionName] = functions.https.onRequest(server)
exports[functionName] = functions.https.onRequest(createExecContext(server))
console.log(`-- Starting '${app.title}' E-Com Plus app with Function '${functionName}'`)

// schedule update tokens job
Expand Down
5 changes: 3 additions & 2 deletions functions/lib/pagarme/payment-subscriptions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { logger } = require('../../context')
const axios = require('./axios-create')
const { parseAddress } = require('./parses-utils')

Expand Down Expand Up @@ -146,7 +147,7 @@ const createSubscription = async (params, appData, storeId, plan, customer) => {
}
pagarmeSubscription.service_referer_name = partnerId

console.log('> Subscription: ', JSON.stringify(pagarmeSubscription))
logger.info(`> Subscription: ${JSON.stringify(pagarmeSubscription)}`)

return pagarmeAxios.post(
'/subscriptions',
Expand Down Expand Up @@ -238,7 +239,7 @@ const createPayment = async (params, appData, storeId, customer) => {
pagarmeOrder.payments = [payment]
pagarmeOrder.service_referer_name = partnerId

console.log('> Order PagarMe: ', JSON.stringify(pagarmeOrder))
logger.info(`> Order PagarMe: ${JSON.stringify(pagarmeOrder)}`)

return pagarmeAxios.post(
'/orders',
Expand Down

0 comments on commit af3f843

Please sign in to comment.