Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable NODE_DEBUG without environment variables?

I am trying to debug what appears to be a socket error in the node.js https library environment when running in the AWS Lambda environment (Node.js 4.3). This issue only occurs highly intermittently and only under heavy load. My team has been able to reproduce the issue consistently with a load test, and we would like to enable debug logging from the https module.

I've found in the node documentation that I can enable debug logging by setting the NODE_DEBUG=https environment variable. However, I don't believe that I can set environment variables: How can I use environmental variables on AWS Lambda?. Additionally, I don't have the ability to change the command line that Lambda uses to invoke my function.

Is there another way to create the same debug logging as setting NODE_DEBUG?

like image 737
Ryan Gross Avatar asked Aug 15 '16 20:08

Ryan Gross


People also ask

What is Node_debug?

NODE_DEBUG is used by built in util. debuglog . This is used by all nodejs builtin core modules and all the third party packages that decide to use it. DEBUG is used by debug module. So if you are using any package that is using this module for logging, then you need to use DEBUG .


2 Answers

Here's a monkeypatch:

const util    = require('util');
let debuglog  = util.debuglog;
util.debuglog = set => {
  if (set === 'https') {
    let pid = process.pid;
    return function() {
      let msg = util.format.apply(util, arguments);
      console.error('%s %d: %s', set, pid, msg);
    }
  }
  return debuglog(set);
}

// This has to occur _after_ the code above:
const https = require('https');

It basically enables debug logging for https regardless of $NODE_DEBUG. Easily rewritten to work for any module, tested with Node v4 and v6.

like image 80
robertklep Avatar answered Oct 07 '22 11:10

robertklep


I'm not familiar with Aws Lambda, but maybe you can still export the variable in the command like below:

NODE_DEBUG=https node app.js
like image 32
Slawomir Pasko Avatar answered Oct 07 '22 11:10

Slawomir Pasko