-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error with Entry.writerScanner: token too long #564
Comments
We've run into this issue in TritonDataCenter/containerpilot#423. In our case it's happening because we're piping stdout of a child process and can't enforce that we're terminating it with newlines. (It's an end-user error for our project, but we still need to handle it gracefully.) Looking at the implementation here, I'd argue that simply increasing the token size just pushes the issue off. I'll see if I can figure out a way to gracefully handle this error by handling |
I agree with you, increasing buffer size is not a solution. |
In my opinion logrus shouldn't provide I've expressed a similar opinion in another open issue where @tgross I've checked your package main
import (
"bytes"
"github.com/sirupsen/logrus"
"os/exec"
"sync"
)
func main() {
w := &logrusWriter{
entry: logrus.StandardLogger().WithField("logger", "cmd"),
}
cmd := exec.Command("sh", "-c", "echo stdout; echo 1>&2 stderr")
cmd.Stdout = w
cmd.Stderr = w
err := cmd.Start()
if err != nil {
logrus.Fatalf("Cmd.Start failed: %v", err)
}
err = cmd.Wait()
w.Flush()
if err != nil {
logrus.Fatalf("Cmd.Wait failed: %v", err)
}
}
type logrusWriter struct {
entry *logrus.Entry
buf bytes.Buffer
mu sync.Mutex
}
func (w *logrusWriter) Write(b []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
origLen := len(b)
for {
if len(b) == 0 {
return origLen, nil
}
i := bytes.IndexByte(b, '\n')
if i < 0 {
w.buf.Write(b)
return origLen, nil
}
w.buf.Write(b[:i])
w.alwaysFlush()
b = b[i+1:]
}
}
func (w *logrusWriter) alwaysFlush() {
w.entry.Info(w.buf.String())
w.buf.Reset()
}
func (w *logrusWriter) Flush() {
w.mu.Lock()
defer w.mu.Unlock()
if w.buf.Len() != 0 {
w.alwaysFlush()
}
} Some possible modifications that one might want in his/her own writer implementation:
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Also encountering this issue when piping the output of a process with many progress-bar messages like
|
If you pass too long string (or byte slice) without newlines to *io.PipeWriter, provided by Entry.WriterLevel, you can see errors like this:
Error while reading from Writer: bufio.Scanner: token too long
.Sample code to reproduce this issue:
This is head of output on my machine:
This error (ErrTooLong) is set in Scanner.Scan if buffer length is greater than max token size.
Maximum token size is defined as constant - bufio.MaxScanTokenSize. Current value is 65536. But you can override it with passing custom buffer and max token size to Scanner.Buffer.
So I see 2 ways of fixing this error:
The text was updated successfully, but these errors were encountered: