1. 로그 형식을 json 형태로 출력 (https://stackoverflow.com/a/73936927)
func jsonLoggerMiddleware() gin.HandlerFunc {
return gin.LoggerWithFormatter(
func(params gin.LogFormatterParams) string {
log := make(map[string]interface{})
log["status_code"] = params.StatusCode
log["path"] = params.Path
log["method"] = params.Method
log["start_time"] = params.TimeStamp.Format("2006/01/02 - 15:04:05")
log["remote_addr"] = params.ClientIP
log["response_time"] = params.Latency.String()
s, _ := json.Marshal(log)
return string(s) + "\n"
},
)
}
2. 출력 가능 내용
https://github.com/gin-gonic/gin/blob/8763f33c65f7df8be5b9fe7504ab7fcf20abb41d/logger.go#L63
gin/logger.go at 8763f33c65f7df8be5b9fe7504ab7fcf20abb41d · gin-gonic/gin
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin. - ...
github.com
3. response body 출력
https://stackoverflow.com/a/38548555
How to log response body in gin
I need to log the response body in a middleware of gin, but I don't find how to get the response body. Can anyone help? I am using a middleware like this: func Logger() gin.HandlerFunc { ret...
stackoverflow.com
4. 근데 response body는 한 번 읽으면 다시 처리가 불가능해서 response body를 단순 c.Bind() 함수로 처리하고 있었으면 c.ShouldBindBodyWith() 함수로 변경해서 처리해야 body를 여러번 쓸 수 있어서 활용할 수 있음
https://pkg.go.dev/github.com/gin-gonic/gin#Context.ShouldBindBodyWith
'Programming > Tip&Informaion' 카테고리의 다른 글
[Spark] spark event log 로 데이터 사용량 추출 (event log 구조 분석) (0) | 2025.03.26 |
---|---|
[log4j2] log4j2 설정법 및 spark operator 연동 (feat. datahub 로깅) (0) | 2024.10.18 |
[Spark] spark history server 띄울 때, iam 권한으로 s3 읽기 (0) | 2024.05.31 |
[실수노트] aws에서 여러 eks cluster에서 karpenter 운영 시 실수 (0) | 2024.04.18 |
knative/istio activator 종료 및 재실행 현상 (0) | 2024.02.19 |