Skip to content

Commit

Permalink
Convert to using slog
Browse files Browse the repository at this point in the history
  • Loading branch information
snowzach committed Oct 23, 2023
1 parent 471a37c commit fd77859
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 39 deletions.
12 changes: 4 additions & 8 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package cmd

import (
"fmt"
"log/slog"
"net/http"
"strings"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
cli "github.com/spf13/cobra"
"go.uber.org/zap/zapcore"

"github.com/snowzach/golib/conf"
"github.com/snowzach/golib/httpserver"
Expand Down Expand Up @@ -99,8 +99,6 @@ var (
signal.Stop.OnSignal(signal.DefaultStopSignals...)
<-signal.Stop.Chan() // Wait until Stop
signal.Stop.Wait() // Wait until everyone cleans up
log.Flush() // Flush the logger

},
}
)
Expand All @@ -120,10 +118,8 @@ func newRouter() (chi.Router, error) {
return nil, fmt.Errorf("could not parser server.log config: %w", err)
}
switch conf.C.String("logger.encoding") {
case "stackdriver":
router.Use(logger.LoggerStackdriverMiddleware(log.Base.Named("server"), loggerConfig))
default:
router.Use(logger.LoggerStandardMiddleware(log.Base.Named("server"), loggerConfig))
router.Use(logger.LoggerStandardMiddleware(log.Logger.With("context", "server"), loggerConfig))
}
}

Expand Down Expand Up @@ -186,9 +182,9 @@ func newDatabase() (*postgres.Client, error) {
}

// Loggers
postgresConfig.Logger = log.NewWrapper(log.Logger.Named("database.postgres").Desugar(), zapcore.InfoLevel)
postgresConfig.Logger = log.NewWrapper(log.Logger.With("context", "database.postgres"), slog.LevelInfo)
if conf.C.Bool("database.log_queries") {
postgresConfig.QueryLogger = log.NewWrapper(log.Logger.Named("database.postgres.query").Desugar(), zapcore.DebugLevel)
postgresConfig.QueryLogger = log.NewWrapper(log.Logger.With("context", "database.postgres.query"), slog.LevelDebug)
}

// Migrations
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ var (
r.HandleFunc("/debug/pprof/profile", pprof.Profile)
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
r.HandleFunc("/debug/pprof/trace", pprof.Trace)
log.Infow("Profiler enabled", "profiler_path", fmt.Sprintf("http://%s/debug/pprof/", hostPort))
log.Info("Profiler enabled", "profiler_path", fmt.Sprintf("http://%s/debug/pprof/", hostPort))
}
go func() {
if err := http.ListenAndServe(hostPort, r); err != nil {
log.Errorf("Metrics server error: %v", err)
}
}()
log.Infow("Metrics enabled", "address", hostPort)
log.Info("Metrics enabled", "address", hostPort)
}

// Create Pid File
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/snowzach/gorestapi

go 1.20
go 1.21

toolchain go1.21.3

require (
github.com/gavv/httpexpect/v2 v2.1.0
Expand All @@ -10,23 +12,21 @@ require (
github.com/jmoiron/sqlx v1.3.5
github.com/prometheus/client_golang v1.17.0
github.com/rs/xid v1.5.0
github.com/snowzach/golib v1.0.2
github.com/snowzach/golib v1.0.3
github.com/snowzach/queryp v0.3.6
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.2
go.uber.org/zap v1.26.0
)

require (
github.com/ajg/form v1.5.1 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blendle/zapdriver v1.3.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/creasty/defaults v1.7.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand Down Expand Up @@ -70,7 +70,6 @@ require (
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
Expand All @@ -81,3 +80,4 @@ require (
)

// replace github.com/snowzach/queryp => ../queryp
// replace github.com/snowzach/golib => ../golib
Loading

0 comments on commit fd77859

Please sign in to comment.