Search code examples
node.jswinston

winston 3.2.1 version is not creating log file


I tried to create winston(logger file) using node js.winston 2.4.0 version its sucessfully storeing info and error in log file.but latest version its created log file but cant stored error and info messages.How to fix it

Winston.js

var winston = require('winston');

const logger = winston.createLogger({
    level: 'info',
    format: winston.format.json(),
    defaultMeta: { service: 'user-service' },
    transports: [
      //
      // - Write to all logs with level `info` and below to `combined.log` 
      // - Write all logs error (and below) to `error.log`.
      //
      new winston.transports.File({ filename: 'error.log', level: 'error' }),
      new winston.transports.File({ filename: 'combined.log' })
    ]
  });
  
  //
  // If we're not in production then log to the `console` with the format:
  // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
  // 
  if (process.env.NODE_ENV !== 'production') {
    logger.add(new winston.transports.Console({
      format: winston.format.simple()
    }));
  }

app.js

/**
 * @fileoverview Root file of the application.
 */

// Import ExpressJS
const winston = require('winston');
const express = require('express');
const app = express();
// StartUp Processes
require('./startup/logging_error_startup');
require('./startup/routes_startup')(app);
require('./startup/check_security_key_startup')();
require('./startup/validation_startup')();
require('./startup/swagger_startup')(app);
require('./startup/production_startup')(app);
// Start the server
const port = process.env.PORT || 4202;
app.listen(port, () => winston.info(`Server Started and Listening on port ${port}`));

I got the output

winston] Attempt to write logs with no transports {"message":"Server Started and Listening on port 4202","level":"info"}

Solution

  • You need to import the logger object from Winston.js file into app.js. In your app.js code, the Winston import is for the npm package and not for the logger object created in Winston.js.

    In Winston.js, export the logger object:

    var winston = require('winston');
    
    const logger = winston.createLogger({
        level: 'info',
        format: winston.format.json(),
        defaultMeta: { service: 'user-service' },
        transports: [
          //
          // - Write to all logs with level `info` and below to `combined.log` 
          // - Write all logs error (and below) to `error.log`.
          //
          new winston.transports.File({ filename: 'error.log', level: 'error' }),
          new winston.transports.File({ filename: 'combined.log' })
        ]
      });
    
      //
      // If we're not in production then log to the `console` with the format:
      // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
      // 
      if (process.env.NODE_ENV !== 'production') {
        logger.add(new winston.transports.Console({
          format: winston.format.simple()
        }));
      }
    
    module.exports = logger;
    

    And in app.js, use that logger object after importing.

    const logger = require('./Winston'); //assuming Winston.js is in the same folder level as app.js
    const express = require('express');
    const app = express();
    // StartUp Processes
    require('./startup/logging_error_startup');
    require('./startup/routes_startup')(app);
    require('./startup/check_security_key_startup')();
    require('./startup/validation_startup')();
    require('./startup/swagger_startup')(app);
    require('./startup/production_startup')(app);
    // Start the server
    const port = process.env.PORT || 4202;
    app.listen(port, () => logger.info(`Server Started and Listening on port ${port}`));