@@ -9,8 +9,11 @@ import (
9
9
"strings"
10
10
"testing"
11
11
12
+ "encoding/json"
12
13
"github.com/labstack/echo"
13
14
"github.com/stretchr/testify/assert"
15
+ "time"
16
+ "unsafe"
14
17
)
15
18
16
19
func TestLogger (t * testing.T ) {
@@ -137,3 +140,34 @@ func TestLoggerTemplate(t *testing.T) {
137
140
assert .True (t , strings .Contains (buf .String (), token ) == present , "Case: " + token )
138
141
}
139
142
}
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