Skip to content

Commit de0a9aa

Browse files
authored
Merge pull request #398 from ut-code/task/#397/logging-with-morgan
#397 Enable to export logging of API requests/responses using morgan
2 parents cf55478 + 87f5185 commit de0a9aa

File tree

4 files changed

+120
-8
lines changed

4 files changed

+120
-8
lines changed

backend/package-lock.json

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

backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
"@elastic/elasticsearch": "^8.13.0",
2020
"@prisma/client": "^5.11.0",
2121
"@supabase/supabase-js": "^2.39.0",
22+
"@types/morgan": "^1.9.9",
2223
"csv-parse": "^5.5.6",
2324
"express": "^4.18.2",
25+
"morgan": "^1.10.0",
2426
"swagger-ui-express": "^5.0.0",
2527
"typescript": "^5.3.2"
2628
},

backend/src/app.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express from "express"
22
import cors from "cors"
3+
import morgan from "morgan"
34
import swaggerUi from "swagger-ui-express"
45
import swaggerDocument from "../../openapi.json"
56
import UserController from "./controllers/UserController"
@@ -19,6 +20,35 @@ app.use(cors())
1920
app.use(express.json())
2021
app.use(express.urlencoded({ extended: true }))
2122

23+
// morgan でリクエストボディとレスポンスボディをログ出力する
24+
// Ref: https://www.npmjs.com/package/morgan-body
25+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26+
function logBodyGen(prependStr: string, getBodyFunc: (req: any, res: any) => string) {
27+
const bodyFormatName = "bodyFmt_" + prependStr
28+
morgan.format(bodyFormatName, function logBody(_, req, res) {
29+
const url = req.url
30+
const method = req.method
31+
const status = res.statusCode
32+
const body = getBodyFunc(req, res)
33+
const timeStamp = new Date().toISOString()
34+
return `[${timeStamp}] ${method} ${url} ${status} ${prependStr}: ${JSON.stringify(body)}`
35+
})
36+
return bodyFormatName
37+
}
38+
39+
const appResponsePrototype = Object.getPrototypeOf(app.response)
40+
const originalSend = appResponsePrototype.send
41+
const morganBodyResponseSymbol = Symbol()
42+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
43+
appResponsePrototype.send = function sendOverWrite(body: any) {
44+
originalSend.call(this, body)
45+
this[morganBodyResponseSymbol] = body
46+
}
47+
48+
app.use(morgan("[:date[iso]] :method :url :status ResponseTime :response-time ms"))
49+
app.use(morgan(logBodyGen("RequestBody", (req) => req.body)))
50+
app.use(morgan(logBodyGen("ResponseBody", (_req, res) => res[morganBodyResponseSymbol])))
51+
2252
app.get("/", (_req, res) => {
2353
res.status(200).send("Hello, world!")
2454
})

backend/src/controllers/SearchController.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ class SearchController {
7878
})
7979

8080
const hits = result.hits.hits
81-
console.log(hits)
8281
if (hits.length === 0) {
8382
res.status(404).json({ error: "Not found" })
8483
return

0 commit comments

Comments
 (0)