2015-02-16 22:19:00 +00:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
2015-03-24 03:09:19 +00:00
|
|
|
func (logger *Logger) Writer() *io.PipeWriter {
|
2015-02-16 22:19:00 +00:00
|
|
|
reader, writer := io.Pipe()
|
|
|
|
|
|
|
|
go logger.writerScanner(reader)
|
|
|
|
runtime.SetFinalizer(writer, writerFinalizer)
|
|
|
|
|
|
|
|
return writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) writerScanner(reader *io.PipeReader) {
|
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
for scanner.Scan() {
|
|
|
|
logger.Print(scanner.Text())
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
logger.Errorf("Error while reading from Writer: %s", err)
|
|
|
|
}
|
|
|
|
reader.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func writerFinalizer(writer *io.PipeWriter) {
|
|
|
|
writer.Close()
|
|
|
|
}
|