Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add zap logger #282

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
go.opentelemetry.io/otel/sdk/metric v1.26.0
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.26.0
go.uber.org/zap v1.27.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ import (
"log"

"github.com/EinStack/glide/pkg/cmd"
"github.com/EinStack/glide/pkg/telemetry"
"go.uber.org/zap"
)

func init() {
config := telemetry.DefaultLogConfig()
config.Level = zap.DebugLevel
config.Encoding = "console"
err := telemetry.InitializeGlobalLogger(config)
if err != nil {
log.Fatalf("Failed to initialize logger: %v", err)
}
}

// @title Glide
// @version 0.0.1
// @description API documentation for Glide, an open-source lightweight high-performance model gateway
Expand All @@ -24,9 +36,10 @@ import (
// @BasePath /
// @schemes http
func main() {
logger := telemetry.GetLogger()
cli := cmd.NewCLI()

if err := cli.Execute(); err != nil {
log.Fatalf("💥Glide has finished with error: %v", err)
logger.Fatal("💥Glide has finished with error", zap.Error(err))
}
}
9 changes: 5 additions & 4 deletions pkg/cmd/cli.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cmd

import (
"log"

"github.com/EinStack/glide/pkg/telemetry"
"github.com/EinStack/glide/pkg/version"
"go.uber.org/zap"

"github.com/EinStack/glide/pkg/config"

Expand All @@ -15,6 +15,7 @@ import (
var (
dotEnvFile string
cfgFile string
logger = telemetry.GetLogger()
)

const Description = `
Expand Down Expand Up @@ -49,9 +50,9 @@ func NewCLI() *cobra.Command {
err := configProvider.LoadDotEnv(dotEnvFile)

if err != nil {
log.Println("⚠️failed to load dotenv file: ", err) // don't have an inited logger at this moment
logger.Warn("⚠️failed to load dotenv file: ", zap.Error(err)) // don't have an inited logger at this moment
} else {
log.Printf("🔧dot env file is loaded (%v)", dotEnvFile)
logger.Info("🔧dot env file is loaded", zap.Any("dotenv", dotEnvFile))
}

_, err = configProvider.Load(cfgFile)
Expand Down
37 changes: 37 additions & 0 deletions pkg/telemetry/logging.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package telemetry

import (
"sync"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand Down Expand Up @@ -70,3 +72,38 @@ func NewLogger(cfg *LogConfig) (*zap.Logger, error) {

return logger, nil
}

var (
globalLogger *zap.Logger
loggerMutex sync.Mutex
)

// To initialize & check looger only once
func InitializeGlobalLogger(cfg *LogConfig) error {
loggerMutex.Lock()
defer loggerMutex.Unlock()

if globalLogger != nil {
return nil
}

logger, err := NewLogger(cfg)
if err != nil {
return err
}

globalLogger = logger
return nil
}

func GetLogger() *zap.Logger {
loggerMutex.Lock()
defer loggerMutex.Unlock()

if globalLogger == nil {
cfg := DefaultLogConfig()
globalLogger, _ = NewLogger(cfg)
}

return globalLogger
}