2023-09-28 16:45:44 +02:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"runtime"
|
2023-11-14 16:07:41 +02:00
|
|
|
"strings"
|
2023-09-28 16:45:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Writer at INFO level. See WriterLevel for details.
|
|
|
|
func (logger *Logger) Writer() *io.PipeWriter {
|
|
|
|
return logger.WriterLevel(InfoLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriterLevel returns an io.Writer that can be used to write arbitrary text to
|
|
|
|
// the logger at the given log level. Each line written to the writer will be
|
|
|
|
// printed in the usual way using formatters and hooks. The writer is part of an
|
|
|
|
// io.Pipe and it is the callers responsibility to close the writer when done.
|
|
|
|
// This can be used to override the standard library logger easily.
|
|
|
|
func (logger *Logger) WriterLevel(level Level) *io.PipeWriter {
|
|
|
|
return NewEntry(logger).WriterLevel(level)
|
|
|
|
}
|
|
|
|
|
2023-11-14 16:07:41 +02:00
|
|
|
// Writer returns an io.Writer that writes to the logger at the info log level
|
2023-09-28 16:45:44 +02:00
|
|
|
func (entry *Entry) Writer() *io.PipeWriter {
|
|
|
|
return entry.WriterLevel(InfoLevel)
|
|
|
|
}
|
|
|
|
|
2023-11-14 16:07:41 +02:00
|
|
|
// WriterLevel returns an io.Writer that writes to the logger at the given log level
|
2023-09-28 16:45:44 +02:00
|
|
|
func (entry *Entry) WriterLevel(level Level) *io.PipeWriter {
|
|
|
|
reader, writer := io.Pipe()
|
|
|
|
|
|
|
|
var printFunc func(args ...interface{})
|
|
|
|
|
2023-11-14 16:07:41 +02:00
|
|
|
// Determine which log function to use based on the specified log level
|
2023-09-28 16:45:44 +02:00
|
|
|
switch level {
|
|
|
|
case TraceLevel:
|
|
|
|
printFunc = entry.Trace
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-14 16:07:41 +02:00
|
|
|
// Start a new goroutine to scan the input and write it to the logger using the specified print function.
|
|
|
|
// It splits the input into chunks of up to 64KB to avoid buffer overflows.
|
2023-09-28 16:45:44 +02:00
|
|
|
go entry.writerScanner(reader, printFunc)
|
2023-11-14 16:07:41 +02:00
|
|
|
|
|
|
|
// Set a finalizer function to close the writer when it is garbage collected
|
2023-09-28 16:45:44 +02:00
|
|
|
runtime.SetFinalizer(writer, writerFinalizer)
|
|
|
|
|
|
|
|
return writer
|
|
|
|
}
|
|
|
|
|
2023-11-14 16:07:41 +02:00
|
|
|
// writerScanner scans the input from the reader and writes it to the logger
|
2023-09-28 16:45:44 +02:00
|
|
|
func (entry *Entry) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) {
|
|
|
|
scanner := bufio.NewScanner(reader)
|
2023-11-14 16:07:41 +02:00
|
|
|
|
|
|
|
// Set the buffer size to the maximum token size to avoid buffer overflows
|
|
|
|
scanner.Buffer(make([]byte, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize)
|
|
|
|
|
|
|
|
// Define a split function to split the input into chunks of up to 64KB
|
|
|
|
chunkSize := bufio.MaxScanTokenSize // 64KB
|
|
|
|
splitFunc := func(data []byte, atEOF bool) (int, []byte, error) {
|
|
|
|
if len(data) >= chunkSize {
|
|
|
|
return chunkSize, data[:chunkSize], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return bufio.ScanLines(data, atEOF)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the custom split function to split the input
|
|
|
|
scanner.Split(splitFunc)
|
|
|
|
|
|
|
|
// Scan the input and write it to the logger using the specified print function
|
2023-09-28 16:45:44 +02:00
|
|
|
for scanner.Scan() {
|
2023-11-14 16:07:41 +02:00
|
|
|
printFunc(strings.TrimRight(scanner.Text(), "\r\n"))
|
2023-09-28 16:45:44 +02:00
|
|
|
}
|
2023-11-14 16:07:41 +02:00
|
|
|
|
|
|
|
// If there was an error while scanning the input, log an error
|
2023-09-28 16:45:44 +02:00
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
entry.Errorf("Error while reading from Writer: %s", err)
|
|
|
|
}
|
2023-11-14 16:07:41 +02:00
|
|
|
|
|
|
|
// Close the reader when we are done
|
2023-09-28 16:45:44 +02:00
|
|
|
reader.Close()
|
|
|
|
}
|
|
|
|
|
2023-11-14 16:07:41 +02:00
|
|
|
// WriterFinalizer is a finalizer function that closes then given writer when it is garbage collected
|
2023-09-28 16:45:44 +02:00
|
|
|
func writerFinalizer(writer *io.PipeWriter) {
|
|
|
|
writer.Close()
|
|
|
|
}
|