Skip to content

Commit

Permalink
Merge pull request FeatureBaseDB#1014 from jaten-molecula/log_in_utc
Browse files Browse the repository at this point in the history
log in UTC in fixed width microseconds RFC3339 format
  • Loading branch information
jaten-molecula committed Oct 22, 2020
2 parents 4cea813 + aad38d1 commit d12639b
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ import (
"fmt"
"io"
"log"
"time"
)

const RFC3339UsecTz0 = "2006-01-02T15:04:05.000000Z07:00"

// Ensure nopLogger implements interface.
var _ Logger = &nopLogger{}

Expand All @@ -45,11 +48,20 @@ type standardLogger struct {
logger *log.Logger
}

const logFlags = log.Ldate | log.Ltime | log.Lmicroseconds
// write in UTC with constant width and microsecond resolution.
type formatLog struct {
w io.Writer
}

func (fl formatLog) Write(bytes []byte) (int, error) {
return fmt.Fprintf(fl.w, "%v %v", time.Now().UTC().Format(RFC3339UsecTz0), string(bytes))
}

func NewStandardLogger(w io.Writer) *standardLogger {
logger := log.New(w, "", 0)
logger.SetOutput(formatLog{w: w})
return &standardLogger{
logger: log.New(w, "", logFlags),
logger: logger,
}
}

Expand All @@ -69,8 +81,10 @@ type verboseLogger struct {
}

func NewVerboseLogger(w io.Writer) *verboseLogger {
logger := log.New(w, "", 0)
logger.SetOutput(formatLog{w: w})
return &verboseLogger{
logger: log.New(w, "", logFlags),
logger: logger,
}
}

Expand Down

0 comments on commit d12639b

Please sign in to comment.