Skip to content

Commit

Permalink
creating separate sentryLogger and cloudwatchLogger, tying them toget…
Browse files Browse the repository at this point in the history
…her in global.ts
  • Loading branch information
ChelseaKR committed Aug 14, 2023
1 parent 3ae022b commit 1ad31c4
Show file tree
Hide file tree
Showing 10 changed files with 6,632 additions and 4,135 deletions.
4,846 changes: 2,274 additions & 2,572 deletions backend/package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"dependencies": {
"@contentful/rich-text-types": "^16.0.3",
"@sentry/node": "^7.55.2",
"@types/dotenv": "^8.2.0",
"@types/winston": "^2.4.4",
"axios": "^1.1.3",
"cors": "^2.8.5",
"db-migrate": "^0.11.11",
Expand All @@ -43,6 +45,7 @@
"tslib": "^2.0.0",
"typescript": "^5.1.3",
"winston": "^3.10.0",
"winston-cloudwatch": "^6.2.0",
"winston-mail": "^2.0.0",
"winston-sentry": "^0.2.1",
"winston-sentry-log": "^1.0.26"
Expand All @@ -53,7 +56,7 @@
"@types/cors": "^2.8.7",
"@types/express": "^4.17.13",
"@types/jest": "^29.2.4",
"@types/node": "^16.11.56",
"@types/node": "^16.18.40",
"@types/pg": "^8.6.5",
"@types/supertest": "^2.0.9",
"@typescript-eslint/eslint-plugin": "^5.56.0",
Expand Down
20 changes: 20 additions & 0 deletions backend/src/utils/cloudwatchLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import dotenv from 'dotenv'
import { createLogger, transports, format } from "winston";
import WinstonCloudWatch from 'winston-cloudwatch';

dotenv.config()

const cloudwatchLogger = createLogger({
format: format.json(),
transports: [
new WinstonCloudWatch({
logGroupName: process.env.LOG_GROUP_NAME,
logStreamName: process.env.LOG_STREAM_NAME,
awsRegion: process.env.AWS_REGION,
createLogGroup: true,
createLogStream: true,
}),
],
});

export default cloudwatchLogger
61 changes: 51 additions & 10 deletions backend/src/utils/global.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,57 @@
import logger from './logger'
import cloudwatchLogger from './cloudwatchLogger';
import sentryLogger from './sentryLogger';

// change global console.log to winston logger type
(function overrideConsoleMethods() {
const statusDict: Array<string> = ["error", "warn", "info", "http", "verbose", "debug"];

(() => {
console.log = function (message, status = "info") {
// the available status
const status_dict = ["error", "warn", "info", "http", "verbose", "debug"];
// if status is not part of the availability, change it to info
if (!status_dict.includes(status)) {
/* We're preserving the original console.log functionality just in case there are log messages that don't fit into
the predefined statuses. This helps in making sure we don't unintentionally suppress important log messages. */
const originalConsoleLog = console.log;

console.log = function (message: string, status = "info") {
if (statusDict.indexOf(status) !== -1) {
status = "info";
}
logger.log(status, message);

switch (status) {
case "error":
cloudwatchLogger.error(message);
sentryLogger.error(message);
break;
case "warn":
cloudwatchLogger.warn(message);
break;
case "info":
cloudwatchLogger.info(message);
break;
case "http":
cloudwatchLogger.log('http', message);
break;
case "debug":
case "verbose":
cloudwatchLogger.debug(message);
break;
}
};

console.info = function (...args) {
const message = args.join(' ');
cloudwatchLogger.info(message);
};
})();

console.warn = function (...args) {
const message = args.join(' ');
cloudwatchLogger.warn(message);
};

console.error = function (...args) {
const message = args.join(' ');
cloudwatchLogger.error(message);
sentryLogger.error(message);
};

console.debug = function (...args) {
const message = args.join(' ');
cloudwatchLogger.debug(message);
};
})();
32 changes: 0 additions & 32 deletions backend/src/utils/logger.ts

This file was deleted.

40 changes: 40 additions & 0 deletions backend/src/utils/sentryLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import dotenv from 'dotenv';
import { createLogger, transports, format } from 'winston';
import WinstonCloudWatch from 'winston-cloudwatch';

dotenv.config();

// Determine log group and stream based on NODE_ENV
let logGroupName: string;
let logStreamName: string;

switch (process.env.NODE_ENV) {
case 'production':
logGroupName = process.env.PROD_LOG_GROUP_NAME || '';
logStreamName = process.env.PROD_LOG_STREAM_NAME || '';
break;
case 'test':
logGroupName = process.env.TEST_LOG_GROUP_NAME || '';
logStreamName = process.env.TEST_LOG_STREAM_NAME || '';
break;
case 'development':
default:
logGroupName = process.env.DEV_LOG_GROUP_NAME || '';
logStreamName = process.env.DEV_LOG_STREAM_NAME || '';
break;
}

const cloudwatchLogger = createLogger({
format: format.json(),
transports: [
new WinstonCloudWatch({
logGroupName: logGroupName,
logStreamName: logStreamName,
awsRegion: process.env.AWS_REGION,
createLogGroup: true,
createLogStream: true,
}),
],
});

export default cloudwatchLogger;
Loading

0 comments on commit 1ad31c4

Please sign in to comment.