This repository has been archived on 2023-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
dex/vendor/github.com/sirupsen/logrus/writer.go

63 lines
1.2 KiB
Go
Raw Normal View History

2016-12-07 22:28:11 +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 {
2017-07-25 20:49:34 +00:00
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-07 22:28:11 +00:00
reader, writer := io.Pipe()
var printFunc func(args ...interface{})
2017-07-25 20:49:34 +00:00
2016-12-07 22:28:11 +00:00
switch level {
case DebugLevel:
2017-07-25 20:49:34 +00:00
printFunc = entry.Debug
2016-12-07 22:28:11 +00:00
case InfoLevel:
2017-07-25 20:49:34 +00:00
printFunc = entry.Info
2016-12-07 22:28:11 +00:00
case WarnLevel:
2017-07-25 20:49:34 +00:00
printFunc = entry.Warn
2016-12-07 22:28:11 +00:00
case ErrorLevel:
2017-07-25 20:49:34 +00:00
printFunc = entry.Error
2016-12-07 22:28:11 +00:00
case FatalLevel:
2017-07-25 20:49:34 +00:00
printFunc = entry.Fatal
2016-12-07 22:28:11 +00:00
case PanicLevel:
2017-07-25 20:49:34 +00:00
printFunc = entry.Panic
2016-12-07 22:28:11 +00:00
default:
2017-07-25 20:49:34 +00:00
printFunc = entry.Print
2016-12-07 22:28:11 +00:00
}
2017-07-25 20:49:34 +00:00
go entry.writerScanner(reader, printFunc)
2016-12-07 22:28:11 +00:00
runtime.SetFinalizer(writer, writerFinalizer)
return writer
}
2017-07-25 20:49:34 +00:00
func (entry *Entry) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) {
2016-12-07 22:28:11 +00:00
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
printFunc(scanner.Text())
}
if err := scanner.Err(); err != nil {
2017-07-25 20:49:34 +00:00
entry.Errorf("Error while reading from Writer: %s", err)
2016-12-07 22:28:11 +00:00
}
reader.Close()
}
func writerFinalizer(writer *io.PipeWriter) {
writer.Close()
}