KyleRoss/node-lambda-log

Setting Debug to false not working

wasitim opened this issue · 4 comments

Using the code in a simple node file, setting debug true/false gives expected results.

  log.debug('some debug message');
  //=> false
  
  // Enable debug messages
  log.options.debug = true;
  log.debug('some debug message again');

  log.options.debug = false;
  log.debug('some other ')

Doing the same in code that gets deployed as a lambda to AWS, the debug statements always print. I'm trying to set only info statements visible at certain stages and all statements at others.

@wasitim Can you confirm which version of lambda-log you are using in production? I'm not seeing the issue on the latest version after deploying to Lambda.

Hey Kyle,
2.1.0

Here's a code snippet

module.exports.handler = async (event) => {
  console.log(`env.debug: ${process.env.debug} - env.info: ${process.env.info}`)
  // cloudwatch output: 2019-02-01T18:08:09.815Z	env.debug: false - env.info: true
  log.options.debug = process.env.debug
  log.options.info = process.env.info

  log.debug(`session: ${JSON.stringify(event)}`)
  // cloudwatch output: 
  // . 2019-02-01T18:08:09.816Z	{"_logLevel":"debug","msg":"session: {\"resource\":\"/v1/login

Maybe my expectations are wrong. I would have thought that last line wouldn't have printed.

If you are setting the environment variable through AWS, it will be a string and not a boolean. You will need to convert it to a boolean:

if(process.env.debug === 'true') {
    log.options.debug = true;
} else if(process.env.debug === 'false') {
    log.options.debug = false;
}

Since a non-empty string will always be true, this is what is causing the debug to always be enabled.

And wow do I feel stupid now!

thanks