DEV Community

Discussion on: console.log(x): The way out

Collapse
 
jamesthomson profile image
James Thomson

Myself I prefer a debug function which accepts a function so anything can be run in debug mode. It's a little more verbose, but very flexible. This React env implementation has a flag for production which may or may not be desirable.

const debug = (fn: () => void) => {
  const env = process.env.REACT_APP_ENV;
  const hasFlag = window.location.search.includes('debug=true');

  if (env === 'production' && !hasFlag) return;
  if (fn && typeof fn === 'function') fn();
};

export default debug;
Enter fullscreen mode Exit fullscreen mode
debug(() => console.log('debugging'));
Enter fullscreen mode Exit fullscreen mode