Question

I'm new to node.js - is there a way to git push to say my Heroku production environment in a way that remove console.log statements? Or is it okay to leave them in?

What's the right way to handle local code full of debugging statements, without losing the statements but without pushing them to production.

I'm developing locally.

Thanks for your expertise.

Was it helpful?

Solution

I create a log helper file. I then use a debugMode field in my config.json or environment variables, and use nconf npm to read from this.

The below is an example of the log.coffee file

##
# * console logging for the application

nconf = require('nconf')
nconf.argv().env().file file: "./config.json"

##
# log - log information to the console 
#   params:
#     code - DEBUG, ERROR, WARNING, INFO
#     message - the log message to display
#     data - optional data info
#   returns: nothing
module.exports.log = (code, message, data) ->
  if nconf.get('DEBUG_MODE') == 'true' || nconf.get('DEBUG_MODE') == true
    if data?
      # if the data is an object, stringify it for output
      if typeof data == 'object'
        data = JSON.stringify(data)

      console.log code.toUpperCase() + ': ' + message
      console.log code.toUpperCase() + ': ' + '(continued) ' + data
    else
      console.log code.toUpperCase() + ': ' + message

Then in any file that I want to log, I simply include this file and log to it, rather than using console.log everywhere.

sample.coffee

logger = require('../log')

module.exports.getEvent = (eventDb) ->
  logger.log 'debug', '/server/adapters/event/getEvent called', eventDb

The logger allows for a severity code, message, and optionally data that will show up in the terminal, and if you are not in debug mode, it will simply not do anything.

OTHER TIPS

Is this an option? https://npmjs.org/package/remove-console-logs

Per the docs:

This is just a small tool which parses your JS and replaces any console.* calls with 0. It uses falafel to walk the AST. Falafel uses esprima to parse your JS. Finally, optimist is used for cli argument parsing

The log statements deserve different treatment depending on the case.

Debug logging to throw away

Some console.log() statements are truly temporary and don't need to remain. To catch and remove those, use:

git commit -vp

By ending the -p for --patch mode, you'll get to interactively review your patches before committing them. There are options for each hunk to split it to smaller hunks, choose not to commit it, or edit the hunk to remove a console.log. After you've committed, you may still have some of debugging code left over that you want to get rid of. You can also use:

git checkout -p

To interactively remove hunks in the same way.

Debug logging to keep

First, use a config system that supports multiple environments, like node-config.

Second, make your own small logging service that wraps a logging service that supports logging levels, like bunyan or winston. (I use bunyan). The primary thing your tiny log service will do is to wrap the third-party log service and set your own configuration defaults.

Configure your local development with a default logging level of debug. Configure your production environment with a default log level of info.

Now the logging system will generate debug logging locally, but not in production.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top