Skip to content

Commit

Permalink
feat: switch to zerolog (#8)
Browse files Browse the repository at this point in the history
* feat: switch to zerolog

Signed-off-by: Sarah Funkhouser <[email protected]>

* core version, startblock

Signed-off-by: Sarah Funkhouser <[email protected]>

---------

Signed-off-by: Sarah Funkhouser <[email protected]>
  • Loading branch information
golanglemonade authored Sep 16, 2024
1 parent 3a7a312 commit ef38d3a
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 129 deletions.
49 changes: 34 additions & 15 deletions cmd/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ package cmd

import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime/debug"
"strings"
"time"

"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
"github.com/knadh/koanf/v2"
"github.com/mitchellh/go-homedir"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

const (
Expand All @@ -24,7 +28,6 @@ const (
var (
cfgFile string
OutputFormat string
Logger *zap.SugaredLogger
Config *koanf.Koanf
)

Expand Down Expand Up @@ -78,24 +81,40 @@ func initConfig() {
setupLogging()
}

// setupLogging configures the logger based on the command flags
// setupLogging sets up the logging defaults for the application
func setupLogging() {
cfg := zap.NewProductionConfig()
if Config.Bool("pretty") {
cfg = zap.NewDevelopmentConfig()
}
// setup logging with time and app name
log.Logger = zerolog.New(os.Stderr).
With().Timestamp().
Logger().
With().Str("app", appName).
Logger()

// set the log level
zerolog.SetGlobalLevel(zerolog.InfoLevel)

// set the log level to debug if the debug flag is set and add additional information
if Config.Bool("debug") {
cfg.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
} else {
cfg.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
}
zerolog.SetGlobalLevel(zerolog.DebugLevel)

l, err := cfg.Build()
cobra.CheckErr(err)
buildInfo, _ := debug.ReadBuildInfo()

Logger = l.Sugar().With("app", appName)
defer Logger.Sync() //nolint:errcheck
log.Logger = log.Logger.With().
Caller().
Int("pid", os.Getpid()).
Str("go_version", buildInfo.GoVersion).Logger()
}

// pretty logging for development
if Config.Bool("pretty") {
log.Logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.RFC3339,
FormatCaller: func(i interface{}) string {
return filepath.Base(fmt.Sprintf("%s", i))
},
})
}
}

// initConfiguration loads the configuration from the command flags of the given cobra command
Expand Down
55 changes: 37 additions & 18 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package cmd

