Skip to content

Commit

Permalink
Merge pull request sirupsen#347 from Sirupsen/level-writer
Browse files Browse the repository at this point in the history
Add WriterLevel() function to the logger
  • Loading branch information
aybabtme committed Apr 16, 2016
2 parents 870c1fc + 218981b commit 2b673ab
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,40 @@ import (
)

func (logger *Logger) Writer() *io.PipeWriter {
return logger.WriterLevel(InfoLevel)
}

func (logger *Logger) WriterLevel(level Level) *io.PipeWriter {
reader, writer := io.Pipe()

go logger.writerScanner(reader)
var printFunc func(args ...interface{})
switch level {
case DebugLevel:
printFunc = logger.Debug
case InfoLevel:
printFunc = logger.Info
case WarnLevel:
printFunc = logger.Warn
case ErrorLevel:
printFunc = logger.Error
case FatalLevel:
printFunc = logger.Fatal
case PanicLevel:
printFunc = logger.Panic
default:
printFunc = logger.Print
}

go logger.writerScanner(reader, printFunc)
runtime.SetFinalizer(writer, writerFinalizer)

return writer
}

func (logger *Logger) writerScanner(reader *io.PipeReader) {
func (logger *Logger) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
logger.Print(scanner.Text())
printFunc(scanner.Text())
}
if err := scanner.Err(); err != nil {
logger.Errorf("Error while reading from Writer: %s", err)
Expand Down

0 comments on commit 2b673ab

Please sign in to comment.