UNPKG

@hoangnam.io/qa-tools

Version:

Logging, Error handling, Notifying for Express codebase

33 lines (30 loc) 1.29 kB
const morgan = require("morgan"); const { genLogFormatFunc } = require("./log-format"); const { getLogger } = require("./logger"); function createLogMiddleware( app, { appName = "AppNameUndefined", extractCallerFunc = (req) => req.caller || null, sensitiveRoutes = ["login", "sign-up", "account", "auth", "token"], ignoreRoutes = [{ method: "GET", route: `/Some/route/that-we-don't-care` }], enableConsoleLog = true, mongoConfig = { connectionString: null, collectionName: "request_logs" }, gcProjectId, // to link with traces, spans... // to send log to gg cloud from premise, note: using process.env.GOOGLE_APPLICATION_CREDENTIALS instead of serviceAccountFilePath // serviceAccountFilePath, TZ = "GMT+0", } ) { const logFormatFunction = genLogFormatFunc({ app, appName, extractCallerFunc, sensitiveRoutes, TZ }); const logger = getLogger({ mongoConfig, enableConsoleLog, gcProjectId }); const logStream = { write: (logString) => logger.log(JSON.parse(logString)) }; return morgan(logFormatFunction, { stream: logStream, skip: (req) => ignoreRoutes.some( ({ method, route }) => req.method.toLowerCase() === method.toLowerCase() && req.url.includes(route) ), }); } module.exports = { createLogMiddleware };