dex/vendor/github.com/sirupsen/logrus/writer.go

63 lines
1.2 KiB
Go
Raw Normal View History

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