-
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
while testing, redirect output to testing.T #834
Comments
Hi @drewwells, I've you tried somehow to set the logger.Out field to os.Stdout instead of os.Stderr ? I wonder if the test environment doesn't do something Stdout in order to only have its output upon test failure. |
@drewwells it's slightly tricky since you need to get access to the In one of our projects at work we have this: package serenity
import (
"io"
"os"
"testing"
"github.com/sirupsen/logrus"
)
// LogCapturer reroutes testing.T log output
type LogCapturer interface {
Release()
}
type logCapturer struct {
*testing.T
origOut io.Writer
}
func (tl logCapturer) Write(p []byte) (n int, err error) {
tl.Logf((string)(p))
return len(p), nil
}
func (tl logCapturer) Release() {
logrus.SetOutput(tl.origOut)
}
// CaptureLog redirects logrus output to testing.Log
func CaptureLog(t *testing.T) LogCapturer {
lc := logCapturer{T: t, origOut: logrus.StandardLogger().Out}
if !testing.Verbose() {
logrus.SetOutput(lc)
}
return &lc
} But it does mean in every test you need to do: func TestFoo(t *testing.T) {
defer serenity.CaptureLog(t).Release()
…
} |
We can use
Note: we need to call |
This issue is stale because it has been open for 30 days with no activity. |
This issue was closed because it has been inactive for 14 days since being marked as stale. |
Is it possible to provide this util type by default? This wrapper is better implemented inside logrus itself than in user test packages. |
Years later, people are still just discarding logs. Our packages are complicated enough that logs are helpful. I'd just prefer they were ordered correctly in my test runs |
The testing package has many logging functions that are used for logging only when errors are encountered. It would be beneficial to be able to easily redirect output to this. I can't see a simple way to do this currently besides creating an io.Writer that outputs to testing.Logf.
The text was updated successfully, but these errors were encountered: