Skip to content
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

Closed
drewwells opened this issue Oct 4, 2018 · 7 comments
Closed

while testing, redirect output to testing.T #834

drewwells opened this issue Oct 4, 2018 · 7 comments

Comments

@drewwells
Copy link

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.

@dgsb
Copy link
Collaborator

dgsb commented Oct 20, 2018

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.

@dgsb dgsb added the question label Oct 20, 2018
@pkqk
Copy link

pkqk commented Dec 10, 2018

@drewwells it's slightly tricky since you need to get access to the *testing.T instance of each test, and it is hardcoded to look back a certain number of stack frames for the calling location.

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()
  …
}

@adamcavendish
Copy link

adamcavendish commented May 27, 2020

We can use logrus.SetOutput(ioutil.Discard) to disable output.

import (
	"io/ioutil"
        "testing"
	"github.com/sirupsen/logrus"
)

func TestFoo(t *testing.T) {
	logrus.Info("Logrus in Testing")
}

func TestMain(m *testing.M) {
	logrus.SetOutput(ioutil.Discard)
	os.Exit(m.Run())
}

Note: we need to call m.Run() to execute the test after defining the TestMain.

@github-actions
Copy link

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Sep 12, 2021
@github-actions
Copy link

This issue was closed because it has been inactive for 14 days since being marked as stale.

@SOF3
Copy link

SOF3 commented Jun 5, 2023

Is it possible to provide this util type by default? This wrapper is better implemented inside logrus itself than in user test packages.

@drewwells
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants