@ -6,6 +6,7 @@ import (
"io"
"io"
"os"
"os"
"regexp"
"regexp"
"runtime"
"sort"
"sort"
"strings"
"strings"
"sync"
"sync"
@ -121,6 +122,14 @@ type TextFormatter struct {
// Whether the logger's out is to a terminal.
// Whether the logger's out is to a terminal.
isTerminal bool
isTerminal bool
// CallerPrettyfier can be set by the user to modify the content
// of the function and file keys in the data when ReportCaller is
// activated. If any of the returned value is the empty string the
// corresponding key will be removed from fields.
CallerPrettyfier func ( * runtime . Frame ) ( function string , file string )
CallerFormatter func ( function , file string ) string
sync . Once
sync . Once
}
}
@ -217,6 +226,23 @@ func (f *TextFormatter) Format(entry *logrus.Entry) ([]byte, error) {
if entry . Message != "" {
if entry . Message != "" {
f . appendKeyValue ( b , "msg" , entry . Message , lastKeyIdx >= 0 )
f . appendKeyValue ( b , "msg" , entry . Message , lastKeyIdx >= 0 )
}
}
if entry . HasCaller ( ) {
var funcVal , fileVal string
if f . CallerPrettyfier != nil {
funcVal , fileVal = f . CallerPrettyfier ( entry . Caller )
} else {
funcVal , fileVal = extractCallerInfo ( entry . Caller )
}
if funcVal != "" {
f . appendKeyValue ( b , "func" , funcVal , true )
}
if fileVal != "" {
f . appendKeyValue ( b , "file" , fileVal , true )
}
}
for i , key := range keys {
for i , key := range keys {
f . appendKeyValue ( b , key , entry . Data [ key ] , lastKeyIdx != i )
f . appendKeyValue ( b , key , entry . Data [ key ] , lastKeyIdx != i )
}
}
@ -276,7 +302,7 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys
prefixFormat := "%s"
prefixFormat := "%s"
if f . PrefixPadding != 0 {
if f . PrefixPadding != 0 {
prefixFormat = fmt . Sprintf ( "%%-%ds ", adjustedPrefixPadding )
prefixFormat = fmt . Sprintf ( "%%-%ds %%s ", adjustedPrefixPadding )
}
}
messageFormat := "%s"
messageFormat := "%s"
@ -284,8 +310,24 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys
messageFormat = fmt . Sprintf ( "%%-%ds" , f . SpacePadding )
messageFormat = fmt . Sprintf ( "%%-%ds" , f . SpacePadding )
}
}
caller := ""
if entry . HasCaller ( ) {
var funcVal , fileVal string
if f . CallerPrettyfier != nil {
funcVal , fileVal = f . CallerPrettyfier ( entry . Caller )
} else {
funcVal , fileVal = extractCallerInfo ( entry . Caller )
}
if f . CallerFormatter != nil {
caller = f . CallerFormatter ( funcVal , fileVal )
} else {
caller = fmt . Sprintf ( " (%s: %s)" , fileVal , funcVal )
}
}
if f . DisableTimestamp {
if f . DisableTimestamp {
fmt . Fprintf ( b , "%s" + prefixFormat + " " + messageFormat , level , prefix , message )
fmt . Fprintf ( b , "%s" + prefixFormat + " " + messageFormat , level , prefix , caller, message)
} else {
} else {
var timestamp string
var timestamp string
if ! f . FullTimestamp {
if ! f . FullTimestamp {
@ -293,7 +335,7 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys
} else {
} else {
timestamp = fmt . Sprintf ( "[%s]" , entry . Time . Format ( timestampFormat ) )
timestamp = fmt . Sprintf ( "[%s]" , entry . Time . Format ( timestampFormat ) )
}
}
fmt . Fprintf ( b , "%s %s" + prefixFormat + " " + messageFormat , colorScheme . TimestampColor ( timestamp ) , level , prefix , message)
fmt . Fprintf ( b , "%s %s" + prefixFormat + " " + messageFormat , colorScheme . TimestampColor ( timestamp ) , level , prefix , caller, message)
}
}
for _ , k := range keys {
for _ , k := range keys {
if k != "prefix" {
if k != "prefix" {
@ -318,6 +360,12 @@ func (f *TextFormatter) needsQuoting(text string) bool {
return false
return false
}
}
func extractCallerInfo ( caller * runtime . Frame ) ( string , string ) {
funcVal := caller . Function
fileVal := fmt . Sprintf ( "%s:%d" , caller . File , caller . Line )
return funcVal , fileVal
}
func extractPrefix ( msg string ) ( string , string ) {
func extractPrefix ( msg string ) ( string , string ) {
prefix := ""
prefix := ""
regex := regexp . MustCompile ( "^\\[(.*?)\\]" )
regex := regexp . MustCompile ( "^\\[(.*?)\\]" )