logrus/json_formatter.go

23 lines
488 B
Go
Raw Normal View History

2014-03-11 03:22:08 +04:00
package logrus
import (
"encoding/json"
"fmt"
"time"
2014-03-11 03:22:08 +04:00
)
type JSONFormatter struct{}
2014-03-11 03:22:08 +04:00
func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
2014-07-27 06:22:39 +04:00
prefixFieldClashes(entry)
entry.Data["time"] = entry.Time.Format(time.RFC3339)
entry.Data["msg"] = entry.Message
entry.Data["level"] = entry.Level.String()
2014-03-11 03:22:08 +04:00
serialized, err := json.Marshal(entry.Data)
if err != nil {
return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
}
return append(serialized, '\n'), nil
}