Skip to content

Commit 181e11a

Browse files
airKing05vishr
authored andcommitted
[Add] custom time stamp format #1046 (#1066)
* [Add] custom time stamp format * [Update] property name & default value & comment about custom logger
1 parent e9f6780 commit 181e11a

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

middleware/logger.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type (
2626
// - time_unix_nano
2727
// - time_rfc3339
2828
// - time_rfc3339_nano
29+
// - time_custom
2930
// - id (Request ID)
3031
// - remote_ip
3132
// - uri
@@ -46,7 +47,10 @@ type (
4647
// Example "${remote_ip} ${status}"
4748
//
4849
// Optional. Default value DefaultLoggerConfig.Format.
49-
Format string `yaml:"format"`
50+
Format string `yaml:"format"`
51+
52+
// Optional. Default value DefaultLoggerConfig.CustomTimeFormat.
53+
CustomTimeFormat string `yaml:"custom_time_format"`
5054

5155
// Output is a writer where logs in JSON format are written.
5256
// Optional. Default value os.Stdout.
@@ -66,6 +70,7 @@ var (
6670
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
6771
`"latency_human":"${latency_human}","bytes_in":${bytes_in},` +
6872
`"bytes_out":${bytes_out}}` + "\n",
73+
CustomTimeFormat:"2006-01-02 15:04:05.00000",
6974
Output: os.Stdout,
7075
colorer: color.New(),
7176
}
@@ -126,6 +131,8 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
126131
return buf.WriteString(time.Now().Format(time.RFC3339))
127132
case "time_rfc3339_nano":
128133
return buf.WriteString(time.Now().Format(time.RFC3339Nano))
134+
case "time_custom":
135+
return buf.WriteString(time.Now().Format(config.CustomTimeFormat))
129136
case "id":
130137
id := req.Header.Get(echo.HeaderXRequestID)
131138
if id == "" {

middleware/logger_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import (
99
"strings"
1010
"testing"
1111

12+
"encoding/json"
1213
"github.com/labstack/echo"
1314
"github.com/stretchr/testify/assert"
15+
"time"
16+
"unsafe"
1417
)
1518

1619
func TestLogger(t *testing.T) {
@@ -137,3 +140,34 @@ func TestLoggerTemplate(t *testing.T) {
137140
assert.True(t, strings.Contains(buf.String(), token) == present, "Case: "+token)
138141
}
139142
}
143+
144+
func TestLoggerCustomTimestamp(t *testing.T) {
145+
buf := new(bytes.Buffer)
146+
customTimeFormat := "2006-01-02 15:04:05.00000"
147+
e := echo.New()
148+
e.Use(LoggerWithConfig(LoggerConfig{
149+
Format: `{"time":"${time_custom}","id":"${id}","remote_ip":"${remote_ip}","host":"${host}","user_agent":"${user_agent}",` +
150+
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
151+
`"latency_human":"${latency_human}","bytes_in":${bytes_in}, "path":"${path}", "referer":"${referer}",` +
152+
`"bytes_out":${bytes_out},"ch":"${header:X-Custom-Header}",` +
153+
`"us":"${query:username}", "cf":"${form:username}", "session":"${cookie:session}"}` + "\n",
154+
CustomTimeFormat: customTimeFormat,
155+
Output: buf,
156+
}))
157+
158+
e.GET("/", func(c echo.Context) error {
159+
return c.String(http.StatusOK, "custom time stamp test")
160+
})
161+
162+
req := httptest.NewRequest(echo.GET, "/", nil)
163+
rec := httptest.NewRecorder()
164+
e.ServeHTTP(rec, req)
165+
166+
var objs map[string]*json.RawMessage
167+
if err := json.Unmarshal([]byte(buf.String()), &objs); err != nil {
168+
panic(err)
169+
}
170+
loggedTime := *(*string)(unsafe.Pointer(objs["time"]))
171+
_, err := time.Parse(customTimeFormat, loggedTime)
172+
assert.Error(t, err)
173+
}

0 commit comments

Comments
 (0)