lazywithclass / winston-cloudwatch

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

Metadata not displayed in cloudwatch #173

Open edenmydev opened 2 years ago

edenmydev commented 2 years ago

I can't see the metada sent with wintson in Cloudwatch. This is my file config:

const winston = require("winston");
const WinstonCloudWatch = require("winston-cloudwatch");
const config = require("./config");
require("winston-daily-rotate-file");

const myFormat = winston.format.printf(({
  level, message, timestamp, ...metadata
}) => {
  let msg = `${timestamp} [${level}] : ${message} `;
  if (metadata) {
    msg += JSON.stringify(metadata);
  }
  return msg;
});

const logger = winston.createLogger({
  level: config.env === "development" ? "debug" : "info",
  format: winston.format.combine(
    enumerateErrorFormat(),
    config.env === "development" ? winston.format.colorize() : winston.format.uncolorize(),
    winston.format.splat(),
    winston.format.timestamp(),
    myFormat,
  ),
  transports: [
    new winston.transports.Console({
      stderrLevels: ["error"],
    }),
    new winston.transports.DailyRotateFile({
      filename: "info-%DATE%.log",
      dirname: "./logs",
      datePattern: "YYYY-MM-DD",
      zippedArchive: true,
      maxSize: "20m",
      maxFiles: "30d",
    }),
  ]

});

if (config.env === "production" || config.env === "development") {
  const cloudwatchConfig = {
    name: config.cloudwatch.logName,
    logGroupName: config.cloudwatch.groupName,
    logStreamName: config.cloudwatch.streamName,
    awsAccessKeyId: config.cloudwatch.accessKey,
    awsSecretKey: config.cloudwatch.secretAccessKey,
    awsRegion: config.cloudwatch.region,
    retentionInDays: config.cloudwatch.retentionInDays,
     **messageFormatter: ({ level, message, additionalInfo }) =>
    `[${level}] : ${message} \nAdditional Info: ${JSON.stringify(additionalInfo)}`,**
  };
  logger.add(new WinstonCloudWatch(cloudwatchConfig));
}

For example: logger.error("getCardStatus - This card is not shared", {cardId:1});

will only print on Cloudwatch: info - getCardStatus - This card is not shared.

mishra011 commented 2 years ago

Hi I am facing the same issue

mishra011 commented 2 years ago

`const logger = winston.createLogger({ transports: [ new CloudWatchTransport({ logGroupName: 'test-group', // REQUIRED logStreamName: ### 'tester-1', // REQUIRED createLogGroup: false, createLogStream: true, submissionInterval: 2000, submissionRetryCount: 1, batchSize: 20, awsConfig: { accessKeyId: CLOUDWATCH_ACCESS_KEY_ID, secretAccessKey: CLOUDWATCH_SECRET_ACCESS_KEY, region: CLOUDWATCH_REGION }}) ] })

logger.info(${variable_name}); `

mishra011 commented 2 years ago

@edenmydev try this way. It worked for me.

KalleV commented 8 months ago

It looks like the fix for this is to set jsonMessage to true or provide a custom messageFormatter in the options. Otherwise, the transport will default to only sending the "message" and "timestamp" properties via the default messageFormatter as text: https://github.com/lazywithclass/winston-cloudwatch/blob/09f8d4d3bfcef59703608e3cddfba0d6b039fd01/index.js#L26-L29