Skip to content

Commit

Permalink
ref: change log format
Browse files Browse the repository at this point in the history
  • Loading branch information
kenriortega committed Nov 11, 2021
1 parent 95f454b commit b993fd1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
3 changes: 2 additions & 1 deletion pkg/badgerdb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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())

}
}
3 changes: 2 additions & 1 deletion pkg/genkey/genkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/healthcheck/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
26 changes: 11 additions & 15 deletions pkg/httpsrv/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/signal"
"time"

"github.com/kenriortega/ngonx/pkg/errors"
"github.com/kenriortega/ngonx/pkg/logger"
)

Expand Down Expand Up @@ -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()
}

Expand All @@ -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")
}

0 comments on commit b993fd1

Please sign in to comment.