Skip to content

Commit

Permalink
#14 Inited Glide logging based on Zap
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-glushko committed Dec 17, 2023
1 parent 86c624c commit 1c16e4b
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/go-playground/validator/v10 v10.16.0
github.com/spf13/cobra v1.8.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.26.0
)

require (
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
Expand Down
14 changes: 14 additions & 0 deletions pkg/cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"github.com/spf13/cobra"
"glide/pkg"
"glide/pkg/telemetry"
"go.uber.org/zap"
)

// NewCLI Create a Glide CLI
Expand All @@ -12,6 +14,18 @@ func NewCLI() *cobra.Command {
Use: "",
Version: pkg.GetVersion(),
RunE: func(cmd *cobra.Command, args []string) error {
// TODO: gonna be read from a config file
logConfig := telemetry.NewLogConfig()
logConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
logConfig.Encoding = "console"
logger, err := telemetry.NewLogger(logConfig)

if err != nil {
return err
}

logger.Debug("logger inited")

gateway, err := pkg.NewGateway()

if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/telemetry/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package telemetry

import "go.uber.org/zap"

type TelemetryConfig struct {
LogConfig *zap.Config `json:"logs" yaml:"logs"`
}
64 changes: 64 additions & 0 deletions pkg/telemetry/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package telemetry

import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

type LogConfig struct {
// Level is the minimum enabled logging level.
Level zap.AtomicLevel `yaml:"level"`

// Encoding sets the logger's encoding. Valid values are "json", "console"
Encoding string `yaml:"encoding"`

// DisableCaller stops annotating logs with the calling function's file name and line number.
// By default, all logs are annotated.
DisableCaller bool `yaml:"disable_caller"`

// DisableStacktrace completely disables automatic stacktrace capturing. By
// default, stacktraces are captured for WarnLevel and above logs in
// development and ErrorLevel and above in production.
DisableStacktrace bool `yaml:"disable_stacktrace"`

// OutputPaths is a list of URLs or file paths to write logging output to.
OutputPaths []string `yaml:"output_paths"`

// InitialFields is a collection of fields to add to the root logger.
InitialFields map[string]interface{} `yaml:"initial_fields"`
}

func NewLogConfig() *LogConfig {
return &LogConfig{
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
Encoding: "json",
OutputPaths: []string{"stdout"},
}
}

func (c *LogConfig) ToZapConfig() *zap.Config {
zapConfig := zap.NewProductionConfig()

if c.Encoding == "console" {
zapConfig = zap.NewDevelopmentConfig()

// Human-readable timestamps for console format of logs.
zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
// Colorized plain console logs
zapConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}

zapConfig.Level = c.Level
zapConfig.DisableCaller = c.DisableCaller
zapConfig.DisableStacktrace = c.DisableStacktrace
zapConfig.OutputPaths = c.OutputPaths
zapConfig.InitialFields = c.InitialFields

return &zapConfig
}

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

return zapConfig.Build()
}

0 comments on commit 1c16e4b

Please sign in to comment.