Skip to content

Commit

Permalink
#14: Configured hertz logger to obey the general logging config
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-glushko committed Dec 23, 2023
1 parent baabc11 commit c524b4e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 14 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ help:
install-checkers: ## Install static checkers
@echo "🚚 Downloading binaries.."
@GOBIN=$(CHECKER_BIN) go install mvdan.cc/gofumpt@latest
@GOBIN=$(CHECKER_BIN) go install go.uber.org/nilaway/cmd/nilaway@latest
@GOBIN=$(CHECKER_BIN) go install golang.org/x/vuln/cmd/govulncheck@latest
@GOBIN=$(CHECKER_BIN) go install github.com/securego/gosec/v2/cmd/gosec@latest

Expand All @@ -33,7 +32,6 @@ static-checks: install-checkers ## Static Analysis
@echo "🧹 GoCI Lint.."
@golangci-lint run ./...
@echo "🧹 Nilaway.."
@$(CHECKER_BIN)/nilaway ./...

vuln: install-checkers ## Check for vulnerabilities
@echo "🔍 Checking for vulnerabilities"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21.5
require (
github.com/cloudwego/hertz v0.7.3
github.com/go-playground/validator/v10 v10.16.0
github.com/hertz-contrib/logger/zap v1.1.0
github.com/spf13/cobra v1.8.0
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ github.com/henrylee2cn/ameda v1.4.10 h1:JdvI2Ekq7tapdPsuhrc4CaFiqw6QXFvZIULWJgQy
github.com/henrylee2cn/ameda v1.4.10/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4=
github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8 h1:yE9ULgp02BhYIrO6sdV/FPe0xQM6fNHkVQW2IAymfM0=
github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8/go.mod h1:Nhe/DM3671a5udlv2AdV2ni/MZzgfv2qrPL5nIi3EGQ=
github.com/hertz-contrib/logger/zap v1.1.0 h1:4efINiIDJrXEtAFeEdDJvc3Hye0VFxp+0X4BwaZgxNs=
github.com/hertz-contrib/logger/zap v1.1.0/go.mod h1:D/rJJgsYn+SGaHVfVqWS3vHTbbc7ODAlJO+6smWgTeE=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
Expand Down
6 changes: 4 additions & 2 deletions pkg/api/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ type Server struct {
}

func NewServer(config *ServerConfig, tel *telemetry.Telemetry, router *pools.Router) (*Server, error) {
srv := config.ToServer()

return &Server{
telemetry: tel,
router: router,
server: config.ToServer(),
server: srv,
}, nil
}

Expand All @@ -39,7 +41,7 @@ func (srv *Server) Run() error {
func (srv *Server) Shutdown(_ context.Context) error {
exitWaitTime := srv.server.GetOptions().ExitWaitTimeout

srv.telemetry.Logger().Info(
srv.telemetry.Logger.Info(
fmt.Sprintf("Begin graceful shutdown, wait at most %d seconds...", exitWaitTime/time.Second),
)

Expand Down
6 changes: 3 additions & 3 deletions pkg/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ LOOP:
select {
// TODO: Watch for config updates
case sig := <-gw.signalC:
gw.telemetry.Logger().Info("Received signal from OS", zap.String("signal", sig.String()))
gw.telemetry.Logger.Info("Received signal from OS", zap.String("signal", sig.String()))
break LOOP
case <-gw.shutdownC:
gw.telemetry.Logger().Info("received shutdown request")
gw.telemetry.Logger.Info("received shutdown request")
break LOOP
case <-ctx.Done():
gw.telemetry.Logger().Info("context done, terminating process")
gw.telemetry.Logger.Info("context done, terminating process")
// Call shutdown with background context as the passed in context has been canceled
return gw.shutdown(context.Background())
}
Expand Down
42 changes: 41 additions & 1 deletion pkg/telemetry/logging.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package telemetry

import (
"github.com/cloudwego/hertz/pkg/common/hlog"
hertzzap "github.com/hertz-contrib/logger/zap"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand Down Expand Up @@ -57,8 +59,46 @@ func (c *LogConfig) ToZapConfig() *zap.Config {
return &zapConfig
}

func NewHertzLogger(zapConfig *zap.Config) (*hertzzap.Logger, error) {
// Both hertzzap and zap have a set of private methods that prevents from leveraging
// their native encoder & sink building functionality
// We had to copy & paste some of those to get it working

var encoder zapcore.Encoder

if zapConfig.Encoding == "console" {
encoder = zapcore.NewConsoleEncoder(zapConfig.EncoderConfig)
} else {
encoder = zapcore.NewJSONEncoder(zapConfig.EncoderConfig)
}

sink, _, err := zap.Open(zapConfig.OutputPaths...)
if err != nil {
return nil, err
}

return hertzzap.NewLogger(
hertzzap.WithCoreEnc(encoder),
hertzzap.WithCoreWs(sink),
hertzzap.WithCoreLevel(zapConfig.Level),
hertzzap.WithZapOptions(zap.AddCallerSkip(3)),
), nil
}

func NewLogger(cfg *LogConfig) (*zap.Logger, error) {
zapConfig := cfg.ToZapConfig()

return zapConfig.Build()
logger, err := zapConfig.Build()
if err != nil {
return nil, err
}

hertzLogger, err := NewHertzLogger(zapConfig)
if err != nil {
return nil, err
}

hlog.SetLogger(hertzLogger)

return logger, nil
}
10 changes: 4 additions & 6 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ type Config struct {
}

type Telemetry struct {
logger *zap.Logger
Config *Config
Logger *zap.Logger
// TODO: add OTEL meter, tracer
}

Expand All @@ -19,10 +20,7 @@ func NewTelemetry(cfg *Config) (*Telemetry, error) {
}

return &Telemetry{
logger: logger,
Config: cfg,
Logger: logger,
}, nil
}

func (t *Telemetry) Logger() *zap.Logger {
return t.logger
}

0 comments on commit c524b4e

Please sign in to comment.