Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the logging time format configurable. #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)

// Format is the interface implemented by StreamHandler formatters.
Expand Down Expand Up @@ -64,9 +69,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 @@ -180,7 +185,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 @@ -216,7 +221,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