title: Node logging uncaught exception (Winston V3)
body: <p>I'm new to Winston, and I'm trying to use it to log uncaught exceptions and then exit. It is logging to the console, but doesn't seem to have time to log to a file before exiting</p>
  
  <p>Here is an example:</p>
  
  <p>index.js</p>
  
  <pre><code>const express = require('express');
  const app = express();
  const logger = require('./startup/logging');
  
  process.on('uncaughtException', (ex) =&gt; {
      logger.error("Won't be logged in the File");
      process.exit(1);
  });
  
  throw new Error("Uncaught");
  </code></pre>
  
  <p>logging.js</p>
  
  <pre><code>const { createLogger, format, transports } = require('winston');
  
  const logger = createLogger({     
      format: format.combine(
          format.timestamp({
              format: 'YYYY-MM-DD hh:mm:ss |'
          }),
          format.printf(info =&gt; `${info.timestamp} ${info.level}: ${info.message}`)
      ),
      transports: [
          new transports.Console({
              format: format.combine(
                  format.colorize({
                      // all: true
                  }),
                  // this is repeated because for some reason colorize() won't work otherwise
                  format.printf(info =&gt; `${info.timestamp} ${info.level}: ${info.message}`)            
              )
          }),
          new transports.File({ filename: './exceptions.log' })
      ]
  });
  
  module.exports = logger;
  </code></pre>
  
  <p>As mentioned, the uncaught exception is logged to the console, but not to the file. Removing <code>process.exit(1)</code> solves the problem of logging, but I do want the program to exit.</p>
  
  <p>Adding <code>handleExceptions: true</code> to the File transport causes the same issue - it exits prior to logging the exception. I'm assuming this is the cause, as adding <code>exitOnError: false</code> then logs the exception.</p>
  
  <p>Is this the intended behaviour? It seems strange to me, even as a beginner.</p>
  
  <p>EDIT: </p>
  
  <pre><code>process.on('uncaughtException', (ex) =&gt; {
      logger.log('error', 'Will be logged');
      setTimeout(function(){process.exit(1)}, 1000);
  });
  </code></pre>
  
  <p>This works, but surely isn't a fix, just shows the problem i'm having</p>