Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of console.log() #7

Open
maxkfranz opened this issue Sep 4, 2018 · 5 comments
Open

Use of console.log() #7

maxkfranz opened this issue Sep 4, 2018 · 5 comments

Comments

@maxkfranz
Copy link
Member

maxkfranz commented Sep 4, 2018

While useful for debugging, these should be removed at least in prod builds.

You can use NODE_ENV and the environment webpack plugin for this, e.g.

const log = function(){
  if(process.env.NODE_ENV === 'development'){
    console.log.apply(arguments);
  } else {
    // no logging in prod
  }
};

// in the code...
log('some debug message');

An approach like above should be used or the messages should be removed altogether.

@paul-shannon
Copy link
Contributor

@maxkfranz great tip. thanks.

@paul-shannon
Copy link
Contributor

@maxkfranz I implemented a simple version of your suggestion. Adequate? It avoids adding another npm module. Manipulation of development|production status is hard-coded, but perhaps not too awful.

var executionMode = "beta";
const log = function(msg)
{
  if(executionMode == "devel")
      console.log(msg);
}

@maxkfranz
Copy link
Member Author

maxkfranz commented Oct 16, 2018

If you're fine with hard-coding that value, that's fine. Otherwise it's best to use something like process.env.NODE_ENV === 'development' with the webpack env plugin: https://webpack.js.org/plugins/environment-plugin/

NODE_ENV is the standard environment variable, but you could use something different if that would integrate better with the other things you're building in your Makefile.

@paul-shannon
Copy link
Contributor

@maxkfranz I'll give it a try. webpack plugins are a little opaque to me at this point. Are these steps about right?

  • use npm to install webpack.EnvironmentPlugin
  • configure webpack.config.js to load that plugin by adding it to the module.exports.plugins array
  • create a make target (in ~/github/cyjShiny/browserCode/makefile) which sets and exports the bash environment variable NODE_ENV=development|prod
  • that value controls the logic of the new log function
  • I don't understand the use of arguments in your log function, without obvious prior assignment

Am I close?

@maxkfranz
Copy link
Member Author

use npm to install webpack.EnvironmentPlugin

It should be built-in

configure webpack.config.js to load that plugin by adding it to the module.exports.plugins array

yes

create a make target (in ~/github/cyjShiny/browserCode/makefile) which sets and exports the bash environment variable NODE_ENV=development|prod

yes

that value controls the logic of the new log function

yes

I don't understand the use of arguments in your log function, without obvious prior assignment

arguments is a keyword that can be used for variadic functions in js. It's an array-like object that contains all the arguments passed to the function. It's useful if your logging function accepts multiple arguments like console.log() does, e.g. console.log('this is the value of foo', foo).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants