Formatters control how log entries are converted to bytes for output. Logrus provides two built-in formatters: TextFormatter for human-readable output and JSONFormatter for machine-parsable JSON logs.
Implement custom formatters by satisfying the Formatter interface.
type Formatter interface {
Format(*Entry) ([]byte, error)
}Formats logs as human-readable text. The default formatter for Logrus. Automatically detects TTY and adds colors when attached to a terminal.
type TextFormatter struct {
// Force color output even when not attached to TTY
ForceColors bool
// Force disabling colors
DisableColors bool
// Force quoting of all values
ForceQuote bool
// Disable quoting of values (lower priority than ForceQuote)
DisableQuote bool
// Override coloring based on CLICOLOR and CLICOLOR_FORCE env vars
EnvironmentOverrideColors bool
// Disable timestamp logging
DisableTimestamp bool
// Enable logging full timestamp when TTY attached
FullTimestamp bool
// Timestamp format (uses time.Format/time.Parse format)
TimestampFormat string
// Disable field sorting for consistent output
DisableSorting bool
// Custom sorting function for field keys
SortingFunc func([]string)
// Don't truncate level text to 4 characters
DisableLevelTruncation bool
// Pad level text so all levels have same length
PadLevelText bool
// Wrap empty fields in quotes
QuoteEmptyFields bool
// Customize default field key names
FieldMap FieldMap
// Customize function and file output when ReportCaller is enabled
CallerPrettyfier func(*runtime.Frame) (function string, file string)
}
func (f *TextFormatter) Format(entry *Entry) ([]byte, error)import (
"os"
log "github.com/sirupsen/logrus"
)
func main() {
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
log.Info("This is a text-formatted log")
}With TTY (colored):
INFO[0000] A walrus appears animal=walrus number=1 size=10Without TTY (logfmt compatible):
time="2015-03-26T01:27:38-04:00" level=info msg="A walrus appears" animal=walrus size=10Formats logs as JSON for machine parsing. Ideal for log aggregation systems like Logstash, Splunk, or Elasticsearch.
type JSONFormatter struct {
// Timestamp format (uses time.Format/time.Parse format)
TimestampFormat string
// Disable timestamp in output
DisableTimestamp bool
// Disable HTML escaping in output
DisableHTMLEscape bool
// Nest all log entry parameters under this key
DataKey string
// Customize default field key names
FieldMap FieldMap
// Customize function and file output when ReportCaller is enabled
CallerPrettyfier func(*runtime.Frame) (function string, file string)
// Indent JSON output for readability
PrettyPrint bool
}
func (f *JSONFormatter) Format(entry *Entry) ([]byte, error)import (
log "github.com/sirupsen/logrus"
)
func main() {
log.SetFormatter(&log.JSONFormatter{})
log.WithFields(log.Fields{
"animal": "walrus",
"size": 10,
}).Info("A group of walrus emerges from the ocean")
}{"animal":"walrus","level":"info","msg":"A group of walrus emerges from the ocean","size":10,"time":"2014-03-10 19:57:38.562264131 -0400 EDT"}With PrettyPrint:
{
"animal": "walrus",
"level": "info",
"msg": "A group of walrus emerges from the ocean",
"size": 10,
"time": "2014-03-10 19:57:38.562264131 -0400 EDT"
}Both formatters support customizing field key names through FieldMap.
type FieldMap map[fieldKey]stringimport (
log "github.com/sirupsen/logrus"
)
func main() {
log.SetFormatter(&log.JSONFormatter{
FieldMap: log.FieldMap{
log.FieldKeyTime: "@timestamp",
log.FieldKeyLevel: "@level",
log.FieldKeyMsg: "@message",
log.FieldKeyFunc: "@caller",
},
})
log.Info("Custom field names")
}Output:
{"@level":"info","@message":"Custom field names","@timestamp":"2014-03-10 19:57:38.562264131 -0400 EDT"}Customize how caller information is formatted when ReportCaller is enabled.
import (
"fmt"
"path"
"runtime"
log "github.com/sirupsen/logrus"
)
func main() {
log.SetReportCaller(true)
log.SetFormatter(&log.TextFormatter{
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
filename := path.Base(f.File)
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", filename, f.Line)
},
})
log.Info("Custom caller format")
}