diff options
Diffstat (limited to 'echo/nginx_log_compat.go')
-rw-r--r-- | echo/nginx_log_compat.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/echo/nginx_log_compat.go b/echo/nginx_log_compat.go new file mode 100644 index 0000000..de9168a --- /dev/null +++ b/echo/nginx_log_compat.go | |||
@@ -0,0 +1,31 @@ | |||
1 | package echo | ||
2 | |||
3 | import ( | ||
4 | "io" | ||
5 | "os" | ||
6 | |||
7 | "github.com/labstack/echo/v4/middleware" | ||
8 | ) | ||
9 | |||
10 | const ( | ||
11 | nginxCombinedHostFormat = `${host} ${remote_ip} - - [${time_custom}] "${method} ${uri} ` + | ||
12 | `${protocol}" ${status} ${bytes_out} "${referer}" "${user_agent}"` + "\n" | ||
13 | nginxCombinedHostDateFormat = "02/Jan/2006:15:04:04 -0700" | ||
14 | ) | ||
15 | |||
16 | func NginxCombinedHostConfig(o io.Writer) middleware.LoggerConfig { | ||
17 | return middleware.LoggerConfig{ | ||
18 | Format: nginxCombinedHostFormat, | ||
19 | CustomTimeFormat: nginxCombinedHostDateFormat, | ||
20 | Output: o, | ||
21 | } | ||
22 | } | ||
23 | |||
24 | // TODO: Implement HUP as re-open (like: https://github.com/client9/reopen/blob/master/reopen.go) | ||
25 | func NginxCombinedHostConfigToFile(f string) (middleware.LoggerConfig, error) { | ||
26 | o, err := os.OpenFile(f, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) | ||
27 | if err != nil { | ||
28 | return middleware.LoggerConfig{}, err | ||
29 | } | ||
30 | return NginxCombinedHostConfig(o), nil | ||
31 | } | ||