re: What JS Logging library / tool do you use? VIEW POST

VIEW PARENT COMMENT VIEW FULL DISCUSSION
 

This is a really good idea that I had never thought of for JS. Is () => {} really a no-op or is there just less overhead? In C, you can use (void) 0 to denote a no-op which would get optimized out in the compiler, but in JS you're interpreted.

 

Please, please try ulog It is just over 1KB and it supports all this stuff out of the box. Plus it has a test suite and everything and does not have the bugs your home-brew implementation will have. I am speaking from experience. I have been developing this simple lib for years now and it is amazing how many things can (and will) go wrong in different environments when it comes to logging.

Basically I am hearing you guys re-inventing the wheel :) ulog has a simple config mechanism via env vars or localStorage that does exactly what you mention here, plus a bit more. Check it out and let me know what you think.

About the no-op thing.... It is probably just minimal overhead, not no overhead. But really if the overhead of an empty function call is an issue in your code, better look very carefully at your code because that should never be an issue.

What could be an issue is if you do this:

log.info(veryExpensiveFunctionCall())

The veryExpensiveFunctionCall() will be performed and the result passed to the no-op method... So still slow.

In ulog we fix that like this:

if (log.level >= log.INFO) {
  log.info(veryExpensiveFunctionCall())
}

The level check makes sure we only call veryExpensiveFunctionCall() if it is really needed.

This code has a weird smell. Can you give me an example of an expensive function call where the only thing you would want to do is log the output in a development environment.

It is an example. The point is that if you do expensive things to produce the arguments of a log call, the fact that the log function is a no-op does not help with performance. In such cases you need the if. The example is there to demonstrate the if.

code of conduct - report abuse