import (
"log"
"fmt"
"os"
"path/filepath"
"runtime/debug"
"time"

"github.com/knadh/koanf/providers/posflag"
"github.com/knadh/koanf/v2"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

const appName = "openlane-cloud"

var (
logger *zap.SugaredLogger
k *koanf.Koanf
k *koanf.Koanf
)

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -46,7 +50,7 @@ func init() {
func initConfig() {
// Load config from flags, including defaults
if err := initCmdFlags(rootCmd); err != nil {
log.Fatalf("error loading config: %v", err)
log.Fatal().Err(err).Msg("error loading config")
}

setupLogging()
Expand All @@ -56,23 +60,38 @@ func initCmdFlags(cmd *cobra.Command) error {
return k.Load(posflag.Provider(cmd.Flags(), k.Delim(), k), nil)
}

// setupLogging sets up the logging defaults for the application
func setupLogging() {
cfg := zap.NewProductionConfig()
// setup logging with time and app name
log.Logger = zerolog.New(os.Stderr).
With().Timestamp().
Logger().
With().Str("app", appName).
Logger()

// set the log level
zerolog.SetGlobalLevel(zerolog.InfoLevel)

// set the log level to debug if the debug flag is set and add additional information
if k.Bool("pretty") {
cfg = zap.NewDevelopmentConfig()
}
zerolog.SetGlobalLevel(zerolog.DebugLevel)

if k.Bool("debug") {
cfg.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
} else {
cfg.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
}
buildInfo, _ := debug.ReadBuildInfo()

l, err := cfg.Build()
if err != nil {
panic(err)
log.Logger = log.Logger.With().
Caller().
Int("pid", os.Getpid()).
Str("go_version", buildInfo.GoVersion).Logger()
}

logger = l.Sugar().With("app", appName)
defer logger.Sync() //nolint:errcheck
// pretty logging for development
if k.Bool("pretty") {
log.Logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.RFC3339,
FormatCaller: func(i interface{}) string {
return filepath.Base(fmt.Sprintf("%s", i))
},
})
}
}
9 changes: 4 additions & 5 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package cmd
import (
"context"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/theopenlane/beacon/otelx"
"go.uber.org/zap"

"github.com/theopenlane/openlane-cloud/internal/httpserve/config"
"github.com/theopenlane/openlane-cloud/internal/httpserve/server"
Expand All @@ -30,7 +30,6 @@ func serve(ctx context.Context) error {
serverOpts := []serveropts.ServerOption{}
serverOpts = append(serverOpts,
serveropts.WithConfigProvider(&config.ConfigProviderWithRefresh{}),
serveropts.WithLogger(logger),
serveropts.WithOpenlaneClient(),
serveropts.WithHTTPS(),
serveropts.WithMiddleware(),
Expand All @@ -40,13 +39,13 @@ func serve(ctx context.Context) error {
so := serveropts.NewServerOptions(serverOpts, k.String("config"))

if err := otelx.NewTracer(so.Config.Settings.Tracer, appName); err != nil {
logger.Fatalw("failed to initialize tracer", "error", err)
log.Fatal().Err(err).Msg("failed to initialize tracer")
}

srv := server.NewServer(so.Config, so.Config.Logger)
srv := server.NewServer(so.Config)

if err := srv.StartEchoServer(ctx); err != nil {
logger.Error("failed to run server", zap.Error(err))
log.Error().Err(err).Msg("failed to run server")
}

return nil
Expand Down
29 changes: 16 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,29 @@ require (
github.com/mcuadros/go-defaults v1.2.0
github.com/mitchellh/go-homedir v1.1.0
github.com/prometheus/client_golang v1.20.3
github.com/rs/zerolog v1.33.0
github.com/schollz/progressbar/v3 v3.15.0
github.com/spf13/cobra v1.8.1
github.com/stoewer/go-strcase v1.3.0
github.com/stretchr/testify v1.9.0
github.com/theopenlane/beacon v0.1.0
github.com/theopenlane/core v0.1.9
github.com/theopenlane/core v0.1.10
github.com/theopenlane/echo-prometheus v0.1.0
github.com/theopenlane/echox v0.1.0
github.com/theopenlane/echozap v0.1.0
github.com/theopenlane/echox v0.2.0
github.com/theopenlane/httpsling v0.2.0
github.com/theopenlane/iam v0.1.6
github.com/theopenlane/utils v0.1.5
go.uber.org/zap v1.27.0
github.com/theopenlane/iam v0.2.0
github.com/theopenlane/utils v0.2.0
golang.org/x/crypto v0.27.0
golang.org/x/text v0.18.0
)

require (
ariga.io/atlas v0.27.0 // indirect
entgo.io/contrib v0.6.0 // indirect
entgo.io/ent v0.14.1 // indirect
github.com/Yamashou/gqlgenc v0.24.0 // indirect
github.com/Yamashou/gqlgenc v0.25.0 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down Expand Up @@ -80,6 +81,8 @@ require (
github.com/lestrrat-go/jwx/v2 v2.1.1 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
Expand All @@ -100,7 +103,7 @@ require (
github.com/segmentio/asm v1.2.0 // indirect
github.com/sosodev/duration v1.3.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/theopenlane/entx v0.1.7 // indirect
github.com/theopenlane/entx v0.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/vektah/gqlparser/v2 v2.5.16 // indirect
Expand All @@ -109,16 +112,16 @@ require (
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/zclconf/go-cty v1.15.0 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.29.0 // indirect
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
go.opentelemetry.io/otel/sdk v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
Expand All @@ -129,7 +132,7 @@ require (
golang.org/x/tools v0.25.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/grpc v1.66.0 // indirect
google.golang.org/grpc v1.66.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit ef38d3a

Please sign in to comment.