1
0
mirror of https://github.com/labstack/echo.git synced 2025-11-23 22:24:54 +02:00

Allows LoggerConfig to add new header, form, path and query tags (#708)

* [feature] implementing new log configuration tags for headers, form, query and path

* [refactor] merging all tests and removing path from the tags available for the logger

* [doc] updating middleware logger doc

* [tests] adding test cases for the other tags on the logger template

* [cleaning] moving h reference on tests to be a direct assignment given we dont need the reference to h anymore
This commit is contained in:
Antonio Pagano
2016-11-02 15:08:51 -05:00
committed by Vishal Rana
parent 2cb612ff57
commit 4f1aef1469
3 changed files with 71 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import (
"io" "io"
"os" "os"
"strconv" "strconv"
"strings"
"sync" "sync"
"time" "time"
@@ -36,6 +37,9 @@ type (
// - latency_human (Human readable) // - latency_human (Human readable)
// - bytes_in (Bytes received) // - bytes_in (Bytes received)
// - bytes_out (Bytes sent) // - bytes_out (Bytes sent)
// - header:<name>
// - query:<name>
// - form:<name>
// //
// Example "${remote_ip} ${status}" // Example "${remote_ip} ${status}"
// //
@@ -160,6 +164,15 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
return w.Write([]byte(b)) return w.Write([]byte(b))
case "bytes_out": case "bytes_out":
return w.Write([]byte(strconv.FormatInt(res.Size, 10))) return w.Write([]byte(strconv.FormatInt(res.Size, 10)))
default:
switch {
case strings.HasPrefix(tag, "header:"):
return buf.Write([]byte(c.Request().Header.Get(tag[7:])))
case strings.HasPrefix(tag, "query:"):
return buf.Write([]byte(c.QueryParam(tag[6:])))
case strings.HasPrefix(tag, "form:"):
return buf.Write([]byte(c.FormValue(tag[5:])))
}
} }
return 0, nil return 0, nil
}) })

View File

@@ -5,6 +5,8 @@ import (
"errors" "errors"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url"
"strings"
"testing" "testing"
"github.com/labstack/echo" "github.com/labstack/echo"
@@ -78,3 +80,56 @@ func TestLoggerIPAddress(t *testing.T) {
h(c) h(c)
assert.Contains(t, ip, buf.String()) assert.Contains(t, ip, buf.String())
} }
func TestLoggerTemplate(t *testing.T) {
buf := new(bytes.Buffer)
e := echo.New()
e.Use(LoggerWithConfig(LoggerConfig{
Format: `{"time":"${time_rfc3339}","remote_ip":"${remote_ip}","host":"${host}","user_agent":"${user_agent}",` +
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
`"latency_human":"${latency_human}","bytes_in":${bytes_in}, "path":"${path}", "referer":"${referer}",` +
`"bytes_out":${bytes_out},"ch":"${header:X-Custom-Header}",` +
`"us":"${query:username}", "cf":"${form:username}"}` + "\n",
Output: buf,
}))
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Header Logged")
})
req, _ := http.NewRequest(echo.GET, "/?username=apagano-param&password=secret", nil)
req.RequestURI = "/"
req.Header.Add(echo.HeaderXRealIP, "127.0.0.1")
req.Header.Add("Referer", "google.com")
req.Header.Add("User-Agent", "echo-tests-agent")
req.Header.Add("X-Custom-Header", "AAA-CUSTOM-VALUE")
req.Form = url.Values{
"username": []string{"apagano-form"},
"password": []string{"secret-form"},
}
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
cases := map[string]bool{
"apagano-param": true,
"apagano-form": true,
"AAA-CUSTOM-VALUE": true,
"BBB-CUSTOM-VALUE": false,
"secret-form": false,
"hexvalue": false,
"GET": true,
"127.0.0.1": true,
"\"path\":\"/\"": true,
"\"uri\":\"/\"": true,
"\"status\":200": true,
"\"bytes_in\":0": true,
"google.com": true,
"echo-tests-agent": true,
}
for token, present := range cases {
assert.True(t, strings.Contains(buf.String(), token) == present, "Case: "+token)
}
}

View File

@@ -63,6 +63,9 @@ LoggerConfig struct {
// - latency_human (Human readable) // - latency_human (Human readable)
// - bytes_in (Bytes received) // - bytes_in (Bytes received)
// - bytes_out (Bytes sent) // - bytes_out (Bytes sent)
// - header:<name>
// - query:<name>
// - form:<name>
// //
// Example "${remote_ip} ${status}" // Example "${remote_ip} ${status}"
// //