Skip to content

Commit

Permalink
Switch to allow header timestamps to be UTC (#65)
Browse files Browse the repository at this point in the history
* Don't test syslog on windows

* Allow selecting UTC time in timestamp header

* Declare func for UTC time outside the method

May save a few ns.

* Removed headerTimeZone

timeZone var isn't accessed if offest is zero
fireflycons authored Apr 22, 2024
1 parent 4d58c4c commit 8102d66
Showing 2 changed files with 21 additions and 4 deletions.
22 changes: 18 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
@@ -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

@@ -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()
@@ -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
@@ -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'
@@ -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
@@ -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
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 (

0 comments on commit 8102d66

Please sign in to comment.