Skip to content

Commit

Permalink
Make the logging time format configurable.
Browse files Browse the repository at this point in the history
This is a very basic approach, but it is pretty non-invasive, doesn't
have a runtime overhead, and provides a fix until #35 is resolved in a
more comprehensive way.
  • Loading branch information
kormat committed Aug 8, 2016
1 parent 0b45fb2 commit 11de1a2
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ import (
)

const (
timeFormat = "2006-01-02T15:04:05-0700"
termTimeFormat = "01-02|15:04:05"
floatFormat = 'f'
termMsgJust = 40
DefTimeFormat = "2006-01-02T15:04:05-0700"
DefTermTimeFormat = "01-02|15:04:05"
floatFormat = 'f'
termMsgJust = 40
)

var (
TimeFormat = DefTimeFormat
TermTimeFormat = DefTermTimeFormat
)

type Format interface {
Expand Down Expand Up @@ -63,9 +68,9 @@ func TerminalFormat() Format {
b := &bytes.Buffer{}
lvl := strings.ToUpper(r.Lvl.String())
if color > 0 {
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %s ", color, lvl, r.Time.Format(termTimeFormat), r.Msg)
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %s ", color, lvl, r.Time.Format(TermTimeFormat), r.Msg)
} else {
fmt.Fprintf(b, "[%s] [%s] %s ", lvl, r.Time.Format(termTimeFormat), r.Msg)
fmt.Fprintf(b, "[%s] [%s] %s ", lvl, r.Time.Format(TermTimeFormat), r.Msg)
}

// try to justify the log output for short messages
Expand Down Expand Up @@ -179,7 +184,7 @@ func formatShared(value interface{}) (result interface{}) {

switch v := value.(type) {
case time.Time:
return v.Format(timeFormat)
return v.Format(TimeFormat)

case error:
return v.Error()
Expand Down Expand Up @@ -212,7 +217,7 @@ func formatLogfmtValue(value interface{}) string {
// Performance optimization: No need for escaping since the provided
// timeFormat doesn't have any escape characters, and escaping is
// expensive.
return t.Format(timeFormat)
return t.Format(TimeFormat)
}
value = formatShared(value)
switch v := value.(type) {
Expand Down

0 comments on commit 11de1a2

Please sign in to comment.