1

I was wondering how I am suppoed to do client side logging in react ? Currently im using redux-logger to lok my redux actions and thats working great but i want to do sort of more generic logging too.

The application however gets built on a docker image and has to be as small as possible. Currently i have redux-logger as a dev dependency and wrap with an if statement to check whether or not im in production mode:

if (process.env.NODE_ENV === 'production') {
  store = createStore(rootReducer, applyMiddleware(...middlewares));
} else {
  // Get logger and redux dev tools
  const { logger } = require('redux-logger');
  const { composeWithDevTools } = require('redux-devtools-extension');
  // push the logger to middlewares to be used
  middlewares.push(logger);
  // and create the store
  store = createStore(
    rootReducer,
    composeWithDevTools(
      applyMiddleware(...middlewares)
      // other store enhancers if any
    )
  );
}

What is the best way of logging information without having to manually wrap a console.log() inside an if statement every time i want to log information ?

Looking forward to learning new approaches to logging on the client. Thank you very much !

Marius Meiners
  • 115
  • 1
  • 8
  • 1
    https://github.com/evgenyrodionov/redux-logger#log-only-in-development – ceejayoz Oct 25 '18 at 14:18
  • @ceejayoz as you can see thats exactly what i already did... Im asking if there is a way to do it differently for generic logging becuase i dont want to have to wrap every console.log in an if block – Marius Meiners Oct 25 '18 at 14:35
  • Why do you need to wrap `console.log` calls? My understanding is they'll get caught by redux-logger, and it will decide based on your settings whether or not to just silently suppress them or re-throw them? – ceejayoz Oct 25 '18 at 14:38
  • @ceejayoz i think you are misunderstanding redux-logger here. redux-logger does automatic logging. I do not have to write any console.log statements or anything, it just does everything out of the box. Im asking if there is a way to do custom and manual logging which doesnt show up in production and is, if possible a dev dependency but that i do NOT have to wrap in an if statement every time i use it. – Marius Meiners Oct 25 '18 at 14:41
  • You could override console.log with your own behavior in production: https://stackoverflow.com/questions/7042611/override-console-log-for-production – ceejayoz Oct 25 '18 at 14:45
  • @ceejayoz Thanks ! I guess thats a viable solution. However if I would like to use an npm module for logging such as debug, then i would still have to have the dependency as a non dev depenency right ? If i do have it as a dev dependency i would still need to wrap every log in an if statement or am i mistaken ? – Marius Meiners Oct 25 '18 at 14:57

0 Answers0