Skip to content

Commit dcd51d2

Browse files
Merge pull request #62 from call-0f-code/monitoring/logger
Add logger utility and integrate with error handling and request logging
2 parents 8284f94 + 2fe7f59 commit dcd51d2

File tree

5 files changed

+80
-12
lines changed

5 files changed

+80
-12
lines changed

bun.lock

Lines changed: 41 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@supabase/supabase-js": "^2.50.5",
5353
"@types/cors": "^2.8.19",
5454
"@types/express": "^5.0.3",
55+
"@types/morgan": "^1.9.10",
5556
"@types/multer": "^2.0.0",
5657
"@types/node": "^24.0.13",
5758
"@types/uuid": "^10.0.0",
@@ -62,10 +63,12 @@
6263
"express": "^5.1.0",
6364
"husky": "^9.1.7",
6465
"jest": "^30.0.4",
66+
"morgan": "^1.10.1",
6567
"multer": "^2.0.1",
6668
"ts-jest": "^29.4.0",
6769
"ts-node": "^10.9.2",
68-
"uuid": "^11.1.0"
70+
"uuid": "^11.1.0",
71+
"winston": "^3.19.0"
6972
},
7073
"private": true
7174
}

src/app.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ import { errorHandler } from "./utils/apiError";
77
import { createClient } from "@supabase/supabase-js";
88
import config from "./config";
99
import path from "path";
10-
10+
import { logger } from "./utils/logger";
11+
import morgan from "morgan";
1112
// Initialize Supabase client for storage operations
1213
export const supabase = createClient(
1314
config.SUPABASE_URL,
1415
config.SUPABASE_SERVICE_ROLE_KEY,
1516
);
1617

1718
const app = express();
18-
19-
19+
class LoggerStream {
20+
write(message: string) {
21+
logger.info(message.substring(0, message.lastIndexOf('\n')));
22+
}
23+
}
24+
app.use(morgan('combined', { stream: new LoggerStream()}));
2025
app.use(
2126
cors({
2227
origin: config.ALLOWED_ORIGINS || "*",

src/utils/apiError.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Request, Response, NextFunction } from "express";
2-
2+
import { logger } from "./logger";
33
/**
44
* A custom error type that carries an HTTP status code.
55
*/
@@ -40,7 +40,7 @@ export function errorHandler(
4040
error: true,
4141
message,
4242
};
43-
43+
logger.error('ERROR 🔥', err);
4444
// Include stack trace in development for debugging
4545
if (process.env.NODE_ENV === "development" && err instanceof Error) {
4646
responseBody.stack = err.stack;

src/utils/logger.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import winston from 'winston';
2+
3+
const logger = winston.createLogger({
4+
level: "http",
5+
format: winston.format.combine(
6+
winston.format.timestamp(),
7+
winston.format.json()
8+
),
9+
transports: [
10+
new winston.transports.Console({
11+
format: winston.format.combine(
12+
winston.format.colorize(),
13+
winston.format.simple()
14+
)
15+
})
16+
]
17+
});
18+
19+
logger.stream = {
20+
write: (message: any) => {
21+
logger.info(message.trim());
22+
}
23+
} as any;
24+
25+
export { logger };

0 commit comments

Comments
 (0)