winston - not able to log the console message to file

Viewed 322

I want to log all of the messages from Console using Winston,
and use winston-daily-rotate-file to daily store and remove the logs.

In Console:

{"level":"info","message":"Feathers application started on localhost","timestamp":"5/26/2021, 10:44:36 AM"}
You will see this message every 60 second
You will see this message every 60 second
You will see this message every 60 second

When I view the log file application-2021-05-26.log,

{"level":"info","message":"Feathers application started on localhost","timestamp":"5/26/2021, 10:44:36 AM"}

You will see this message every 60 second is missing in the log file.
What's wrong here?

app.js

var CronJob = require('cron').CronJob;
const moment = require('moment');

new CronJob('10 * * * * *', function() {
  console.log('You will see this message every 60 second');
}, null, true, 'Asia/Singapore');

logger.js

const { createLogger, format, transports } = require('winston');
const winston = require('winston');
require('winston-daily-rotate-file');

const timezoned = () => {
  return new Date().toLocaleString('en-SG', {
      timeZone: 'Asia/Singapore'
  });
}

const logger = createLogger({
  level: 'debug',
  format: format.combine(
    format.splat(),
    format.simple(),
    format.json()
  ),
  transports: [
    new transports.Console(),
    new winston.transports.DailyRotateFile({
      filename: 'application-%DATE%.log',
      dirname: "logs",
      datePattern: 'YYYY-MM-DD',
      zippedArchive: true,
      maxSize: '20m',
      maxFiles: '2d',
      format: winston.format.combine(
        winston.format.timestamp({ format: timezoned }),
        winston.format.json(),
      ),
    })
  ],
});

module.exports = logger;

1 Answers

You are not using the logger.log function inside your CronJob definition.

Please check out this Minimum Viable Example to see how you should configure your codebase to see the logs both on your console and on your file.

When you run the example (using node index.js), the script will emit logs to the console and a local file inside the project's root.

Here is the entire script for reference:

const { CronJob } = require('cron');
const { createLogger, format, transports } = require('winston');
const moment = require('moment');
require('winston-daily-rotate-file');

const timezoned = () =>
  new Date().toLocaleDateString('en-SG', {
    timeZone: 'Asia/Singapore'
  });

const logger = createLogger({
  level: 'debug',
  format: format.combine(format.splat(), format.simple(), format.json()),
  transports: [
    new transports.Console(),
    new transports.DailyRotateFile({
      filename: 'application-%DATE%.log',
      dirname: '.',
      datePattern: 'YYYY-MM-DD',
      zippedArchive: true,
      maxSize: '20m',
      maxFiles: '2d',
      format: format.combine(
        format.timestamp({ format: timezoned }),
        format.json()
      )
    })
  ]
});

const job = new CronJob(
  '10 * * * * *',
  () => logger.debug("You'll see this message every 60 seconds"),
  null,
  null,
  'Asia/Singapore'
);

job.start();
Related