title: Outputting Logs inside JSON Object with Winston in Node.js
body: <p>So I am using <a href="https://github.com/winstonjs/winston" rel="nofollow noreferrer">winston.js</a> to log to a file. I have set up the logger to output JSON with the Winston default JSON format. </p>
  
  <p>This is my Logger configuration.</p>
  
  <p><strong>logger.util.js:</strong></p>
  
  <pre><code>'use strict';
  
  const winston = require('winston');
  const moment = require('moment');
  
  const logger = winston.createLogger({
      format: winston.format.json(),
      transports: [
          new winston.transports.File({
    filename: './logs/' + moment(moment.now()).format('DD-MM-YYYY') + '-API.json',
      handleExceptions: true
    })
   ],
  });
  
  module.exports = logger;
  </code></pre>
  
  <p>When logging an event like so:</p>
  
  <pre><code>logger.log({
    'level': 'info',
    'timestamp': moment.now(),
    'account_id': res.account_id || null,
    'action': JSON.stringify(payload),
    'request_id':  res.request_id,
    'status': success.status.code,
    'route' : res.method +  ' ' + res.route,
    'bytes': res.socket.bytesRead || null,
    'elapsed': res.elapsed || null,
  });
  </code></pre>
  
  <p>I get the following output into my log file</p>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>{
     "level": "error",
     "timestamp": 1544708669700,
     "account_id": 7,
     "action": "SequelizeDatabaseError: column \"alert\" does not exist",
     "request_id": "27cc338b-3980-4818-a9e7-83380b1b2c3a",
     "status": 500,
     "route": "POST /post/new",
     "bytes": 714,
     "elapsed": 35
  }
  {
     "level": "info",
     "timestamp": 1544709322038,
     "action": "{\"device\":{\"id\":57},\"removed\":55}",
     "status": 200,
     "route": "undefined undefined",
     "bytes": 517,
     "elapsed": null
  }
  </code></pre>
  
  <p>I want the log to out to my file like so. So the data can be searched at a later date.</p>
  
  <p><strong>Desired Output:</strong></p>
  
  <pre><code>[
    {
       "level": "error",
       "timestamp": 1544708669700,
       "account_id": 7,
       "action": "SequelizeDatabaseError: column \"alert\" does not exist",
       "request_id": "27cc338b-3980-4818-a9e7-83380b1b2c3a",
       "status": 500,
       "route": "POST /post/new",
       "bytes": 714,
       "elapsed": 35
    },
    {
       "level": "info",
       "timestamp": 1544709322038,
       "action": "{\"device\":{\"id\":57},\"removed\":55}",
       "status": 200,
       "route": "undefined undefined",
       "bytes": 517,
       "elapsed": null
    }
  ]
  </code></pre>
  
  <p>I understand I may need to create a custom format with Winston and wondered if someone could give me an example? After my research, I haven't been able to find something which is similar enough to work off and the docs don't give a similar example either.</p>