Skip to content

Commit

Permalink
Added feature to disable timestamps for standard and field formatters.
Browse files Browse the repository at this point in the history
Kugelschieber committed Sep 24, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 69deb20 commit 7604cff
Showing 4 changed files with 35 additions and 7 deletions.
12 changes: 8 additions & 4 deletions field_formatter.go
Original file line number Diff line number Diff line change
@@ -19,18 +19,22 @@ type Fields map[string]interface{}
// If there is more than one parameter or the type of the parameter is different,
// all parameters will be appended after the message.
type FieldFormatter struct {
timeFormat string
separator string
timeFormat string
disableTime bool
separator string
}

// NewFieldFormatter creates a new FieldFormatter with given timestamp format and separator between message and key value pairs.
// The timestamp can be disabled by passing an empty string.
func NewFieldFormatter(timeFormat, separator string) *FieldFormatter {
return &FieldFormatter{timeFormat: timeFormat, separator: separator}
return &FieldFormatter{timeFormat: timeFormat, disableTime: timeFormat == "", separator: separator}
}

// Fmt formats the message as described for the FieldFormatter.
func (formatter *FieldFormatter) Fmt(buffer *[]byte, level int, t time.Time, msg string, params []interface{}) {
*buffer = append(*buffer, t.Format(formatter.timeFormat)+" "...)
if !formatter.disableTime {
*buffer = append(*buffer, t.Format(formatter.timeFormat)+" "...)
}

switch level {
case LevelDebug:
10 changes: 10 additions & 0 deletions field_formatter_test.go
Original file line number Diff line number Diff line change
@@ -90,3 +90,13 @@ func TestFieldFormatterPanicFmtFields(t *testing.T) {
formatter := NewFieldFormatter(StandardTimeFormat, "\t\t\t")
formatter.Pnc("message", []interface{}{Fields{"variable": "value", "more": 123}})
}

func TestFieldFormatterDiableTime(t *testing.T) {
formatter := NewFieldFormatter("", "\t\t\t")
var buffer []byte
formatter.Fmt(&buffer, LevelDebug, time.Now(), "message", nil)

if string(buffer) != "[DEBUG] message\n" {
t.Fatalf("Unexpected log: %v", string(buffer))
}
}
10 changes: 7 additions & 3 deletions standard_formatter.go
Original file line number Diff line number Diff line change
@@ -13,17 +13,21 @@ const (
// StandardFormatter is the default formatter.
// It prints log messages starting with the timestamp, followed by the log level and the formatted message.
type StandardFormatter struct {
timeFormat string
timeFormat string
disableTime bool
}

// NewStandardFormatter creates a new StandardFormatter with given timestamp format.
// The timestamp can be disabled by passing an empty string.
func NewStandardFormatter(timeFormat string) *StandardFormatter {
return &StandardFormatter{timeFormat: timeFormat}
return &StandardFormatter{timeFormat: timeFormat, disableTime: timeFormat == ""}
}

// Fmt formats the message as described for the StandardFormatter.
func (formatter *StandardFormatter) Fmt(buffer *[]byte, level int, t time.Time, msg string, params []interface{}) {
*buffer = append(*buffer, t.Format(formatter.timeFormat)+" "...)
if !formatter.disableTime {
*buffer = append(*buffer, t.Format(formatter.timeFormat)+" "...)
}

switch level {
case LevelDebug:
10 changes: 10 additions & 0 deletions standard_formatter_test.go
Original file line number Diff line number Diff line change
@@ -68,3 +68,13 @@ func TestStandardFormatterPanicFmt(t *testing.T) {
formatter := NewStandardFormatter(StandardTimeFormat)
formatter.Pnc("message %s", []interface{}{"formatted"})
}

func TestStandardFormatterDiableTime(t *testing.T) {
formatter := NewStandardFormatter("")
var buffer []byte
formatter.Fmt(&buffer, LevelDebug, time.Now(), "message", nil)

if string(buffer) != "[DEBUG] message\n" {
t.Fatalf("Unexpected log: %v", string(buffer))
}
}

0 comments on commit 7604cff

Please sign in to comment.