From b993fd1dfd82c55a5c32d0ce0cb5d519c26b3558 Mon Sep 17 00:00:00 2001 From: kenriortega Date: Thu, 11 Nov 2021 10:30:00 -0500 Subject: [PATCH] ref: change log format --- pkg/badgerdb/client.go | 3 ++- pkg/config/config.go | 6 ++++-- pkg/genkey/genkey.go | 3 ++- pkg/healthcheck/health.go | 3 ++- pkg/httpsrv/server.go | 26 +++++++++++--------------- 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/pkg/badgerdb/client.go b/pkg/badgerdb/client.go index 9789acb..fc9ccfc 100644 --- a/pkg/badgerdb/client.go +++ b/pkg/badgerdb/client.go @@ -2,6 +2,7 @@ package badgerdb import ( badger "github.com/dgraph-io/badger/v3" + "github.com/kenriortega/ngonx/pkg/errors" "github.com/kenriortega/ngonx/pkg/logger" ) @@ -20,7 +21,7 @@ func GetBadgerDB(embedMem bool) *badger.DB { db, err := badger.Open(opt) if err != nil { - logger.LogError(err.Error()) + logger.LogError(errors.Errorf("badger: %v", err).Error()) panic(err) } diff --git a/pkg/config/config.go b/pkg/config/config.go index d16c065..36b8608 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -140,7 +140,8 @@ proxy: path_protected: true ` if err != nil { - logger.LogError(errors.ErrCreatingSettingFile.Error()) + logger.LogError(errors.Errorf("config: %v", errors.ErrCreatingSettingFile).Error()) + } // defer f.Close() @@ -150,6 +151,7 @@ proxy: _, err = f.Write(data) if err != nil { - logger.LogError(errors.ErrWritingSettingFile.Error()) + logger.LogError(errors.Errorf("config: %v", errors.ErrWritingSettingFile).Error()) + } } diff --git a/pkg/genkey/genkey.go b/pkg/genkey/genkey.go index d7df160..211a331 100644 --- a/pkg/genkey/genkey.go +++ b/pkg/genkey/genkey.go @@ -33,7 +33,8 @@ func ApiKeyGenerator(word string) string { h := sha256.New() _, err := h.Write([]byte(word)) if err != nil { - logger.LogError(errors.ErrApiKeyGenerator.Error()) + logger.LogError(errors.Errorf("genkeypkg: %v", errors.ErrApiKeyGenerator).Error()) + } return fmt.Sprintf("%x", h.Sum(nil)) } diff --git a/pkg/healthcheck/health.go b/pkg/healthcheck/health.go index a7032f0..4912d0e 100644 --- a/pkg/healthcheck/health.go +++ b/pkg/healthcheck/health.go @@ -14,7 +14,8 @@ func IsBackendAlive(u *url.URL) bool { timeout := 2 * time.Second conn, err := net.DialTimeout("tcp", u.Host, timeout) if err != nil { - logger.LogError(errors.ErrIsBackendAlive.Error()) + logger.LogError(errors.Errorf("healtcheck: %v", errors.ErrIsBackendAlive).Error()) + return false } _ = conn.Close() diff --git a/pkg/httpsrv/server.go b/pkg/httpsrv/server.go index 897473b..43e7cfd 100644 --- a/pkg/httpsrv/server.go +++ b/pkg/httpsrv/server.go @@ -9,6 +9,7 @@ import ( "os/signal" "time" + "github.com/kenriortega/ngonx/pkg/errors" "github.com/kenriortega/ngonx/pkg/logger" ) @@ -57,31 +58,27 @@ func NewServerSSL(host string, port int, mux http.Handler) *server { // Start runs ListenAndServe on the http.Server with graceful shutdown func (srv *server) Start() { - logger.LogInfo("starting server...") + logger.LogInfo("ngonx: starting server...") go func() { if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { - logger.LogError( - fmt.Sprintf("could not listen on %s due to %s", srv.Addr, err.Error()), - ) + logger.LogError(errors.Errorf("could not listen on %s due to %s", srv.Addr, err).Error()) } }() - logger.LogInfo(fmt.Sprintf("server is ready to handle requests %s", srv.Addr)) + logger.LogInfo(fmt.Sprintf("ngonx: server is ready to handle requests %s", srv.Addr)) srv.gracefulShutdown() } // Start runs ListenAndServe on the http.Server with graceful shutdown func (srv *server) StartSSL(crt, key string) { - logger.LogInfo("starting server...") + logger.LogInfo("ngonx: starting server...") go func() { if err := srv.ListenAndServeTLS(crt, key); err != nil && err != http.ErrServerClosed { - logger.LogError( - fmt.Sprintf("could not listen on %s due to %s", srv.Addr, err.Error()), - ) + logger.LogError(errors.Errorf("could not listen on %s due to %s", srv.Addr, err).Error()) } }() - logger.LogInfo(fmt.Sprintf("server is ready to handle requests %s", srv.Addr)) + logger.LogInfo(fmt.Sprintf("ngonx: server is ready to handle requests %s", srv.Addr)) srv.gracefulShutdown() } @@ -90,16 +87,15 @@ func (srv *server) gracefulShutdown() { signal.Notify(quit, os.Interrupt) sig := <-quit - logger.LogInfo(fmt.Sprintf("server is shutting down %s", sig.String())) + logger.LogInfo(fmt.Sprintf("ngonx: server is shutting down %s", sig.String())) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() srv.SetKeepAlivesEnabled(false) if err := srv.Shutdown(ctx); err != nil { - logger.LogError( - fmt.Sprintf("could not gracefully shutdown the server %s", err.Error()), - ) + logger.LogError(errors.Errorf("could not gracefully shutdown the server %s", err).Error()) + } - logger.LogInfo("server stopped") + logger.LogInfo("ngonx: server stopped") }