package logrusimport ()// Writer at INFO level. See WriterLevel for details.func ( *Logger) () *io.PipeWriter {return .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) ( Level) *io.PipeWriter {returnNewEntry().WriterLevel()}// Writer returns an io.Writer that writes to the logger at the info log levelfunc ( *Entry) () *io.PipeWriter {return .WriterLevel(InfoLevel)}// WriterLevel returns an io.Writer that writes to the logger at the given log levelfunc ( *Entry) ( Level) *io.PipeWriter { , := io.Pipe() := .Print// Determine which log function to use based on the specified log levelswitch {caseTraceLevel: = .TracecaseDebugLevel: = .DebugcaseInfoLevel: = .InfocaseWarnLevel: = .WarncaseErrorLevel: = .ErrorcaseFatalLevel: = .FatalcasePanicLevel: = .Panic }// 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.go .writerScanner(, )// Set a finalizer function to close the writer when it is garbage collectedruntime.SetFinalizer(, writerFinalizer)return}// writerScanner scans the input from the reader and writes it to the loggerfunc ( *Entry) ( *io.PipeReader, func( ...any)) { := bufio.NewScanner()// Set the buffer size to the maximum token size to avoid buffer overflows .Buffer(make([]byte, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize)// Define a split function to split the input into chunks of up to 64KB := bufio.MaxScanTokenSize// 64KB := func( []byte, bool) (int, []byte, error) {iflen() >= {return , [:], nil }returnbufio.ScanLines(, ) }// Use the custom split function to split the input .Split()// Scan the input and write it to the logger using the specified print functionfor .Scan() { (strings.TrimRight(.Text(), "\r\n")) }// If there was an error while scanning the input, log an errorif := .Err(); != nil { .Errorf("Error while reading from Writer: %s", ) }// Close the reader when we are done .Close()}// WriterFinalizer is a finalizer function that closes then given writer when it is garbage collectedfunc writerFinalizer( *io.PipeWriter) { .Close()}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.