Created
August 28, 2017 12:52
-
-
Save theburningmonk/e4a785acfbdd8a7043211bf741a94bc1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const reqContext = require('./requestContext'); | |
function getContext () { | |
// note: this is a shallow copy | |
return Object.assign({}, reqContext.get()); | |
} | |
function isDebugEnabled () { | |
// disable debug logging by default, but allow override via env variables | |
// or if enabled via forwarded request context | |
return process.env.DEBUG_LOG === 'true' || reqContext.get()["Debug-Log-Enabled"] === 'true'; | |
} | |
function log (level, msg, params) { | |
if (level === 'DEBUG' && !isDebugEnabled()) { | |
return; | |
} | |
let logMsg = getContext(); | |
logMsg.level = level; | |
logMsg.message = msg; | |
logMsg.params = params; | |
console.log(JSON.stringify(logMsg)); | |
} | |
module.exports.debug = (msg, params) => log('DEBUG', msg, params); | |
module.exports.info = (msg, params) => log('INFO', msg, params); | |
module.exports.warn = (msg, params) => log('WARN', msg, params); | |
module.exports.error = (msg, params) => log('ERROR', msg, params); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment