title: How to suppress application logging messages from a node.js application when running unit tests?
body: <p>While unit-testing my node.js application (which is basically a REST backend) using mocha and supertest, I need only the test-specific message on the screen, but the stdout is also cluttered with application log messages.</p>
  
  <p>I start the unit test with:</p>
  
  <pre><code>mocha -R spec .
  </code></pre>
  
  <p>... and get this output (this is what it <em>should not</em> be):</p>
  
  <pre><code>[App] Listening on port 3000 ...
  [App] Starting app, hooray!
  
    Project API
      GET /projects
  [App] entering "projects" module ...
        √ should return an array of projects (317ms)
  </code></pre>
  
  <p>I marked the application log message with [App]. What I <em>really want</em> would be this output from the unit test:</p>
  
  <pre><code>  Project API
      GET /projects
        √ should return an array of projects (317ms)
  </code></pre>
  
  <p>How can I suppress console.log/warn/error output by the application interspersed with Mocha's reporter output?</p>
  
  <p><strong>SOLUTION:</strong></p>
  
  <p>Following dankohn's approach, I ended up like this, which solves my issue (using <a href="http://github.com/flatiron/winston">winston</a> for logging):</p>
  
  <p>(in node's "main" server file, server.js:)</p>
  
  <pre><code>if (process.env.NODE_ENV !== 'test') {
      logger = new (winston.Logger)({
          transports: [
              new (winston.transports.Console)(),
              new (winston.transports.File)({ filename: 'foo.log' })
          ]
      });
  } else {
      // while testing, log only to file, leaving stdout free for unit test status messages
      logger = new (winston.Logger)({
          transports: [
              new (winston.transports.File)({ filename: 'foo.log' })
          ]
      });
  }
  </code></pre>
  
  <p>... and to set the env variable, each unit test file starts with:</p>
  
  <pre><code>process.env.NODE_ENV = 'test';
  </code></pre>