Allows logs to use local time zone instead of UTC
Co-authored-by: Ludovic Fernandez <ldez@users.noreply.github.com>
This commit is contained in:
parent
889b38f75a
commit
8b08f89d2c
2 changed files with 27 additions and 0 deletions
|
@ -24,6 +24,8 @@ func (f *CommonLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||||
var timestamp = defaultValue
|
var timestamp = defaultValue
|
||||||
if v, ok := entry.Data[StartUTC]; ok {
|
if v, ok := entry.Data[StartUTC]; ok {
|
||||||
timestamp = v.(time.Time).Format(commonLogTimeFormat)
|
timestamp = v.(time.Time).Format(commonLogTimeFormat)
|
||||||
|
} else if v, ok := entry.Data[StartLocal]; ok {
|
||||||
|
timestamp = v.(time.Time).Local().Format(commonLogTimeFormat)
|
||||||
}
|
}
|
||||||
|
|
||||||
var elapsedMillis int64
|
var elapsedMillis int64
|
||||||
|
|
|
@ -2,6 +2,7 @@ package accesslog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -57,10 +58,34 @@ func TestCommonLogFormatter_Format(t *testing.T) {
|
||||||
ServiceURL: "http://10.0.0.2/toto",
|
ServiceURL: "http://10.0.0.2/toto",
|
||||||
},
|
},
|
||||||
expectedLog: `10.0.0.1 - Client [10/Nov/2009:23:00:00 +0000] "GET /foo http" 123 132 "referer" "agent" - "foo" "http://10.0.0.2/toto" 123000ms
|
expectedLog: `10.0.0.1 - Client [10/Nov/2009:23:00:00 +0000] "GET /foo http" 123 132 "referer" "agent" - "foo" "http://10.0.0.2/toto" 123000ms
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "all data with local time",
|
||||||
|
data: map[string]interface{}{
|
||||||
|
StartLocal: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
|
||||||
|
Duration: 123 * time.Second,
|
||||||
|
ClientHost: "10.0.0.1",
|
||||||
|
ClientUsername: "Client",
|
||||||
|
RequestMethod: http.MethodGet,
|
||||||
|
RequestPath: "/foo",
|
||||||
|
RequestProtocol: "http",
|
||||||
|
OriginStatus: 123,
|
||||||
|
OriginContentSize: 132,
|
||||||
|
RequestRefererHeader: "referer",
|
||||||
|
RequestUserAgentHeader: "agent",
|
||||||
|
RequestCount: nil,
|
||||||
|
RouterName: "foo",
|
||||||
|
ServiceURL: "http://10.0.0.2/toto",
|
||||||
|
},
|
||||||
|
expectedLog: `10.0.0.1 - Client [10/Nov/2009:14:00:00 -0900] "GET /foo http" 123 132 "referer" "agent" - "foo" "http://10.0.0.2/toto" 123000ms
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set timezone to Alaska to have a constant behavior
|
||||||
|
os.Setenv("TZ", "US/Alaska")
|
||||||
|
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
test := test
|
test := test
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue