แก้ไข middleware log ให้รองรับกรณี response เป็น array #223
This commit is contained in:
parent
a22d4cf515
commit
6c60c3636c
2 changed files with 15 additions and 1 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -131,3 +131,5 @@ dist
|
|||
.yarn/build-state.yml
|
||||
.yarn/install-state.gz
|
||||
.pnp.*
|
||||
|
||||
.claude
|
||||
|
|
@ -59,6 +59,17 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
|||
// Get rootId from token
|
||||
const rootId = req.app.locals.logData?.orgRootDnaId;
|
||||
|
||||
let _msg = data?.message;
|
||||
if (!_msg) {
|
||||
if (Array.isArray(data)) {
|
||||
_msg = res.statusCode >= 500
|
||||
? "ไม่สำเร็จ"
|
||||
: res.statusCode >= 400
|
||||
? "พบข้อผิดพลาด"
|
||||
: "สำเร็จ"
|
||||
}
|
||||
}
|
||||
|
||||
const obj = {
|
||||
logType: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warning" : "info",
|
||||
ip: req.ip,
|
||||
|
|
@ -71,7 +82,8 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
|||
method: req.method,
|
||||
endpoint: req.url,
|
||||
responseCode: String(res.statusCode === 304 ? 200 : res.statusCode),
|
||||
responseDescription: data?.message,
|
||||
// responseDescription: data?.message,
|
||||
responseDescription: _msg,
|
||||
input: level === 4 ? JSON.stringify(req.body, null, 2) : undefined,
|
||||
output: level === 4 ? JSON.stringify(data, null, 2) : undefined,
|
||||
...req.app.locals.logData,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue