2016-07-20 09:46:01 +00:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (logger *Logger) Writer() *io.PipeWriter {
|
|
|
|
reader, writer := io.Pipe()
|
|
|
|
|
2016-09-17 13:50:35 +00:00
|
|
|
go logger.writerScanner(reader)
|
2016-07-20 09:46:01 +00:00
|
|
|
runtime.SetFinalizer(writer, writerFinalizer)
|
|
|
|
|
|
|
|
return writer
|
|
|
|
}
|
|
|
|
|
2016-09-17 13:50:35 +00:00
|
|
|
func (logger *Logger) writerScanner(reader *io.PipeReader) {
|
2016-07-20 09:46:01 +00:00
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
for scanner.Scan() {
|
2016-09-17 13:50:35 +00:00
|
|
|
logger.Print(scanner.Text())
|
2016-07-20 09:46:01 +00:00
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
logger.Errorf("Error while reading from Writer: %s", err)
|
|
|
|
}
|
|
|
|
reader.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func writerFinalizer(writer *io.PipeWriter) {
|
|
|
|
writer.Close()
|
|
|
|
}
|