Skip to content

Commit

Permalink
Refactor logging configuration for AWS Cloudwatch
Browse files Browse the repository at this point in the history
  • Loading branch information
DhananjayThomble committed Sep 23, 2024
1 parent 65bd710 commit 5a2c1a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
39 changes: 39 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
3 changes: 3 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 5a2c1a1

Please sign in to comment.