Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata not displayed in cloudwatch #173

Open
edenmydev opened this issue Jan 25, 2022 · 4 comments
Open

Metadata not displayed in cloudwatch #173

edenmydev opened this issue Jan 25, 2022 · 4 comments

Comments

@edenmydev
Copy link

edenmydev commented Jan 25, 2022

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
Copy link

Hi I am facing the same issue

@mishra011
Copy link

`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
Copy link

@edenmydev try this way. It worked for me.

@KalleV
Copy link

KalleV commented Jan 8, 2024

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:

var messageFormatter = options.messageFormatter ? options.messageFormatter : function(log) {
return [ log.level, log.message ].join(' - ')
};
this.formatMessage = options.jsonMessage ? stringify : messageFormatter;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants