Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 1 | package logrus |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "io" |
| 6 | "runtime" |
| 7 | ) |
| 8 | |
| 9 | func (logger *Logger) Writer() *io.PipeWriter { |
| 10 | return logger.WriterLevel(InfoLevel) |
| 11 | } |
| 12 | |
| 13 | func (logger *Logger) WriterLevel(level Level) *io.PipeWriter { |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 14 | return NewEntry(logger).WriterLevel(level) |
| 15 | } |
| 16 | |
| 17 | func (entry *Entry) Writer() *io.PipeWriter { |
| 18 | return entry.WriterLevel(InfoLevel) |
| 19 | } |
| 20 | |
| 21 | func (entry *Entry) WriterLevel(level Level) *io.PipeWriter { |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 22 | reader, writer := io.Pipe() |
| 23 | |
| 24 | var printFunc func(args ...interface{}) |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 25 | |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 26 | switch level { |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 27 | case TraceLevel: |
| 28 | printFunc = entry.Trace |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 29 | case DebugLevel: |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 30 | printFunc = entry.Debug |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 31 | case InfoLevel: |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 32 | printFunc = entry.Info |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 33 | case WarnLevel: |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 34 | printFunc = entry.Warn |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 35 | case ErrorLevel: |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 36 | printFunc = entry.Error |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 37 | case FatalLevel: |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 38 | printFunc = entry.Fatal |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 39 | case PanicLevel: |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 40 | printFunc = entry.Panic |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 41 | default: |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 42 | printFunc = entry.Print |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 45 | go entry.writerScanner(reader, printFunc) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 46 | runtime.SetFinalizer(writer, writerFinalizer) |
| 47 | |
| 48 | return writer |
| 49 | } |
| 50 | |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 51 | func (entry *Entry) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) { |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 52 | scanner := bufio.NewScanner(reader) |
| 53 | for scanner.Scan() { |
| 54 | printFunc(scanner.Text()) |
| 55 | } |
| 56 | if err := scanner.Err(); err != nil { |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 57 | entry.Errorf("Error while reading from Writer: %s", err) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 58 | } |
| 59 | reader.Close() |
| 60 | } |
| 61 | |
| 62 | func writerFinalizer(writer *io.PipeWriter) { |
| 63 | writer.Close() |
| 64 | } |