Skip to content

Commit 62568d8

Browse files
authored
Merge pull request #114 from thockin/master
funcr: Allow customization of the timestamp format
2 parents 1215c81 + 4d5bdde commit 62568d8

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

funcr/funcr.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ type Options struct {
9494
// overhead, so some users might not want it.
9595
LogTimestamp bool
9696

97+
// TimestampFormat tells funcr how to render timestamps when LogTimestamp
98+
// is enabled. If not specified, a default format will be used. For more
99+
// details, see docs for Go's time.Layout.
100+
TimestampFormat string
101+
97102
// Verbosity tells funcr which V logs to produce. Higher values enable
98103
// more logs. Info logs at or below this level will be written, while logs
99104
// above this level will be discarded.
@@ -137,8 +142,6 @@ const (
137142
Error
138143
)
139144

140-
const timestampFmt = "2006-01-02 15:04:05.000000"
141-
142145
// fnlogger inherits some of its LogSink implementation from Formatter
143146
// and just needs to add some glue code.
144147
type fnlogger struct {
@@ -190,7 +193,12 @@ func NewFormatterJSON(opts Options) Formatter {
190193
return newFormatter(opts, outputJSON)
191194
}
192195

196+
const defaultTimestampFmt = "2006-01-02 15:04:05.000000"
197+
193198
func newFormatter(opts Options, outfmt outputFormat) Formatter {
199+
if opts.TimestampFormat == "" {
200+
opts.TimestampFormat = defaultTimestampFmt
201+
}
194202
f := Formatter{
195203
outputFormat: outfmt,
196204
prefix: "",
@@ -665,7 +673,7 @@ func (f Formatter) FormatInfo(level int, msg string, kvList []interface{}) (pref
665673
prefix = ""
666674
}
667675
if f.opts.LogTimestamp {
668-
args = append(args, "ts", time.Now().Format(timestampFmt))
676+
args = append(args, "ts", time.Now().Format(f.opts.TimestampFormat))
669677
}
670678
if policy := f.opts.LogCaller; policy == All || policy == Info {
671679
args = append(args, "caller", f.caller())
@@ -685,7 +693,7 @@ func (f Formatter) FormatError(err error, msg string, kvList []interface{}) (pre
685693
prefix = ""
686694
}
687695
if f.opts.LogTimestamp {
688-
args = append(args, "ts", time.Now().Format(timestampFmt))
696+
args = append(args, "ts", time.Now().Format(f.opts.TimestampFormat))
689697
}
690698
if policy := f.opts.LogCaller; policy == All || policy == Error {
691699
args = append(args, "caller", f.caller())

funcr/funcr_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,3 +1051,17 @@ func TestErrorWithCallDepth(t *testing.T) {
10511051
}
10521052
})
10531053
}
1054+
1055+
func TestOptionsTimestampFormat(t *testing.T) {
1056+
cap := &capture{}
1057+
// This timestamp format contains none of the characters that are
1058+
// considered placeholders, so will produce a constant result.
1059+
sink := newSink(cap.Func, NewFormatter(Options{LogTimestamp: true, TimestampFormat: "TIMESTAMP"}))
1060+
dSink, _ := sink.(logr.CallDepthLogSink)
1061+
sink = dSink.WithCallDepth(1)
1062+
sink.Info(0, "msg")
1063+
expect := ` "ts"="TIMESTAMP" "level"=0 "msg"="msg"`
1064+
if cap.log != expect {
1065+
t.Errorf("\nexpected %q\n got %q", expect, cap.log)
1066+
}
1067+
}

0 commit comments

Comments
 (0)