diff --git a/backend/index.js b/backend/index.js index 5103f70..1ccd6a7 100644 --- a/backend/index.js +++ b/backend/index.js @@ -90,6 +90,45 @@ app.listen(PORT, function () { console.log(`Listening on port ${PORT}`); }); +// -------------------AWS Cloudwatch Logs------------------- +import winston from 'winston'; +import WinstonCloudWatch from 'winston-cloudwatch'; + +const isProduction = process.env.NODE_ENV === 'production'; +// const isProduction = true; + +// Configure Winston logger +const logger = winston.createLogger({ + transports: [ + isProduction + ? new WinstonCloudWatch({ + logGroupName: 'snapurl', + logStreamName: 'express-server', + awsRegion: 'ap-south-1', + jsonMessage: true, + awsAccessKeyId: process.env.AWS_ACCESS_KEY_ID, + awsSecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, + }) + : new winston.transports.Console({ + format: winston.format.simple(), + }), + ], +}); + +logger.level = 'debug'; +logger.on('error', (err) => { + console.error('Error in Winston CloudWatch logger:', err); +}); + +// override console.log and console.error +console.log = function (...args) { + logger.info(args.join(' ')); +}; + +console.error = function (...args) { + logger.error(args.join(' ')); +}; + initCustomDomainJobs(); export { app }; diff --git a/backend/package.json b/backend/package.json index 7bfc46d..f0a4d74 100644 --- a/backend/package.json +++ b/backend/package.json @@ -24,6 +24,7 @@ }, "homepage": "https://github.com/DhananjayThomble/NodeJS-URL-Shortener-Microservice#readme", "dependencies": { + "aws-sdk": "^2.1691.0", "bcrypt": "^5.1.0", "bcryptjs": "^2.4.3", "body-parser": "^1.20.1", @@ -46,6 +47,8 @@ "passport-local": "^1.0.0", "random-words": "^2.0.0", "swagger-ui-express": "^4.6.0", + "winston": "^3.14.2", + "winston-cloudwatch": "^6.3.0", "yamljs": "^0.3.0" }, "type": "module",