Skip to content

Commit

Permalink
Introduce configurable timestamp format ala Logrus practices
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Reagor committed Jan 5, 2018
1 parent 9f2c7bc commit c73f47f
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions config/logger/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ func (l *Config) Init() error {
case "json":
formatter = &logrus.JSONFormatter{}
case "default":
formatter = &DefaultLogFormatter{}
formatter = &DefaultLogFormatter{
TimestampFormat: time.RFC3339Nano,
}
default:
return fmt.Errorf("Unknown log format '%s'", l.Format)
}
Expand All @@ -86,13 +88,14 @@ func (l *Config) Init() error {

// DefaultLogFormatter delegates formatting to standard go log package
type DefaultLogFormatter struct {
TimestampFormat string
}

// Format formats the logrus entry by passing it to the "log" package
func (f *DefaultLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
b := &bytes.Buffer{}
logger := log.New(b, "", 0)
logger.Println(time.Now().Format(time.RFC3339Nano) + " " + string(entry.Message))
logger.Println(time.Now().Format(f.TimestampFormat) + " " + string(entry.Message))
// Panic and Fatal are handled by logrus automatically
return b.Bytes(), nil
}
Expand Down

0 comments on commit c73f47f

Please sign in to comment.