Skip to content

Instantly share code, notes, and snippets.

@logie17
Created January 20, 2016 23:40
Show Gist options
  • Save logie17/3e79f010db4c6b4c714d to your computer and use it in GitHub Desktop.
Save logie17/3e79f010db4c6b4c714d to your computer and use it in GitHub Desktop.
var logLines []string
type logMock struct{}
func (l *logMock) Write(p []byte) (int, error) {
logLines = append(logLines, string(p))
return 0, nil
}
//CaptureLogOutput A utility to capture the log output of a function and pass it along
func CaptureLogOutput(toBeCaptured func(), handleResults func([]string)) {
logLines = []string{}
currentFlags := log.Flags()
log.SetFlags(0)
log.SetOutput(&logMock{})
toBeCaptured()
log.SetOutput(os.Stderr)
log.SetFlags(currentFlags)
handleResults(logLines)
}
@logie17
Copy link
Author

logie17 commented Jan 29, 2016

Great idea, all ideas implemented: https://github.com/MediaMath/auth_go_common_libs

@logie17
Copy link
Author

logie17 commented Jan 29, 2016

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