writer.go 570 B

1234567891011121314151617181920212223242526272829303132
  1. package logrus
  2. import (
  3. "bufio"
  4. "io"
  5. "runtime"
  6. )
  7. func (logger *Logger) Writer() *io.PipeWriter {
  8. reader, writer := io.Pipe()
  9. go logger.writerScanner(reader)
  10. runtime.SetFinalizer(writer, writerFinalizer)
  11. return writer
  12. }
  13. func (logger *Logger) writerScanner(reader *io.PipeReader) {
  14. scanner := bufio.NewScanner(reader)
  15. for scanner.Scan() {
  16. logger.Print(scanner.Text())
  17. }
  18. if err := scanner.Err(); err != nil {
  19. logger.Errorf("Error while reading from Writer: %s", err)
  20. }
  21. reader.Close()
  22. }
  23. func writerFinalizer(writer *io.PipeWriter) {
  24. writer.Close()
  25. }