Winston is not writing logs to files


I made two transports for errors and warnings in Winston that supposedly writes to files. The existing transport for console logging works fine, and I did check pm2 logs and saw the logs, but the transports for files are not saving anyting.

'use strict';

const winston = require('winston');
const m = require('moment-timezone');
let logger = null;
 * Initializes the logger
 * @param {object} configLogging
module.exports.initialize = function initialize(configLogging) {
  const dateFormat = 'dddd, MMMM Do YYYY, h:mm:ss a';

  logger = new winston.Logger({
    transports: [
      new (winston.transports.Console)({
        name: 'info-console',
        level: configLogging.level,
        colorize: true,
        timestamp: function() { return m.utc().format(dateFormat); }
      new (winston.transports.File)({
        name: 'warning-file',
        filename: 'warning-file.log',
        level: 'warning'
      new (winston.transports.File)({
        name: 'error-file',
        filename: 'error-file.log',
        level: 'error'
  });'Starting logging service');

 * Gets the logger instance
 * @returns {LoggerInstance} winLogger
module.exports.get = function get() {
  return logger;


please create one log.js file and write this all code

var winston = require('winston');
const env = process.env.NODE_ENV;
const logDir = 'logs';
const fs = require('fs');

if (!fs.existsSync(logDir)) {

const now = new Date();
var logger = new(winston.Logger)({
transports: [

    new winston.transports.File({
        name: 'error-file',
        filename: './logs/exceptions.log',
        level: 'error',
        json: false

        filename: `${logDir}/-apimodules.log`,
        timestamp: now,
        datePattern: 'dd-MM-yyyy',
        prepend: true,
        json: false,
        level: env === 'development' ? 'verbose' : 'info'
exitOnError: false

module.exports = logger; = {
  write: function(message, encoding) {;
    console.log('message=', message);

For add log, use this file everywhere that need to log using this code

var logger = require('./path of/log.js');'*** Requested for First log... ***');

Answered By – kumbhani bhavesh

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.