Skip to content

Commit

Permalink
Merge pull request #598 from sirupsen/goling_formatters
Browse files Browse the repository at this point in the history
Fix golint issues in formatters
  • Loading branch information
Damien Mathieu committed Jul 26, 2017
2 parents 259b4b7 + 325575f commit abee6f9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package logrus

import "time"

const DefaultTimestampFormat = time.RFC3339
const defaultTimestampFormat = time.RFC3339

// The Formatter interface is used to implement a custom Formatter. It takes an
// `Entry`. It exposes all the fields, including the default ones:
Expand Down
9 changes: 7 additions & 2 deletions json_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
)

type fieldKey string

// FieldMap allows customization of the key names for default fields.
type FieldMap map[fieldKey]string

// Default key names for the default fields
const (
FieldKeyMsg = "msg"
FieldKeyLevel = "level"
Expand All @@ -22,14 +25,15 @@ func (f FieldMap) resolve(key fieldKey) string {
return string(key)
}

// JSONFormatter formats logs into parsable json
type JSONFormatter struct {
// TimestampFormat sets the format used for marshaling timestamps.
TimestampFormat string

// DisableTimestamp allows disabling automatic timestamps in output
DisableTimestamp bool

// FieldMap allows users to customize the names of keys for various fields.
// FieldMap allows users to customize the names of keys for default fields.
// As an example:
// formatter := &JSONFormatter{
// FieldMap: FieldMap{
Expand All @@ -41,6 +45,7 @@ type JSONFormatter struct {
FieldMap FieldMap
}

// Format renders a single log entry
func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
data := make(Fields, len(entry.Data)+3)
for k, v := range entry.Data {
Expand All @@ -57,7 +62,7 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {

timestampFormat := f.TimestampFormat
if timestampFormat == "" {
timestampFormat = DefaultTimestampFormat
timestampFormat = defaultTimestampFormat
}

if !f.DisableTimestamp {
Expand Down
4 changes: 3 additions & 1 deletion text_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func init() {
baseTimestamp = time.Now()
}

// TextFormatter formats logs into text
type TextFormatter struct {
// Set to true to bypass checking for a TTY before outputting colors.
ForceColors bool
Expand Down Expand Up @@ -64,6 +65,7 @@ func (f *TextFormatter) init(entry *Entry) {
}
}

// Format renders a single log entry
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
var b *bytes.Buffer
keys := make([]string, 0, len(entry.Data))
Expand All @@ -88,7 +90,7 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {

timestampFormat := f.TimestampFormat
if timestampFormat == "" {
timestampFormat = DefaultTimestampFormat
timestampFormat = defaultTimestampFormat
}
if isColored {
f.printColored(b, entry, keys, timestampFormat)
Expand Down

0 comments on commit abee6f9

Please sign in to comment.