lazywithclass / winston-cloudwatch

Send logs to Amazon Cloudwatch using Winston.
MIT License
260 stars 105 forks source link

CloudWatch LogGroups Not Getting Created #135

Closed arcsoftech closed 3 years ago

arcsoftech commented 4 years ago

My lambda function is not attached to any VPC, Still It is not posting logs to cloudwatch.

Here is my code.

 var exports = (module.exports = {});

const winston = require('winston');
const { createLogger, transports, format, error } = require('winston');
const WinstonCloudwatch = require('winston-cloudwatch');
const loggerFormat = format.combine(
  format.errors({ stack: true }),
  format.timestamp(),
  format.metadata(),
  format.json()
)

const consoleFormat = {
  format: format.combine(
    format.splat(),
    format.simple(),
    format.errors({ stack: true })
  ),
  level: 'info',
  handleExceptions: true,
  json: false,
  colorize: true
}
      let S3StreamLogger = require('s3-streamlogger-daily').S3StreamLogger;
    let date = new Date().toISOString().split('T')[0];
    let s3_stream_access = new S3StreamLogger({
      bucket: process.env.LOG_BUCKET,
      folder: userID,
      rotate_every: 'day',
      name_format: '%Y-%m-%d-%H-%M-keylogs.log'
    });
const logger = winston.createLogger({
  level: 'info',
  format: loggerFormat,
  transports: [
    //
    // - Write to all logs with level `info` and below to `combined.log` 
    // - Write all logs error (and below) to `error.log`.
    //
    new transports.Stream({
                stream: s3_stream_access,
                level: 'error'
              }),
    new WinstonCloudwatch({
          logGroupName: 'stepp-application-logs',
          exitOnError: false,
          logStreamName: `${date}/test`,
          createLogGroup: true,
          createLogStream: true,
          awsAccessKeyId : process.env.AWS_ACCESS_KEY_ID,
          awsSecretKey: process.env.AWS_SECRET_ACCESS_KEY,
          awsRegion: process.env.AWS_REGION,
          jsonMessage: false,
          awsOptions: {
            logStreamName: process.env.AWS_REGION
          },
          retentionInDays: 365,
          errorHandler: (err) => {
            console.log(err);
          },
          level: 'info'
        }),
    new winston.transports.Console(consoleFormat)
  ]
})

winston.add(logger)
winston.debug('Hey man, I am here!')

exports.logger = logger; 
lazywithclass commented 4 years ago

Did you manage to solve this?

arcsoftech commented 4 years ago

No. It did not work. I had to use this package "winston-aws-cloudwatch" to make it work.

lazywithclass commented 4 years ago

Ok I see. I will close this ticket then, unless you're willing to give more details, because this module is working on AWS Lambda for other users.

TripCreighton commented 3 years ago

+1 to @arcsoftech

This package does not work out of the box with cloudwatch in a node environment.

Does it assume that the stream and group have already been created? If so, that should be explained in the read me.

lazywithclass commented 3 years ago

It does not assume that stream / group have already been created, if it doesn't find them, it will create them.