lazywithclass / winston-cloudwatch

Send logs to Amazon Cloudwatch using Winston.
MIT License
258 stars 104 forks source link

The log cannot be recorded in Cloud Watch until the second log come through #213

Open lanceliumeng opened 1 year ago

lanceliumeng commented 1 year ago
const { createLogger, format, transports } = require('winston');
const { combine, timestamp,printf} = format;
const WinstonCloudWatch = require('winston-cloudwatch');
const AWS = require("aws-sdk")
AWS.config.update({ region: 'xxxxxxx'})
const cwLogs = new AWS.CloudWatchLogs()

const logGroupName = 'xxxx';
const logStreamName = `xxxxx`;

const myFormat = printf(({ level, message }) => {
  return JSON.stringify({ timestamp, level, message }) + ",";
});

const userLogger = createLogger({
    format: combine(
            timestamp({ format: 'MMM-DD-YYYY HH:mm:ss' }),
            myFormat,
    ),
    transports: [
      new WinstonCloudWatch({
        cloudWatchLogs: cwLogs,
        logGroupName,
        logStreamName,
        awsRegion:'xxxxxx',
        jsonMessage: true,
        level: 'info',
        retentionInDays: 14,
      })
    ],  
  });
  module.exports = {userLogger}

Dear Team, I tried to use above code in my Lambda function for operation logs record.

eg: in the lambda function, I used userLogger.info or userLogger.error for different level logs record, but I found the log couldn't be recored in Cloud watch real time. The Log A won't be recorded in Cloud Watch until Log B sent through, meanwhile, the Log B won't be recorded If there is no Log C.

Could you please kindly advise?

nairrajeev commented 1 year ago

Hi @lanceliumeng have you tried flushing the logs?