Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to allow header timestamps to be UTC #65

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,16 @@ type Logger struct {
// If Caller is negative, adds the full /path/to/file:line of the "caller" key.
Caller int

// TimeField defines the time filed name in output. It uses "time" in if empty.
// TimeField defines the time field name in output. It uses "time" in if empty.
TimeField string

// TimeFormat specifies the time format in output. It uses time.RFC3339 with milliseconds if empty.
// If set with `TimeFormatUnix`, `TimeFormatUnixMs`, times are formated as UNIX timestamp.
TimeFormat string

// TimeUTC specifices that the timestamps should be UTC instead of system local time.
TimeUTC bool

// Context specifies an optional context of logger.
Context Context

Expand Down Expand Up @@ -438,6 +441,9 @@ const smallsString = "00010203040506070809" +
"90919293949596979899"

var timeNow = time.Now
var timeUtcNow = func() time.Time {
return time.Now().UTC()
}
var timeOffset, timeZone = func() (int64, string) {
now := timeNow()
_, n := now.Zone()
Expand All @@ -450,6 +456,14 @@ func (l *Logger) silent(level Level) bool {
}

func (l *Logger) header(level Level) *Entry {
headerTimeFunc := timeNow
headerTimeOffset := timeOffset

if l.TimeUTC {
headerTimeFunc = timeUtcNow
headerTimeOffset = 0
}

e := epool.Get().(*Entry)
e.buf = e.buf[:0]
e.Level = level
Expand All @@ -471,7 +485,7 @@ func (l *Logger) header(level Level) *Entry {
sec, nsec, _ := now()
var tmp [32]byte
var buf []byte
if timeOffset == 0 {
if headerTimeOffset == 0 {
// "2006-01-02T15:04:05.999Z"
tmp[25] = '"'
tmp[24] = 'Z'
Expand All @@ -488,7 +502,7 @@ func (l *Logger) header(level Level) *Entry {
buf = tmp[:31]
}
// date time
sec += 9223372028715321600 + timeOffset // unixToInternal + internalToAbsolute + timeOffset
sec += 9223372028715321600 + headerTimeOffset // unixToInternal + internalToAbsolute + timeOffset
year, month, day, _ := absDate(uint64(sec), true)
hour, minute, second := absClock(uint64(sec))
// year
Expand Down Expand Up @@ -626,7 +640,7 @@ func (l *Logger) header(level Level) *Entry {
e.buf = append(e.buf, tmp[:]...)
default:
e.buf = append(e.buf, '"')
e.buf = timeNow().AppendFormat(e.buf, l.TimeFormat)
e.buf = headerTimeFunc().AppendFormat(e.buf, l.TimeFormat)
e.buf = append(e.buf, '"')
}
// level
Expand Down
3 changes: 3 additions & 0 deletions syslog_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !windows
// +build !windows

package log

import (
Expand Down