Skip to content

Commit

Permalink
Merge pull request #104 from binaek/utc_zone
Browse files Browse the repository at this point in the history
Add support for custom timestamp. Closes #103
  • Loading branch information
evanphx committed Jan 10, 2022
2 parents 37db868 + 7465a0a commit 0cd5096
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
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: time.Now,
}
)

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

// This is an interface so that it's shared by any derived loggers, since
Expand Down Expand Up @@ -116,6 +117,7 @@ func newLogger(opts *LoggerOptions) *intLogger {
json: opts.JSONFormat,
name: opts.Name,
timeFormat: TimeFormat,
timeFn: time.Now,
disableTime: opts.DisableTime,
mutex: mutex,
writer: newWriter(output, opts.Color),
Expand All @@ -130,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 @@ -152,7 +157,7 @@ func (l *intLogger) log(name string, level Level, msg string, args ...interface{
return
}

t := time.Now()
t := l.timeFn()

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

var (
Expand Down Expand Up @@ -228,6 +229,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 @@ -257,6 +260,9 @@ type LoggerOptions struct {
// The time format to use instead of the default
TimeFormat string

// 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.
DisableTime bool
Expand Down
46 changes: 46 additions & 0 deletions logger_test.go
Expand Up @@ -249,6 +249,24 @@ func TestLogger(t *testing.T) {
assert.Equal(t, str[:dataIdx], time.Now().Format(time.Kitchen))
})

t.Run("use UTC time zone", func(t *testing.T) {
var buf bytes.Buffer

logger := New(&LoggerOptions{
Name: "test",
Output: &buf,
TimeFormat: time.Kitchen,
TimeFn: func() time.Time { return time.Now().UTC() },
})

logger.Info("this is test", "who", "programmer", "why", "testing is fun")

str := buf.String()
dataIdx := strings.IndexByte(str, ' ')

assert.Equal(t, str[:dataIdx], time.Now().UTC().Format(time.Kitchen))
})

t.Run("respects DisableTime", func(t *testing.T) {
var buf bytes.Buffer

Expand Down Expand Up @@ -657,6 +675,34 @@ func TestLogger_JSON(t *testing.T) {
assert.Equal(t, val, time.Now().Format(time.Kitchen))
})

t.Run("use UTC time zone", func(t *testing.T) {
var buf bytes.Buffer

logger := New(&LoggerOptions{
Name: "test",
Output: &buf,
JSONFormat: true,
TimeFormat: time.Kitchen,
TimeFn: func() time.Time { return time.Now().UTC() },
})

logger.Info("Lacatan banana")

b := buf.Bytes()

var raw map[string]interface{}
if err := json.Unmarshal(b, &raw); err != nil {
t.Fatal(err)
}

val, ok := raw["@timestamp"]
if !ok {
t.Fatal("missing '@timestamp' key")
}

assert.Equal(t, val, time.Now().UTC().Format(time.Kitchen))
})

t.Run("respects DisableTime", func(t *testing.T) {
var buf bytes.Buffer
logger := New(&LoggerOptions{
Expand Down

0 comments on commit 0cd5096

Please sign in to comment.