As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Unverified Commit 354ae8d8 authored by Aakriti Gupta's avatar Aakriti Gupta Committed by Ash McKenzie
Browse files

Format timestamp in UTC with a precision of a milisecond

parent a7b63073
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -21,6 +21,15 @@ var (
ProgName string
)
type UTCFormatter struct {
defaultFormatter log.Formatter
}
func (f *UTCFormatter) Format(entry *log.Entry) ([]byte, error) {
entry.Time = entry.Time.UTC()
return f.defaultFormatter.Format(entry)
}
func Configure(cfg *config.Config) error {
mutex.Lock()
defer mutex.Unlock()
Loading
Loading
@@ -34,8 +43,20 @@ func Configure(cfg *config.Config) error {
}
log.SetOutput(logWriter)
if cfg.LogFormat == "json" {
log.SetFormatter(&log.JSONFormatter{})
log.SetFormatter(&UTCFormatter{
defaultFormatter: &log.JSONFormatter{
TimestampFormat: "2020-02-05 15:04:05.999Z",
},
})
} else {
log.SetFormatter(&UTCFormatter{
defaultFormatter: &log.TextFormatter{
TimestampFormat: "2020-02-05 15:04:05.999Z",
FullTimestamp: true,
},
})
}
return nil
Loading
Loading
package logger
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"testing"
)
func TestSomething(t *testing.T){
logger, hook := test.NewNullLogger()
logger.Error("Helloerror")
assert.Equal(t, 1, len(hook.Entries))
assert.Equal(t, logrus.ErrorLevel, hook.LastEntry().Level)
assert.Equal(t, "Helloerror", hook.LastEntry().Message)
// TODO Check timestamp format here
assert.Equal(t, "", hook.LastEntry().Time)
fmt.Println(hook.LastEntry().Time)
hook.Reset()
assert.Nil(t, hook.LastEntry())
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment