traefik/vendor/github.com/Sirupsen/logrus/writer.go

63 lines
1.2 KiB
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
package logrus
import (
"bufio"
"io"
"runtime"
)
func (logger *Logger) Writer() *io.PipeWriter {
return logger.WriterLevel(InfoLevel)
}
func (logger *Logger) WriterLevel(level Level) *io.PipeWriter {
return NewEntry(logger).WriterLevel(level)
}
func (entry *Entry) Writer() *io.PipeWriter {
return entry.WriterLevel(InfoLevel)
}
func (entry *Entry) WriterLevel(level Level) *io.PipeWriter {
2017-02-07 21:33:23 +00:00
reader, writer := io.Pipe()
var printFunc func(args ...interface{})
2017-02-07 21:33:23 +00:00
switch level {
case DebugLevel:
printFunc = entry.Debug
2017-02-07 21:33:23 +00:00
case InfoLevel:
printFunc = entry.Info
2017-02-07 21:33:23 +00:00
case WarnLevel:
printFunc = entry.Warn
2017-02-07 21:33:23 +00:00
case ErrorLevel:
printFunc = entry.Error
2017-02-07 21:33:23 +00:00
case FatalLevel:
printFunc = entry.Fatal
2017-02-07 21:33:23 +00:00
case PanicLevel:
printFunc = entry.Panic
2017-02-07 21:33:23 +00:00
default:
printFunc = entry.Print
2017-02-07 21:33:23 +00:00
}
go entry.writerScanner(reader, printFunc)
2017-02-07 21:33:23 +00:00
runtime.SetFinalizer(writer, writerFinalizer)
return writer
}
func (entry *Entry) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) {
2017-02-07 21:33:23 +00:00
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
printFunc(scanner.Text())
}
if err := scanner.Err(); err != nil {
entry.Errorf("Error while reading from Writer: %s", err)
2017-02-07 21:33:23 +00:00
}
reader.Close()
}
func writerFinalizer(writer *io.PipeWriter) {
writer.Close()
}