Skip to content

Commit d18738c

Browse files
Merge pull request #63 from call-0f-code/main
adding logger to prod
2 parents 4df5d01 + dcd51d2 commit d18738c

File tree

6 files changed

+89
-15
lines changed

6 files changed

+89
-15
lines changed

.github/workflows/tests.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ name: Run Tests
22

33
on:
44
pull_request:
5-
branches:
6-
- main
75

86
jobs:
97
test:
8+
if: github.base_ref == 'main'
109
runs-on: ubuntu-latest
1110

1211
steps:
@@ -16,7 +15,7 @@ jobs:
1615
- name: Set up Bun
1716
uses: oven-sh/setup-bun@v2
1817
with:
19-
bun-version: "1.2.18" # or pin to whatever Bun version you need
18+
bun-version: "1.2.18"
2019

2120
- name: Cache Bun dependencies
2221
uses: actions/cache@v4
@@ -36,3 +35,10 @@ jobs:
3635

3736
- name: Run tests
3837
run: bun jest
38+
skip-tests:
39+
if: github.base_ref != 'main'
40+
runs-on: ubuntu-latest
41+
42+
steps:
43+
- name: Skip tests for non-main PR
44+
run: echo "Tests skipped - PR is not targeting main branch"

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)