Merge pull request #598 from sirupsen/goling_formatters

Fix golint issues in formatters
This commit is contained in:
Damien Mathieu 2017-07-26 20:39:46 +02:00 committed by GitHub
commit abee6f9b06
3 changed files with 11 additions and 4 deletions

View File

@ -2,7 +2,7 @@ package logrus
import "time" import "time"
const DefaultTimestampFormat = time.RFC3339 const defaultTimestampFormat = time.RFC3339
// The Formatter interface is used to implement a custom Formatter. It takes an // The Formatter interface is used to implement a custom Formatter. It takes an
// `Entry`. It exposes all the fields, including the default ones: // `Entry`. It exposes all the fields, including the default ones:

View File

@ -6,8 +6,11 @@ import (
) )
type fieldKey string type fieldKey string
// FieldMap allows customization of the key names for default fields.
type FieldMap map[fieldKey]string type FieldMap map[fieldKey]string
// Default key names for the default fields
const ( const (
FieldKeyMsg = "msg" FieldKeyMsg = "msg"
FieldKeyLevel = "level" FieldKeyLevel = "level"
@ -22,6 +25,7 @@ func (f FieldMap) resolve(key fieldKey) string {
return string(key) return string(key)
} }
// JSONFormatter formats logs into parsable json
type JSONFormatter struct { type JSONFormatter struct {
// TimestampFormat sets the format used for marshaling timestamps. // TimestampFormat sets the format used for marshaling timestamps.
TimestampFormat string TimestampFormat string
@ -29,7 +33,7 @@ type JSONFormatter struct {
// DisableTimestamp allows disabling automatic timestamps in output // DisableTimestamp allows disabling automatic timestamps in output
DisableTimestamp bool 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: // As an example:
// formatter := &JSONFormatter{ // formatter := &JSONFormatter{
// FieldMap: FieldMap{ // FieldMap: FieldMap{
@ -41,6 +45,7 @@ type JSONFormatter struct {
FieldMap FieldMap FieldMap FieldMap
} }
// Format renders a single log entry
func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) { func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
data := make(Fields, len(entry.Data)+3) data := make(Fields, len(entry.Data)+3)
for k, v := range entry.Data { for k, v := range entry.Data {
@ -57,7 +62,7 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
timestampFormat := f.TimestampFormat timestampFormat := f.TimestampFormat
if timestampFormat == "" { if timestampFormat == "" {
timestampFormat = DefaultTimestampFormat timestampFormat = defaultTimestampFormat
} }
if !f.DisableTimestamp { if !f.DisableTimestamp {

View File

@ -26,6 +26,7 @@ func init() {
baseTimestamp = time.Now() baseTimestamp = time.Now()
} }
// TextFormatter formats logs into text
type TextFormatter struct { type TextFormatter struct {
// Set to true to bypass checking for a TTY before outputting colors. // Set to true to bypass checking for a TTY before outputting colors.
ForceColors bool ForceColors bool
@ -64,6 +65,7 @@ func (f *TextFormatter) init(entry *Entry) {
} }
} }
// Format renders a single log entry
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
var b *bytes.Buffer var b *bytes.Buffer
keys := make([]string, 0, len(entry.Data)) keys := make([]string, 0, len(entry.Data))
@ -88,7 +90,7 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
timestampFormat := f.TimestampFormat timestampFormat := f.TimestampFormat
if timestampFormat == "" { if timestampFormat == "" {
timestampFormat = DefaultTimestampFormat timestampFormat = defaultTimestampFormat
} }
if isColored { if isColored {
f.printColored(b, entry, keys, timestampFormat) f.printColored(b, entry, keys, timestampFormat)