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

Added SetTimeNow function to override global timeNow func #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.test
.idea
*.iml
*~
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func main() {

// Set the backends to be used.
logging.SetBackend(backend1Leveled, backend2Formatter)

// Set log time to UTC, default is Local
logging.SetTimeNow(time.Now().UTC)

log.Debugf("debug %s", Password("secret"))
log.Info("info")
Expand Down
5 changes: 5 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ func MustGetLogger(module string) *Logger {
return logger
}

// SetTimeNow configures the time.Time value generated for log time values
func SetTimeNow(now func () time.Time) {
timeNow = now
}

// Reset restores the internal state of the logging library.
func Reset() {
// TODO make a global Init() method to be less magic? or make it such that
Expand Down
22 changes: 21 additions & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package logging

import "testing"
import (
"testing"
"time"
)

type Password string

Expand Down Expand Up @@ -60,3 +63,20 @@ func TestPrivateBackend(t *testing.T) {
t.Error("logged to defaultBackend:", MemoryRecordN(privateBackend, 0))
}
}

func TestLogger_SetTimerNow(t *testing.T) {
_ = InitForTesting(DEBUG)
_ = MustGetLogger("test")

if now := timeNow(); now != time.Unix(0, 0).UTC() {
t.Error("test timeNow incorrect", now)
}

SetTimeNow(func() time.Time {
return time.Unix(1, 1)
})

if now := timeNow(); now != time.Unix(1, 1) {
t.Error("test timeNow dit not get overwritten", now)
}
}