From c10a9103f959498c360be002b2621e978bb82e19 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Thu, 5 May 2022 22:45:13 +0200 Subject: replace glog with sigsum-go's log package --- cmd/sigsum_log_go/main.go | 64 ++++++++++++++++++++++++++++++++------------ go.mod | 1 - integration/test.sh | 4 ++- pkg/db/trillian.go | 4 +-- pkg/instance/experimental.go | 4 +-- pkg/instance/handler.go | 18 ++++++------- pkg/state/single.go | 10 +++---- 7 files changed, 68 insertions(+), 37 deletions(-) diff --git a/cmd/sigsum_log_go/main.go b/cmd/sigsum_log_go/main.go index 356726a..4ab9c2a 100644 --- a/cmd/sigsum_log_go/main.go +++ b/cmd/sigsum_log_go/main.go @@ -16,11 +16,11 @@ import ( "syscall" "time" - "github.com/golang/glog" "github.com/google/trillian" "github.com/prometheus/client_golang/prometheus/promhttp" "google.golang.org/grpc" + "git.sigsum.org/sigsum-go/pkg/log" "git.sigsum.org/sigsum-go/pkg/types" "git.sigsum.org/sigsum-go/pkg/dns" "git.sigsum.org/log-go/pkg/db" @@ -39,14 +39,20 @@ var ( maxRange = flag.Int64("max_range", 10, "maximum number of entries that can be retrived in a single request") interval = flag.Duration("interval", time.Second*30, "interval used to rotate the log's cosigned STH") shardStart = flag.Int64("shard_interval_start", 0, "start of shard interval since the UNIX epoch in seconds") + logFile = flag.String("log-file", "", "file to write logs to (Default: stderr)") + logLevel = flag.String("log-level", "info", "log level (Available options: debug, info, warning, error. Default: info)") + logColor = flag.Bool("log-color", false, "colored logging output (Default: off)") gitCommit = "unknown" ) func main() { flag.Parse() - defer glog.Flush() - glog.Infof("sigsum-log-go git-commit %s", gitCommit) + + if err := setupLogging(*logFile, *logLevel, *logColor); err != nil { + log.Fatal("setup logging: %v", err) + } + log.Info("log-go git-commit %s", gitCommit) // wait for clean-up before exit var wg sync.WaitGroup @@ -54,41 +60,65 @@ func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - glog.V(3).Infof("configuring sigsum-log-go instance...") + log.Debug("configuring log-go instance") instance, err := setupInstanceFromFlags() if err != nil { - glog.Errorf("setupInstance: %v", err) - return + log.Fatal("setup instance: %v", err) } - glog.V(3).Infof("spawning state manager") + log.Debug("starting state manager routine") go func() { wg.Add(1) defer wg.Done() instance.Stateman.Run(ctx) - glog.Errorf("state manager shutdown") + log.Debug("state manager shutdown") cancel() // must have state manager running }() - glog.V(3).Infof("spawning await") + log.Debug("starting await routine") server := http.Server{Addr: *httpEndpoint} go await(ctx, func() { wg.Add(1) defer wg.Done() ctxInner, _ := context.WithTimeout(ctx, time.Second*60) - glog.Infof("Shutting down HTTP server...") + log.Info("stopping http server, please wait...") server.Shutdown(ctxInner) - glog.V(3).Infof("HTTP server shutdown") - glog.Infof("Shutting down spawned go routines...") + log.Info("stopping go routines, please wait...") cancel() }) - glog.Infof("Serving on %v/%v", *httpEndpoint, *prefix) + log.Info("serving on %v/%v", *httpEndpoint, *prefix) if err = server.ListenAndServe(); err != http.ErrServerClosed { - glog.Errorf("ListenAndServe: %v", err) + log.Error("serve: %v", err) } } +func setupLogging(logFile, logLevel string, logColor bool) error { + if len(logFile) != 0 { + f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return err + } + log.SetOutput(f) + } + + switch logLevel { + case "debug": + log.SetLevel(log.DebugLevel) + case "info": + log.SetLevel(log.InfoLevel) + case "warning": + log.SetLevel(log.WarningLevel) + case "error": + log.SetLevel(log.ErrorLevel) + default: + return fmt.Errorf("invalid logging level %s", logLevel) + } + + log.SetColor(logColor) + return nil +} + // SetupInstance sets up a new sigsum-log-go instance from flags func setupInstanceFromFlags() (*instance.Instance, error) { var i instance.Instance @@ -137,10 +167,10 @@ func setupInstanceFromFlags() (*instance.Instance, error) { mux := http.NewServeMux() http.Handle("/", mux) for _, handler := range i.Handlers() { - glog.V(3).Infof("adding handler: %s", handler.Path()) + log.Debug("adding handler: %s", handler.Path()) mux.Handle(handler.Path(), handler) } - glog.V(3).Infof("Adding prometheus handler on path: /metrics") + log.Debug("adding prometheus handler on path: /metrics") http.Handle("/metrics", promhttp.Handler()) return &i, nil @@ -184,6 +214,6 @@ func await(ctx context.Context, done func()) { case <-sigs: case <-ctx.Done(): } - glog.V(3).Info("received shutdown signal") + log.Debug("received shutdown signal") done() } diff --git a/go.mod b/go.mod index 8ae7fa4..579b196 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.15 require ( git.sigsum.org/sigsum-go v0.0.7 - github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b github.com/golang/mock v1.4.4 github.com/google/certificate-transparency-go v1.1.1 // indirect github.com/google/trillian v1.3.13 diff --git a/integration/test.sh b/integration/test.sh index 3bc967c..873d248 100755 --- a/integration/test.sh +++ b/integration/test.sh @@ -107,7 +107,9 @@ function sigsum_setup() { -witnesses=$ssrv_witnesses\ -interval=$ssrv_interval\ -http_endpoint=$ssrv_endpoint\ - -log_dir=$log_dir -v=3 2>/dev/null & + -log-color="true"\ + -log-level="debug"\ + -log-file=$log_dir/sigsum-log.log 2>/dev/null & ssrv_pid=$! log_url=$ssrv_endpoint/$ssrv_prefix/sigsum/v0 diff --git a/pkg/db/trillian.go b/pkg/db/trillian.go index 024a021..3147c8d 100644 --- a/pkg/db/trillian.go +++ b/pkg/db/trillian.go @@ -5,9 +5,9 @@ import ( "fmt" "time" + "git.sigsum.org/sigsum-go/pkg/log" "git.sigsum.org/sigsum-go/pkg/requests" "git.sigsum.org/sigsum-go/pkg/types" - "github.com/golang/glog" "github.com/google/trillian" trillianTypes "github.com/google/trillian/types" "google.golang.org/grpc/codes" @@ -33,7 +33,7 @@ func (c *TrillianClient) AddLeaf(ctx context.Context, req *requests.Leaf) error } serialized := leaf.ToBinary() - glog.V(3).Infof("queueing leaf request: %x", types.LeafHash(serialized)) + log.Debug("queueing leaf request: %x", types.LeafHash(serialized)) rsp, err := c.GRPC.QueueLeaf(ctx, &trillian.QueueLeafRequest{ LogId: c.TreeID, Leaf: &trillian.LogLeaf{ diff --git a/pkg/instance/experimental.go b/pkg/instance/experimental.go index 3db11e9..24feeaf 100644 --- a/pkg/instance/experimental.go +++ b/pkg/instance/experimental.go @@ -11,8 +11,8 @@ import ( "fmt" "net/http" + "git.sigsum.org/sigsum-go/pkg/log" "git.sigsum.org/sigsum-go/pkg/types" - "github.com/golang/glog" ) // algEd25519 identifies a checkpoint signature algorithm @@ -21,7 +21,7 @@ const algEd25519 byte = 1 // getCheckpoint is an experimental endpoint that is not part of the official // Sigsum API. Documentation can be found in the transparency-dev repo. func getCheckpoint(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { - glog.V(3).Info("handling get-checkpoint request") + log.Debug("handling get-checkpoint request") sth, err := i.Stateman.ToCosignTreeHead(ctx) if err != nil { return http.StatusInternalServerError, err diff --git a/pkg/instance/handler.go b/pkg/instance/handler.go index ecc22e2..fa465ee 100644 --- a/pkg/instance/handler.go +++ b/pkg/instance/handler.go @@ -6,8 +6,8 @@ import ( "net/http" "time" + "git.sigsum.org/sigsum-go/pkg/log" "git.sigsum.org/sigsum-go/pkg/types" - "github.com/golang/glog" ) // Handler implements the http.Handler interface, and contains a reference @@ -72,14 +72,14 @@ func (h Handler) handle(w http.ResponseWriter, r *http.Request) int { code, err := h.Handler(ctx, h.Instance, w, r) if err != nil { - glog.V(3).Infof("%s/%s: %v", h.Instance.Prefix, h.Endpoint, err) + log.Debug("%s/%s: %v", h.Instance.Prefix, h.Endpoint, err) http.Error(w, fmt.Sprintf("error=%s", err.Error()), code) } return code } func addLeaf(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { - glog.V(3).Info("handling add-entry request") + log.Debug("handling add-leaf request") req, err := i.leafRequestFromHTTP(ctx, r) if err != nil { return http.StatusBadRequest, err @@ -91,7 +91,7 @@ func addLeaf(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Re } func addCosignature(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { - glog.V(3).Info("handling add-cosignature request") + log.Debug("handling add-cosignature request") req, err := i.cosignatureRequestFromHTTP(r) if err != nil { return http.StatusBadRequest, err @@ -104,7 +104,7 @@ func addCosignature(ctx context.Context, i *Instance, w http.ResponseWriter, r * } func getTreeHeadToCosign(ctx context.Context, i *Instance, w http.ResponseWriter, _ *http.Request) (int, error) { - glog.V(3).Info("handling get-tree-head-to-cosign request") + log.Debug("handling get-tree-head-to-cosign request") sth, err := i.Stateman.ToCosignTreeHead(ctx) if err != nil { return http.StatusInternalServerError, err @@ -116,7 +116,7 @@ func getTreeHeadToCosign(ctx context.Context, i *Instance, w http.ResponseWriter } func getTreeHeadCosigned(ctx context.Context, i *Instance, w http.ResponseWriter, _ *http.Request) (int, error) { - glog.V(3).Info("handling get-tree-head-cosigned request") + log.Debug("handling get-tree-head-cosigned request") cth, err := i.Stateman.CosignedTreeHead(ctx) if err != nil { return http.StatusInternalServerError, err @@ -128,7 +128,7 @@ func getTreeHeadCosigned(ctx context.Context, i *Instance, w http.ResponseWriter } func getConsistencyProof(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { - glog.V(3).Info("handling get-consistency-proof request") + log.Debug("handling get-consistency-proof request") req, err := i.consistencyProofRequestFromHTTP(r) if err != nil { return http.StatusBadRequest, err @@ -146,7 +146,7 @@ func getConsistencyProof(ctx context.Context, i *Instance, w http.ResponseWriter } func getInclusionProof(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { - glog.V(3).Info("handling get-proof-by-hash request") + log.Debug("handling get-inclusion-proof request") req, err := i.inclusionProofRequestFromHTTP(r) if err != nil { return http.StatusBadRequest, err @@ -164,7 +164,7 @@ func getInclusionProof(ctx context.Context, i *Instance, w http.ResponseWriter, } func getLeaves(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { - glog.V(3).Info("handling get-leaves request") + log.Debug("handling get-leaves request") req, err := i.leavesRequestFromHTTP(r) if err != nil { return http.StatusBadRequest, err diff --git a/pkg/state/single.go b/pkg/state/single.go index e1f0e75..695f0e3 100644 --- a/pkg/state/single.go +++ b/pkg/state/single.go @@ -9,8 +9,8 @@ import ( "time" "git.sigsum.org/log-go/pkg/db" + "git.sigsum.org/sigsum-go/pkg/log" "git.sigsum.org/sigsum-go/pkg/types" - "github.com/golang/glog" ) // StateManagerSingle implements a single-instance StateManager @@ -50,7 +50,7 @@ func (sm *StateManagerSingle) Run(ctx context.Context) { rotation := func() { nextSTH, err := sm.latestSTH(ctx) if err != nil { - glog.Warningf("cannot rotate without tree head: %v", err) + log.Warning("cannot rotate without tree head: %v", err) return } sm.rotate(nextSTH) @@ -108,21 +108,21 @@ func (sm *StateManagerSingle) rotate(nextSTH *types.SignedTreeHead) { sm.Lock() defer sm.Unlock() - glog.V(3).Infof("rotating tree heads") + log.Debug("rotating tree heads") sm.handleEvents() sm.setCosignedTreeHead() sm.setToCosignTreeHead(nextSTH) } func (sm *StateManagerSingle) handleEvents() { - glog.V(3).Infof("handling any outstanding events") + log.Debug("handling any outstanding events") for i, n := 0, len(sm.events); i < n; i++ { sm.handleEvent(<-sm.events) } } func (sm *StateManagerSingle) handleEvent(ev *event) { - glog.V(3).Infof("handling event from witness %x", ev.keyHash[:]) + log.Debug("handling event from witness %x", ev.keyHash[:]) sm.cosignatures[*ev.keyHash] = ev.cosignature } -- cgit v1.2.3