Skip to content

Commit

Permalink
funcr: Allow customization of the timestamp format
Browse files Browse the repository at this point in the history
  • Loading branch information
thockin committed Oct 20, 2021
1 parent 1215c81 commit 4d5bdde
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
16 changes: 12 additions & 4 deletions funcr/funcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ type Options struct {
// overhead, so some users might not want it.
LogTimestamp bool

// TimestampFormat tells funcr how to render timestamps when LogTimestamp
// is enabled. If not specified, a default format will be used. For more
// details, see docs for Go's time.Layout.
TimestampFormat string

// Verbosity tells funcr which V logs to produce. Higher values enable
// more logs. Info logs at or below this level will be written, while logs
// above this level will be discarded.
Expand Down Expand Up @@ -137,8 +142,6 @@ const (
Error
)

const timestampFmt = "2006-01-02 15:04:05.000000"

// fnlogger inherits some of its LogSink implementation from Formatter
// and just needs to add some glue code.
type fnlogger struct {
Expand Down Expand Up @@ -190,7 +193,12 @@ func NewFormatterJSON(opts Options) Formatter {
return newFormatter(opts, outputJSON)
}

const defaultTimestampFmt = "2006-01-02 15:04:05.000000"

func newFormatter(opts Options, outfmt outputFormat) Formatter {
if opts.TimestampFormat == "" {
opts.TimestampFormat = defaultTimestampFmt
}
f := Formatter{
outputFormat: outfmt,
prefix: "",
Expand Down Expand Up @@ -665,7 +673,7 @@ func (f Formatter) FormatInfo(level int, msg string, kvList []interface{}) (pref
prefix = ""
}
if f.opts.LogTimestamp {
args = append(args, "ts", time.Now().Format(timestampFmt))
args = append(args, "ts", time.Now().Format(f.opts.TimestampFormat))
}
if policy := f.opts.LogCaller; policy == All || policy == Info {
args = append(args, "caller", f.caller())
Expand All @@ -685,7 +693,7 @@ func (f Formatter) FormatError(err error, msg string, kvList []interface{}) (pre
prefix = ""
}
if f.opts.LogTimestamp {
args = append(args, "ts", time.Now().Format(timestampFmt))
args = append(args, "ts", time.Now().Format(f.opts.TimestampFormat))
}
if policy := f.opts.LogCaller; policy == All || policy == Error {
args = append(args, "caller", f.caller())
Expand Down
14 changes: 14 additions & 0 deletions funcr/funcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,3 +1051,17 @@ func TestErrorWithCallDepth(t *testing.T) {
}
})
}

func TestOptionsTimestampFormat(t *testing.T) {
cap := &capture{}
// This timestamp format contains none of the characters that are
// considered placeholders, so will produce a constant result.
sink := newSink(cap.Func, NewFormatter(Options{LogTimestamp: true, TimestampFormat: "TIMESTAMP"}))
dSink, _ := sink.(logr.CallDepthLogSink)
sink = dSink.WithCallDepth(1)
sink.Info(0, "msg")
expect := ` "ts"="TIMESTAMP" "level"=0 "msg"="msg"`
if cap.log != expect {
t.Errorf("\nexpected %q\n got %q", expect, cap.log)
}
}

0 comments on commit 4d5bdde

Please sign in to comment.