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

Add support for custom timestamp. Closes #103 #104

Merged
merged 3 commits into from Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions global.go
Expand Up @@ -2,6 +2,7 @@ package hclog

import (
"sync"
"time"
)

var (
Expand All @@ -14,6 +15,7 @@ var (
DefaultOptions = &LoggerOptions{
Level: DefaultLevel,
Output: DefaultOutput,
TimeFn: func() time.Time { return time.Now() },
binaek marked this conversation as resolved.
Show resolved Hide resolved
}
)

Expand Down
12 changes: 6 additions & 6 deletions intlogger.go
Expand Up @@ -60,7 +60,7 @@ type intLogger struct {
callerOffset int
name string
timeFormat string
useUtcTime bool
timeFn TimeFunction
disableTime bool

// This is an interface so that it's shared by any derived loggers, since
Expand Down Expand Up @@ -117,7 +117,7 @@ func newLogger(opts *LoggerOptions) *intLogger {
json: opts.JSONFormat,
name: opts.Name,
timeFormat: TimeFormat,
useUtcTime: opts.UTCTime,
timeFn: func() time.Time { return time.Now() },
binaek marked this conversation as resolved.
Show resolved Hide resolved
disableTime: opts.DisableTime,
mutex: mutex,
writer: newWriter(output, opts.Color),
Expand All @@ -132,6 +132,9 @@ func newLogger(opts *LoggerOptions) *intLogger {
if l.json {
l.timeFormat = TimeFormatJSON
}
if opts.TimeFn != nil {
l.timeFn = opts.TimeFn
}
if opts.TimeFormat != "" {
l.timeFormat = opts.TimeFormat
}
Expand All @@ -154,10 +157,7 @@ func (l *intLogger) log(name string, level Level, msg string, args ...interface{
return
}

t := time.Now()
if l.useUtcTime {
t = t.UTC()
}
t := l.timeFn()

l.mutex.Lock()
defer l.mutex.Unlock()
Expand Down
7 changes: 5 additions & 2 deletions logger.go
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"strings"
"time"
)

var (
Expand Down Expand Up @@ -219,6 +220,8 @@ type StandardLoggerOptions struct {
ForceLevel Level
}

type TimeFunction = func() time.Time

// LoggerOptions can be used to configure a new logger.
type LoggerOptions struct {
// Name of the subsystem to prefix logs with
Expand Down Expand Up @@ -248,8 +251,8 @@ type LoggerOptions struct {
// The time format to use instead of the default
TimeFormat string

// Control whether to use UTC instead of the default local time-zone
UTCTime bool
// A function which is called to get the time object that is formatted using `TimeFormat`
TimeFn TimeFunction

// Control whether or not to display the time at all. This is required
// because setting TimeFormat to empty assumes the default format.
Expand Down
4 changes: 2 additions & 2 deletions logger_test.go
Expand Up @@ -256,7 +256,7 @@ func TestLogger(t *testing.T) {
Name: "test",
Output: &buf,
TimeFormat: time.Kitchen,
UTCTime: true,
TimeFn: func() time.Time { return time.Now().UTC() },
})

logger.Info("this is test", "who", "programmer", "why", "testing is fun")
Expand Down Expand Up @@ -683,7 +683,7 @@ func TestLogger_JSON(t *testing.T) {
Output: &buf,
JSONFormat: true,
TimeFormat: time.Kitchen,
UTCTime: true,
TimeFn: func() time.Time { return time.Now().UTC() },
})

logger.Info("Lacatan banana")
Expand Down