| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | package logrus |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "io" |
| 6 | "runtime" |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 7 | "strings" |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 8 | ) |
| 9 | |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 10 | // Writer at INFO level. See WriterLevel for details. |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 11 | func (logger *Logger) Writer() *io.PipeWriter { |
| 12 | return logger.WriterLevel(InfoLevel) |
| 13 | } |
| 14 | |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 15 | // WriterLevel returns an io.Writer that can be used to write arbitrary text to |
| 16 | // the logger at the given log level. Each line written to the writer will be |
| 17 | // printed in the usual way using formatters and hooks. The writer is part of an |
| 18 | // io.Pipe and it is the callers responsibility to close the writer when done. |
| 19 | // This can be used to override the standard library logger easily. |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 20 | func (logger *Logger) WriterLevel(level Level) *io.PipeWriter { |
| 21 | return NewEntry(logger).WriterLevel(level) |
| 22 | } |
| 23 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 24 | // Writer returns an io.Writer that writes to the logger at the info log level |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 25 | func (entry *Entry) Writer() *io.PipeWriter { |
| 26 | return entry.WriterLevel(InfoLevel) |
| 27 | } |
| 28 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 29 | // WriterLevel returns an io.Writer that writes to the logger at the given log level |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 30 | func (entry *Entry) WriterLevel(level Level) *io.PipeWriter { |
| 31 | reader, writer := io.Pipe() |
| 32 | |
| 33 | var printFunc func(args ...interface{}) |
| 34 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 35 | // Determine which log function to use based on the specified log level |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 36 | switch level { |
| 37 | case TraceLevel: |
| 38 | printFunc = entry.Trace |
| 39 | case DebugLevel: |
| 40 | printFunc = entry.Debug |
| 41 | case InfoLevel: |
| 42 | printFunc = entry.Info |
| 43 | case WarnLevel: |
| 44 | printFunc = entry.Warn |
| 45 | case ErrorLevel: |
| 46 | printFunc = entry.Error |
| 47 | case FatalLevel: |
| 48 | printFunc = entry.Fatal |
| 49 | case PanicLevel: |
| 50 | printFunc = entry.Panic |
| 51 | default: |
| 52 | printFunc = entry.Print |
| 53 | } |
| 54 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 55 | // Start a new goroutine to scan the input and write it to the logger using the specified print function. |
| 56 | // It splits the input into chunks of up to 64KB to avoid buffer overflows. |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 57 | go entry.writerScanner(reader, printFunc) |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 58 | |
| 59 | // Set a finalizer function to close the writer when it is garbage collected |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 60 | runtime.SetFinalizer(writer, writerFinalizer) |
| 61 | |
| 62 | return writer |
| 63 | } |
| 64 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 65 | // writerScanner scans the input from the reader and writes it to the logger |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 66 | func (entry *Entry) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) { |
| 67 | scanner := bufio.NewScanner(reader) |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 68 | |
| 69 | // Set the buffer size to the maximum token size to avoid buffer overflows |
| 70 | scanner.Buffer(make([]byte, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize) |
| 71 | |
| 72 | // Define a split function to split the input into chunks of up to 64KB |
| 73 | chunkSize := bufio.MaxScanTokenSize // 64KB |
| 74 | splitFunc := func(data []byte, atEOF bool) (int, []byte, error) { |
| 75 | if len(data) >= chunkSize { |
| 76 | return chunkSize, data[:chunkSize], nil |
| 77 | } |
| 78 | |
| 79 | return bufio.ScanLines(data, atEOF) |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 80 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 81 | |
| 82 | // Use the custom split function to split the input |
| 83 | scanner.Split(splitFunc) |
| 84 | |
| 85 | // Scan the input and write it to the logger using the specified print function |
| 86 | for scanner.Scan() { |
| 87 | printFunc(strings.TrimRight(scanner.Text(), "\r\n")) |
| 88 | } |
| 89 | |
| 90 | // If there was an error while scanning the input, log an error |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 91 | if err := scanner.Err(); err != nil { |
| 92 | entry.Errorf("Error while reading from Writer: %s", err) |
| 93 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 94 | |
| 95 | // Close the reader when we are done |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 96 | reader.Close() |
| 97 | } |
| 98 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 99 | // WriterFinalizer is a finalizer function that closes then given writer when it is garbage collected |
| khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 100 | func writerFinalizer(writer *io.PipeWriter) { |
| 101 | writer.Close() |
| 102 | } |