1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
| // Middleware
e.Use(middleware.LoggerWithConfig(customLogger()))
// Handler
e.GET("/greet", greet)
e.GET("/healthcheck", healthcheck)
var ResponseLogForamt = `{` +
`"time":"${time_custom}",` +
`"id":"${id}",` + `"remote_ip":"${remote_ip}",` +
`"host":"${host}",` +
`"method":"${method}",` +
`"uri":"${uri}",` +
`"user_agent":"${user_agent}",` +
`"status":${status},` +
`"error":"${error}",` +
`"latency":${latency},` +
`"latency_human":"${latency_human}",` +
`"bytes_in":${bytes_in},` +
`"bytes_out":${bytes_out},` +
`"forwarded-for":"${header:x-forwarded-for}",` +
`"same-as-id":${header:X-Request-Id},` +
`"query":${query:lang}` +
`}`
func customLogger() middleware.LoggerConfig {
cl := middleware.DefaultLoggerConfig
cl.Skipper = customeSkipper
cl.Format = ResponseLogForamt
cl.CustomTimeFormat = "2006/01/02 15:04:05.00000"
return cl
}
func customeSkipper(c echo.Context) bool {
if c.Path() == "/healthcheck" {
return true
}
if os.Getenv("ENV") == "auto-test" {
return true
} else {
return false
}
}
func greet(c echo.Context) error {
switch c.QueryParam("lang") {
case "jp":
return c.String(http.StatusOK, "こんにちは\n")
case "en":
return c.String(http.StatusOK, "Hello World\n")
default:
return c.String(http.StatusOK, "ウホホイ!!\n")
}
}
func healthcheck(c echo.Context) error {
return c.String(http.StatusOK, "I'm fine\n")
}
|