Skip to content

Specify a default LoggerProvider for tests. #39

@jamesross3

Description

@jamesross3

Log output can be helpful in diagnosing test failures, but witchcraft-go-logging requires a LoggerProvider to be set to actually emit useful logs. One workaround to this is including either of the methods specified below to set a LoggerProvider within a test package:

func TestMain(m *testing.M) {
    wlog.SetDefaultLoggerProvider(wlogzap.LoggerProvider())
    os.Exit(m.Run())
}

func init() {
    wlog.SetDefaultLoggerProvider(wlogzap.LoggerProvider())
}

This is cumbersome, since every test package would require setting this. Furthermore, the go community seems to have inconsistencies regarding test package naming - for a package foo, tests can be written either in foo or foo_test. I believe this means that the init/TestMain method approach above could actually clobber another init method's SetDefaultLoggerProvider call (not sure what guarantees go makes about ordering init methods across packages).

Proposed solution:
Add an init method to witchcraft-go-logging that checks if we're running in a test and, if this is the case, set some LoggerProvider:

func init() {
  executable, err := os.Executable()
  if err != nil {
    // log or just return
  }
  // see https://golang.org/cmd/go/#hdr-Testing_flags
  if strings.HasSuffix(executable, ".test") {
    wlog.SetDefaultLoggerProvider(loggerProviderBackedByStdLibraryLogger)
  }
}

@bmoylan @nmiyake thoughts? I am happy to contribute this as well as a stdlog LoggerProvider implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions