5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-25 06:21:36 +00:00
matterbridge/vendor/github.com/Sirupsen/logrus/writer.go

63 lines
1.2 KiB
Go
Raw Normal View History

2016-04-10 21:39:38 +00:00
package logrus
import (
"bufio"
"io"
"runtime"
)
func (logger *Logger) Writer() *io.PipeWriter {
2017-03-25 19:45:10 +00:00
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 {
2016-04-10 21:39:38 +00:00
reader, writer := io.Pipe()
2017-03-25 19:45:10 +00:00
var printFunc func(args ...interface{})
switch level {
case DebugLevel:
printFunc = entry.Debug
case InfoLevel:
printFunc = entry.Info
case WarnLevel:
printFunc = entry.Warn
case ErrorLevel:
printFunc = entry.Error
case FatalLevel:
printFunc = entry.Fatal
case PanicLevel:
printFunc = entry.Panic
default:
printFunc = entry.Print
}
go entry.writerScanner(reader, printFunc)
2016-04-10 21:39:38 +00:00
runtime.SetFinalizer(writer, writerFinalizer)
return writer
}
2017-03-25 19:45:10 +00:00
func (entry *Entry) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) {
2016-04-10 21:39:38 +00:00
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
2017-03-25 19:45:10 +00:00
printFunc(scanner.Text())
2016-04-10 21:39:38 +00:00
}
if err := scanner.Err(); err != nil {
2017-03-25 19:45:10 +00:00
entry.Errorf("Error while reading from Writer: %s", err)
2016-04-10 21:39:38 +00:00
}
reader.Close()
}
func writerFinalizer(writer *io.PipeWriter) {
writer.Close()
}