mirror of https://github.com/sirupsen/logrus.git
support to remove colors when writing to text files
This commit is contained in:
parent
fdf1618bf7
commit
bae0101b91
|
@ -10,15 +10,26 @@ import (
|
||||||
type Hook struct {
|
type Hook struct {
|
||||||
Writer io.Writer
|
Writer io.Writer
|
||||||
LogLevels []log.Level
|
LogLevels []log.Level
|
||||||
|
Formatter log.Formatter
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fire will be called when some logging function is called with current hook
|
// Fire will be called when some logging function is called with current hook
|
||||||
// It will format log entry to string and write it to appropriate writer
|
// It will format log entry to string and write it to appropriate writer
|
||||||
func (hook *Hook) Fire(entry *log.Entry) error {
|
func (hook *Hook) Fire(entry *log.Entry) error {
|
||||||
line, err := entry.Bytes()
|
|
||||||
|
var line []byte
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if hook.Formatter != nil {
|
||||||
|
line, err = hook.Formatter.Format(entry)
|
||||||
|
} else {
|
||||||
|
line, err = entry.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = hook.Writer.Write(line)
|
_, err = hook.Writer.Write(line)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -199,37 +199,14 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
if timestampFormat == "" {
|
if timestampFormat == "" {
|
||||||
timestampFormat = defaultTimestampFormat
|
timestampFormat = defaultTimestampFormat
|
||||||
}
|
}
|
||||||
if f.isColored() {
|
|
||||||
f.printColored(b, entry, keys, data, timestampFormat)
|
|
||||||
} else {
|
|
||||||
|
|
||||||
for _, key := range fixedKeys {
|
f.printer(b, entry, keys, data, timestampFormat)
|
||||||
var value interface{}
|
|
||||||
switch {
|
|
||||||
case key == f.FieldMap.resolve(FieldKeyTime):
|
|
||||||
value = entry.Time.Format(timestampFormat)
|
|
||||||
case key == f.FieldMap.resolve(FieldKeyLevel):
|
|
||||||
value = entry.Level.String()
|
|
||||||
case key == f.FieldMap.resolve(FieldKeyMsg):
|
|
||||||
value = entry.Message
|
|
||||||
case key == f.FieldMap.resolve(FieldKeyLogrusError):
|
|
||||||
value = entry.err
|
|
||||||
case key == f.FieldMap.resolve(FieldKeyFunc) && entry.HasCaller():
|
|
||||||
value = funcVal
|
|
||||||
case key == f.FieldMap.resolve(FieldKeyFile) && entry.HasCaller():
|
|
||||||
value = fileVal
|
|
||||||
default:
|
|
||||||
value = data[key]
|
|
||||||
}
|
|
||||||
f.appendKeyValue(b, key, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
b.WriteByte('\n')
|
b.WriteByte('\n')
|
||||||
return b.Bytes(), nil
|
return b.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string, data Fields, timestampFormat string) {
|
func (f *TextFormatter) printer(b *bytes.Buffer, entry *Entry, keys []string, data Fields, timestampFormat string) {
|
||||||
var levelColor int
|
var levelColor int
|
||||||
switch entry.Level {
|
switch entry.Level {
|
||||||
case DebugLevel, TraceLevel:
|
case DebugLevel, TraceLevel:
|
||||||
|
@ -282,15 +259,27 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case f.DisableTimestamp:
|
case f.DisableTimestamp:
|
||||||
|
if f.DisableColors {
|
||||||
|
fmt.Fprintf(b, "%s%s %-44s ", levelText, caller, entry.Message)
|
||||||
|
} else {
|
||||||
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m%s %-44s ", levelColor, levelText, caller, entry.Message)
|
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m%s %-44s ", levelColor, levelText, caller, entry.Message)
|
||||||
|
}
|
||||||
case !f.FullTimestamp:
|
case !f.FullTimestamp:
|
||||||
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d]%s %-44s ", levelColor, levelText, int(entry.Time.Sub(baseTimestamp)/time.Second), caller, entry.Message)
|
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d]%s %-44s ", levelColor, levelText, int(entry.Time.Sub(baseTimestamp)/time.Second), caller, entry.Message)
|
||||||
default:
|
default:
|
||||||
|
if f.DisableColors {
|
||||||
|
fmt.Fprintf(b, "%s[%s]%s %-44s ", levelText, entry.Time.Format(timestampFormat), caller, entry.Message)
|
||||||
|
} else {
|
||||||
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s]%s %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), caller, entry.Message)
|
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s]%s %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), caller, entry.Message)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
for _, k := range keys {
|
for _, k := range keys {
|
||||||
v := data[k]
|
v := data[k]
|
||||||
|
if f.DisableColors {
|
||||||
|
fmt.Fprintf(b, " %s=", k)
|
||||||
|
} else {
|
||||||
fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=", levelColor, k)
|
fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=", levelColor, k)
|
||||||
|
}
|
||||||
f.appendValue(b, v)
|
f.appendValue(b, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue