mirror of
https://github.com/labstack/echo.git
synced 2025-05-13 22:06:36 +02:00
* [Add] custom time stamp format * [Update] property name & default value & comment about custom logger
This commit is contained in:
parent
e9f67801e3
commit
181e11a330
@ -26,6 +26,7 @@ type (
|
|||||||
// - time_unix_nano
|
// - time_unix_nano
|
||||||
// - time_rfc3339
|
// - time_rfc3339
|
||||||
// - time_rfc3339_nano
|
// - time_rfc3339_nano
|
||||||
|
// - time_custom
|
||||||
// - id (Request ID)
|
// - id (Request ID)
|
||||||
// - remote_ip
|
// - remote_ip
|
||||||
// - uri
|
// - uri
|
||||||
@ -46,7 +47,10 @@ type (
|
|||||||
// Example "${remote_ip} ${status}"
|
// Example "${remote_ip} ${status}"
|
||||||
//
|
//
|
||||||
// Optional. Default value DefaultLoggerConfig.Format.
|
// Optional. Default value DefaultLoggerConfig.Format.
|
||||||
Format string `yaml:"format"`
|
Format string `yaml:"format"`
|
||||||
|
|
||||||
|
// Optional. Default value DefaultLoggerConfig.CustomTimeFormat.
|
||||||
|
CustomTimeFormat string `yaml:"custom_time_format"`
|
||||||
|
|
||||||
// Output is a writer where logs in JSON format are written.
|
// Output is a writer where logs in JSON format are written.
|
||||||
// Optional. Default value os.Stdout.
|
// Optional. Default value os.Stdout.
|
||||||
@ -66,6 +70,7 @@ var (
|
|||||||
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
|
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
|
||||||
`"latency_human":"${latency_human}","bytes_in":${bytes_in},` +
|
`"latency_human":"${latency_human}","bytes_in":${bytes_in},` +
|
||||||
`"bytes_out":${bytes_out}}` + "\n",
|
`"bytes_out":${bytes_out}}` + "\n",
|
||||||
|
CustomTimeFormat:"2006-01-02 15:04:05.00000",
|
||||||
Output: os.Stdout,
|
Output: os.Stdout,
|
||||||
colorer: color.New(),
|
colorer: color.New(),
|
||||||
}
|
}
|
||||||
@ -126,6 +131,8 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
|
|||||||
return buf.WriteString(time.Now().Format(time.RFC3339))
|
return buf.WriteString(time.Now().Format(time.RFC3339))
|
||||||
case "time_rfc3339_nano":
|
case "time_rfc3339_nano":
|
||||||
return buf.WriteString(time.Now().Format(time.RFC3339Nano))
|
return buf.WriteString(time.Now().Format(time.RFC3339Nano))
|
||||||
|
case "time_custom":
|
||||||
|
return buf.WriteString(time.Now().Format(config.CustomTimeFormat))
|
||||||
case "id":
|
case "id":
|
||||||
id := req.Header.Get(echo.HeaderXRequestID)
|
id := req.Header.Get(echo.HeaderXRequestID)
|
||||||
if id == "" {
|
if id == "" {
|
||||||
|
@ -9,8 +9,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"encoding/json"
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLogger(t *testing.T) {
|
func TestLogger(t *testing.T) {
|
||||||
@ -137,3 +140,34 @@ func TestLoggerTemplate(t *testing.T) {
|
|||||||
assert.True(t, strings.Contains(buf.String(), token) == present, "Case: "+token)
|
assert.True(t, strings.Contains(buf.String(), token) == present, "Case: "+token)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoggerCustomTimestamp(t *testing.T) {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
customTimeFormat := "2006-01-02 15:04:05.00000"
|
||||||
|
e := echo.New()
|
||||||
|
e.Use(LoggerWithConfig(LoggerConfig{
|
||||||
|
Format: `{"time":"${time_custom}","id":"${id}","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}", "session":"${cookie:session}"}` + "\n",
|
||||||
|
CustomTimeFormat: customTimeFormat,
|
||||||
|
Output: buf,
|
||||||
|
}))
|
||||||
|
|
||||||
|
e.GET("/", func(c echo.Context) error {
|
||||||
|
return c.String(http.StatusOK, "custom time stamp test")
|
||||||
|
})
|
||||||
|
|
||||||
|
req := httptest.NewRequest(echo.GET, "/", nil)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
e.ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
var objs map[string]*json.RawMessage
|
||||||
|
if err := json.Unmarshal([]byte(buf.String()), &objs); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
loggedTime := *(*string)(unsafe.Pointer(objs["time"]))
|
||||||
|
_, err := time.Parse(customTimeFormat, loggedTime)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user