btclog icon indicating copy to clipboard operation
btclog copied to clipboard

Add TestLogger for testing purposes

Open stevenroose opened this issue 7 years ago • 3 comments

When unit testing a subpackage, it's often useful to see log messages.

For this, it would be possible to add something like this in a _test.go file:

func init() {
    log = btclog.TestLogger
}

stevenroose avatar Dec 05 '17 16:12 stevenroose

I would prefer that log messages are written to the testing.T logger, so they only appear when the test fails or the verbose flag is used. I don't think there's a great way to handle that from the btclog package though, since it can't import testing to implement the required interface.

jrick avatar Dec 05 '17 18:12 jrick

Also with the explicit type attached? Let me check that tomorrow.

On 5 Dec 2017 19:26, "Josh Rickmar" [email protected] wrote:

@jrick commented on this pull request.

In log.go https://github.com/btcsuite/btclog/pull/14#discussion_r155034238:

-func init() {

  • Disabled = &slog{lvl: LevelOff, b: NewBackend(ioutil.Discard)}

fyi, this was done just to remove some ugly from the godoc page which would otherwise show the variable initialized to an unexported type, which isn't super helpful

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/btcsuite/btclog/pull/14#pullrequestreview-81286762, or mute the thread https://github.com/notifications/unsubscribe-auth/AA0F3EcfzQrBVu_cNthZ-r_AZ2lCorY4ks5s9YphgaJpZM4Q2kJH .

stevenroose avatar Dec 05 '17 18:12 stevenroose

also, this should already be possible from a test file with something like:

func init() {
    SetLogger(btclog.NewBackend(os.Stdout).Logger("TEST"))
}

which is the same number of lines as what it would require with this patch.

After considering what I really want though, which is to write to the testing.T logger, I don't believe this is currently possible, and definitely not if using parallel tests, because the testing.T is different for each test function while the log variable is a package global and can't be modified by several tests concurrently. We would need some refactoring to allow the logger to be a dependency of any created object, which is something I've advocated for before with little to no success :(

jrick avatar Dec 05 '17 19:12 jrick