4

Hi I want to check that the output of a Log.error() is valid, I have this following code here

func logError(param bool)
bool {
    if (param == true)
        log.Error(fmt.Sprintf("I need to somehow tst, that the output here is correct"))

    return param;
}

I am not allowed to modify my existing code, I just want to ensure that whatever is printed by the console by my logError function, is what I expect it to be.

Is this possible without modifying my existing code, thank you.

3
  • The answer depends on which log library you are using. Commented Oct 26, 2021 at 2:17
  • Apologies, I have just added my Log library. Commented Oct 26, 2021 at 2:36
  • The Log library I am using is github.com/sirupsen/logrus Commented Oct 26, 2021 at 2:37

1 Answer 1

4

With logrus, you can capture the output of the logger in a test:

oldOut:=log.StandardLogger().Out // Save current log target
buf:=bytes.Buffer{}
log.SetOutput(&buf)
logError(true)
// Here, buf.String() should give you the log msg
log.SetOutput(oldOut) // Restore log target
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.