diff --git a/profiler/profiler.go b/profiler/profiler.go index 3be07af1a6c3..0d7fdb6a1fee 100644 --- a/profiler/profiler.go +++ b/profiler/profiler.go @@ -39,6 +39,7 @@ import ( "context" "errors" "fmt" + "io" "log" "os" "regexp" @@ -130,6 +131,10 @@ type Config struct { // defaults to false. DebugLogging bool + // DebugLoggingOutput is where the logger will write debug logs to, if enabled. + // It defaults to os.Stderr. + DebugLoggingOutput io.Writer + // MutexProfiling enables mutex profiling. It defaults to false. // Note that mutex profiling is not supported by Go versions older // than Go 1.8. @@ -225,7 +230,10 @@ func Start(cfg Config, options ...option.ClientOption) error { } func start(cfg Config, options ...option.ClientOption) error { - logger = log.New(os.Stderr, "Cloud Profiler: ", log.LstdFlags) + if cfg.DebugLoggingOutput == nil { + cfg.DebugLoggingOutput = os.Stderr + } + logger = log.New(cfg.DebugLoggingOutput, "Cloud Profiler: ", log.LstdFlags) if err := initializeConfig(cfg); err != nil { debugLog("failed to initialize config: %v", err) return err diff --git a/profiler/profiler_test.go b/profiler/profiler_test.go index bcebbd4048c8..63140762314b 100644 --- a/profiler/profiler_test.go +++ b/profiler/profiler_test.go @@ -972,13 +972,16 @@ func TestAgentWithServer(t *testing.T) { quitProfilee := make(chan bool) go profileeLoop(quitProfilee) + var logs bytes.Buffer if err := Start(Config{ - Service: testService, - ProjectID: testProjectID, - APIAddr: srv.Addr, - Instance: testInstance, - Zone: testZone, - numProfiles: 2, + Service: testService, + ProjectID: testProjectID, + APIAddr: srv.Addr, + Instance: testInstance, + Zone: testZone, + numProfiles: 2, + DebugLogging: true, + DebugLoggingOutput: &logs, }); err != nil { t.Fatalf("Start(): %v", err) } @@ -997,6 +1000,10 @@ func TestAgentWithServer(t *testing.T) { t.Errorf("validateProfile(%s) got error: %v", pType, err) } } + + if logs.Len() == 0 { + t.Error("expected some debug logging output, but got none") + } } // testConnPool is a gtransport.ConnPool used for testing.