diff --git a/fixchain/chainfix/chainfix.go b/fixchain/chainfix/chainfix.go index 8720d1fd2b..603365bc0d 100644 --- a/fixchain/chainfix/chainfix.go +++ b/fixchain/chainfix/chainfix.go @@ -42,7 +42,7 @@ func processChains(file string, fl *fixchain.FixAndLog) { } defer func() { if err := f.Close(); err != nil { - log.Fatalf("Can't close file: %v\n", err) + log.Fatalf("Can't close file: %v", err) } }() @@ -90,7 +90,7 @@ func contentStore(baseDir string, subDir string, content []byte) { } defer func() { if err := f.Close(); err != nil { - log.Fatalf("Can't close file: %v\n", err) + log.Fatalf("Can't close file: %v", err) } }() if _, err := f.Write(content); err != nil { diff --git a/fixchain/roundtrip_test.go b/fixchain/roundtrip_test.go index 7a494626b1..3a88811b57 100644 --- a/fixchain/roundtrip_test.go +++ b/fixchain/roundtrip_test.go @@ -211,7 +211,7 @@ func (rt postTestRoundTripper) RoundTrip(request *http.Request) (*http.Response, rt.t.Error(errStr) return nil, errors.New(errStr) } - + if err != nil { errStr := fmt.Sprintf("#%d: Could not read request body: %s", rt.testIndex, err.Error()) rt.t.Error(errStr) diff --git a/fixchain/url_cache.go b/fixchain/url_cache.go index 29d7083e7b..ebbe52ebf5 100644 --- a/fixchain/url_cache.go +++ b/fixchain/url_cache.go @@ -17,11 +17,12 @@ package fixchain import ( "fmt" "io" - "log" "net/http" "sync" "sync/atomic" "time" + + "k8s.io/klog/v2" ) type lockedCache struct { @@ -68,9 +69,9 @@ func (u *urlCache) getURL(url string) ([]byte, error) { atomic.AddUint32(&u.errors, 1) return nil, err } - defer func(){ + defer func() { if err := c.Body.Close(); err != nil { - log.Fatalf("Operation to close response body failed: %v\n", err) + klog.Errorf("Operation to close response body failed: %v", err) } }() // TODO(katjoyce): Add caching of permanent errors. @@ -95,7 +96,7 @@ func newURLCache(c *http.Client, logStats bool) *urlCache { t := time.NewTicker(time.Second) go func() { for range t.C { - log.Printf("url cache: %d hits, %d misses, %d errors, "+ + klog.Infof("url cache: %d hits, %d misses, %d errors, "+ "%d bad status, %d read fail, %d cached", u.hit, u.miss, u.errors, u.badStatus, u.readFail, len(u.cache.m)) diff --git a/internal/witness/client/http/witness_client.go b/internal/witness/client/http/witness_client.go index ee5ab97c54..c7c1d06d9a 100644 --- a/internal/witness/client/http/witness_client.go +++ b/internal/witness/client/http/witness_client.go @@ -22,12 +22,12 @@ import ( "errors" "fmt" "io" - "log" "net/http" "net/url" "os" wit_api "github.com/google/certificate-transparency-go/internal/witness/api" + "k8s.io/klog/v2" ) // ErrSTHTooOld is returned if the STH passed to Update needs to be updated. @@ -54,7 +54,7 @@ func (w Witness) GetLatestSTH(ctx context.Context, logID string) ([]byte, error) } defer func() { if err := resp.Body.Close(); err != nil { - log.Fatalf("Failed to close response body: %v\n", err) + klog.Errorf("Failed to close response body: %v", err) } }() if resp.StatusCode == 404 { @@ -94,7 +94,7 @@ func (w Witness) Update(ctx context.Context, logID string, sth []byte, proof [][ } defer func() { if err := resp.Body.Close(); err != nil { - log.Fatalf("Failed to close response body: %v\n", err) + klog.Errorf("Failed to close response body: %v", err) } }() body, err := io.ReadAll(resp.Body) diff --git a/internal/witness/cmd/client/main.go b/internal/witness/cmd/client/main.go index 8bd7ef621b..a6ed2b7f03 100644 --- a/internal/witness/cmd/client/main.go +++ b/internal/witness/cmd/client/main.go @@ -218,7 +218,7 @@ func readHTTP(u *url.URL) ([]byte, error) { } defer func() { if err := resp.Body.Close(); err != nil { - klog.Exitf("failed to close response body %v\n", err) + klog.Errorf("failed to close response body %v", err) } }() return io.ReadAll(resp.Body) diff --git a/internal/witness/cmd/feeder/main.go b/internal/witness/cmd/feeder/main.go index 3706e941d0..0d097df7c2 100644 --- a/internal/witness/cmd/feeder/main.go +++ b/internal/witness/cmd/feeder/main.go @@ -24,7 +24,6 @@ import ( "flag" "fmt" "io" - "log" "net/http" "net/url" "os" @@ -261,7 +260,7 @@ func readHTTP(u *url.URL) ([]byte, error) { } defer func() { if err := resp.Body.Close(); err != nil { - log.Fatalf("Failed to close response body: %v\n", err) + klog.Errorf("Failed to close response body: %v", err) } }() return io.ReadAll(resp.Body) diff --git a/internal/witness/cmd/witness/config/config.go b/internal/witness/cmd/witness/config/config.go index e40b59cae9..cb66aada59 100644 --- a/internal/witness/cmd/witness/config/config.go +++ b/internal/witness/cmd/witness/config/config.go @@ -20,7 +20,6 @@ import ( "flag" "fmt" "io" - "log" "net/http" "net/url" "os" @@ -87,7 +86,7 @@ func readHTTP(u *url.URL) ([]byte, error) { } defer func() { if err := resp.Body.Close(); err != nil { - log.Fatalf("Operation to close response body failed: %v\n", err) + klog.Errorf("Operation to close response body failed: %v", err) } }() return io.ReadAll(resp.Body) diff --git a/internal/witness/cmd/witness/internal/witness/witness.go b/internal/witness/cmd/witness/internal/witness/witness.go index 7aa5ec6677..bd158af965 100644 --- a/internal/witness/cmd/witness/internal/witness/witness.go +++ b/internal/witness/cmd/witness/internal/witness/witness.go @@ -27,7 +27,6 @@ import ( "encoding/pem" "errors" "fmt" - "log" "reflect" ct "github.com/google/certificate-transparency-go" @@ -120,9 +119,9 @@ func (w *Witness) GetLogs() ([]string, error) { if err != nil { return nil, err } - defer func(){ + defer func() { if err := rows.Close(); err != nil { - log.Fatalf("Operation to close rows failed: %v\n", err) + klog.Errorf("Operation to close rows failed: %v", err) } }() var logs []string diff --git a/jsonclient/client_test.go b/jsonclient/client_test.go index 5f169ab868..f4d439e686 100644 --- a/jsonclient/client_test.go +++ b/jsonclient/client_test.go @@ -145,11 +145,11 @@ func MockServer(t *testing.T, failCount int, retryAfter int) *httptest.Server { decoder := json.NewDecoder(r.Body) err := decoder.Decode(&s) if err != nil { - panic("Failed to decode: " + err.Error()) + t.Fatalf("Failed to decode: " + err.Error()) } defer func() { if err := r.Body.Close(); err != nil { - panic("Failed to close request body: " + err.Error()) + t.Fatalf("Failed to close request body: " + err.Error()) } }() } @@ -162,11 +162,11 @@ func MockServer(t *testing.T, failCount int, retryAfter int) *httptest.Server { decoder := json.NewDecoder(r.Body) err := decoder.Decode(¶ms) if err != nil { - panic("Failed to decode: " + err.Error()) + t.Fatalf("Failed to decode: " + err.Error()) } defer func() { if err := r.Body.Close(); err != nil { - panic("Failed to close request body: " + err.Error()) + t.Fatalf("Failed to close request body: " + err.Error()) } }() } diff --git a/submission/loglist_manager_test.go b/submission/loglist_manager_test.go index 1c9e735c11..d6e1c4d3ec 100644 --- a/submission/loglist_manager_test.go +++ b/submission/loglist_manager_test.go @@ -54,7 +54,7 @@ func TestFirstRefresh(t *testing.T) { } defer func() { if err := os.Remove(f); err != nil { - t.Fatalf("Operation to remove temp file failed: %v\n", err) + t.Fatalf("Operation to remove temp file failed: %v", err) } }() @@ -81,7 +81,7 @@ func TestSecondRefresh(t *testing.T) { } defer func() { if err := os.Remove(f); err != nil { - t.Fatalf("Operation to remove temp file failed: %v\n", err) + t.Fatalf("Operation to remove temp file failed: %v", err) } }() diff --git a/submission/loglist_refresher_test.go b/submission/loglist_refresher_test.go index a144fe7cc8..e16a5f2baf 100644 --- a/submission/loglist_refresher_test.go +++ b/submission/loglist_refresher_test.go @@ -37,17 +37,14 @@ func createTempFile(data string) (string, error) { if err != nil { return "", err } - defer func(){ + defer func() { if err := f.Close(); err != nil { - log.Fatalf("Operation to close file failed: %v\n", err) + log.Fatalf("Operation to close file failed: %v", err) } }() if _, err := f.WriteString(data); err != nil { return "", err } - // if err := f.Close(); err != nil { - // return "", err - // } return f.Name(), nil } @@ -61,7 +58,7 @@ func ExampleLogListRefresher() { } defer func() { if err := os.Remove(f); err != nil { - log.Fatalf("Operation to remove temp file failed: %v\n", err) + log.Fatalf("Operation to remove temp file failed: %v", err) } }() @@ -148,7 +145,7 @@ func TestNewLogListRefresher(t *testing.T) { } defer func() { if err := os.Remove(f); err != nil { - log.Fatalf("Operation to remove temp file failed: %v\n", err) + t.Fatalf("Operation to remove temp file failed: %v", err) } }() @@ -218,7 +215,7 @@ func TestNewLogListRefresherUpdate(t *testing.T) { } defer func() { if err := os.Remove(f); err != nil { - log.Fatalf("Operation to remove temp file failed: %v\n", err) + t.Fatalf("Operation to remove temp file failed: %v", err) } }() diff --git a/submission/proxy_test.go b/submission/proxy_test.go index 19be02fdc8..4399a08e9d 100644 --- a/submission/proxy_test.go +++ b/submission/proxy_test.go @@ -98,7 +98,7 @@ func TestProxyInitState(t *testing.T) { } defer func() { if err := os.Remove(f); err != nil { - t.Fatalf("Operation to remove temp file failed: %v\n", err) + t.Fatalf("Operation to remove temp file failed: %v", err) } }() diff --git a/trillian/ctfe/ct_server/main.go b/trillian/ctfe/ct_server/main.go index 430212c105..0aa27a21df 100644 --- a/trillian/ctfe/ct_server/main.go +++ b/trillian/ctfe/ct_server/main.go @@ -112,12 +112,12 @@ func main() { } if err != nil { - klog.Errorf("Failed to read config: %v", err) + klog.Exitf("Failed to read config: %v", err) } beMap, err := ctfe.ValidateLogMultiConfig(cfg) if err != nil { - klog.Errorf("Invalid config: %v", err) + klog.Exitf("Invalid config: %v", err) } klog.CopyStandardLogTo("WARNING") @@ -134,16 +134,16 @@ func main() { cfg := clientv3.Config{Endpoints: strings.Split(*etcdServers, ","), DialTimeout: 5 * time.Second} client, err := clientv3.New(cfg) if err != nil { - klog.Errorf("Failed to connect to etcd at %v: %v", *etcdServers, err) + klog.Exitf("Failed to connect to etcd at %v: %v", *etcdServers, err) } httpManager, err := endpoints.NewManager(client, *etcdHTTPService) if err != nil { - klog.Errorf("Failed to create etcd http manager: %v", err) + klog.Exitf("Failed to create etcd http manager: %v", err) } metricsManager, err := endpoints.NewManager(client, *etcdMetricsService) if err != nil { - klog.Errorf("Failed to create etcd metrics manager: %v", err) + klog.Exitf("Failed to create etcd metrics manager: %v", err) } etcdHTTPKey := fmt.Sprintf("%s/%s", *etcdHTTPService, *httpEndpoint) @@ -197,13 +197,13 @@ func main() { } conn, err := grpc.Dial(be.BackendSpec, dialOpts...) if err != nil { - klog.Errorf("Could not dial RPC server: %v: %v", be, err) + klog.Exitf("Could not dial RPC server: %v: %v", be, err) } - defer func(){ + defer func() { if err := conn.Close(); err != nil { klog.Errorf("Could not close RPC connection: %v", err) } - }() + }() clientMap[be.Name] = trillian.NewTrillianLogClient(conn) } @@ -220,7 +220,7 @@ func main() { for _, c := range cfg.LogConfigs.Config { inst, err := setupAndRegister(ctx, clientMap[c.LogBackendName], *rpcDeadline, c, corsMux, *handlerPrefix, *maskInternalErrors) if err != nil { - klog.Errorf("Failed to set up log instance for %+v: %v", cfg, err) + klog.Exitf("Failed to set up log instance for %+v: %v", cfg, err) } if *getSTHInterval > 0 { go inst.RunUpdateSTH(ctx, *getSTHInterval) @@ -285,7 +285,7 @@ func main() { if *tracing { handler, err = opencensus.EnableHTTPServerTracing(*tracingProjectID, *tracingPercent) if err != nil { - klog.Errorf("Failed to initialize stackdriver / opencensus tracing: %v", err) + klog.Exitf("Failed to initialize stackdriver / opencensus tracing: %v", err) } } diff --git a/trillian/integration/ct_hammer/main.go b/trillian/integration/ct_hammer/main.go index 4bc1db6a2a..4d1d30fae9 100644 --- a/trillian/integration/ct_hammer/main.go +++ b/trillian/integration/ct_hammer/main.go @@ -257,7 +257,7 @@ func main() { klog.Exitf("Failed to print banner!") } if err := r.Close(); err != nil { - klog.Exitf("Failed to close reader %v\n", err) + klog.Exitf("Failed to close reader %v", err) } fmt.Print("\n\nHammer Time\n\n") } diff --git a/trillian/integration/ct_integration.go b/trillian/integration/ct_integration.go index 1be39ad2bc..96e4206f53 100644 --- a/trillian/integration/ct_integration.go +++ b/trillian/integration/ct_integration.go @@ -25,7 +25,6 @@ import ( "crypto/sha256" "encoding/pem" "fmt" - "log" "math/rand" "net" "net/http" @@ -914,7 +913,7 @@ func (ls *logStats) fromServer(ctx context.Context, servers string) (*logStats, } defer func() { if err := httpRsp.Body.Close(); err != nil { - log.Fatalf("Operation to close http response body failed: %v\n", err) + fmt.Printf("Operation to close http response body failed: %v\n", err) } }() @@ -988,9 +987,9 @@ func setTreeState(ctx context.Context, adminServer string, logID int64, state tr if err != nil { return err } - defer func(){ + defer func() { if err := conn.Close(); err != nil { - log.Fatalf("Operation to close RPC connection failed: %v\n", err) + fmt.Printf("Operation to close RPC connection failed: %v\n", err) } }() diff --git a/trillian/integration/hammer_test.go b/trillian/integration/hammer_test.go index d884321737..1a8926b699 100644 --- a/trillian/integration/hammer_test.go +++ b/trillian/integration/hammer_test.go @@ -222,12 +222,12 @@ func (s *fakeCTServer) addChain(w http.ResponseWriter, req *http.Request) { func (s *fakeCTServer) close() { if s.server != nil { if err := s.server.Close(); err != nil { - klog.Errorf("Operation to close server failed: %v\n", err) + klog.Errorf("Operation to close server failed: %v", err) } } if s.lis != nil { if err := s.lis.Close(); err != nil { - klog.Errorf("Operation to close server listener failed: %v\n", err) + klog.Errorf("Operation to close server listener failed: %v", err) } } } diff --git a/trillian/integration/logenv.go b/trillian/integration/logenv.go index 7b745e10f4..8e7fb4b559 100644 --- a/trillian/integration/logenv.go +++ b/trillian/integration/logenv.go @@ -115,7 +115,7 @@ func NewCTLogEnv(ctx context.Context, cfgs []*configpb.LogConfig, numSequencers // Close shuts down the servers. func (env *CTLogEnv) Close() { if err := env.ctListener.Close(); err != nil { - log.Fatalf("Operation to close listener failed: %v\n", err) + log.Fatalf("Operation to close listener failed: %v", err) } env.wg.Wait() env.logEnv.Close() diff --git a/trillian/migrillian/main.go b/trillian/migrillian/main.go index 6eafde1405..04b3121895 100644 --- a/trillian/migrillian/main.go +++ b/trillian/migrillian/main.go @@ -84,7 +84,7 @@ func main() { defer func() { if err := conn.Close(); err != nil { - klog.Exitf("Could not close RPC connection: %v\n", err) + klog.Errorf("Could not close RPC connection: %v", err) } }()