Skip to content

Commit

Permalink
Merge pull request vmware-tanzu#487 from TaoZou1/log1
Browse files Browse the repository at this point in the history
Update log variant
  • Loading branch information
TaoZou1 authored Jan 18, 2024
2 parents 770e903 + 4237de2 commit 21e21d7
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func init() {
os.Exit(1)
}

logf.SetLogger(logger.ZapLogger(cf))
logf.SetLogger(logger.ZapLogger(cf.DefaultConfig.Debug, config.LogLevel))

if os.Getenv("NSX_OPERATOR_NAMESPACE") != "" {
nsxOperatorNamespace = os.Getenv("NSX_OPERATOR_NAMESPACE")
Expand Down
2 changes: 1 addition & 1 deletion cmd_clean/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func main() {
cf.CaFile = []string{caFile}
cf.Cluster = cluster

logf.SetLogger(logger.ZapLogger(cf))
logf.SetLogger(logger.ZapLogger(cf.DefaultConfig.Debug, config.LogLevel))

// just a demo to show how to use customer http client
// customer http client should handle verify and authentication
Expand Down
14 changes: 6 additions & 8 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"go.uber.org/zap/zapcore"
logf "sigs.k8s.io/controller-runtime/pkg/log"
zapcr "sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/vmware-tanzu/nsx-operator/pkg/config"
)

const logTmFmtWithMS = "2006-01-02 15:04:05.000"
Expand All @@ -31,19 +29,19 @@ func init() {

// If debug set in configmap, set log level to 1.
// If loglevel set in command line and greater than debug log level, set it to command line level.
func getLogLevel(cf *config.NSXOperatorConfig) int {
func getLogLevel(cfDebug bool, cfLogLevel int) int {
logLevel := 0
if cf.DefaultConfig.Debug {
if cfDebug {
logLevel = 1
}
realLogLevel := logLevel
if config.LogLevel > logLevel {
realLogLevel = config.LogLevel
if cfLogLevel > logLevel {
realLogLevel = cfLogLevel
}
return realLogLevel
}

func ZapLogger(cf *config.NSXOperatorConfig) logr.Logger {
func ZapLogger(cfDebug bool, cfLogLevel int) logr.Logger {
encoderConf := zapcore.EncoderConfig{
CallerKey: "caller_line",
LevelKey: "level_name",
Expand All @@ -69,7 +67,7 @@ func ZapLogger(cf *config.NSXOperatorConfig) logr.Logger {
// In level.go of zapcore, higher levels are more important.
// However, in logr.go, a higher verbosity level means a log message is less important.
// So we need to reverse the order of the levels.
logLevel := getLogLevel(cf)
logLevel := getLogLevel(cfDebug, cfLogLevel)
opts.Level = zapcore.Level(-1 * logLevel)
opts.ZapOpts = append(opts.ZapOpts, zap.AddCaller(), zap.AddCallerSkip(0))
if logLevel > 0 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"sync"

"github.com/prometheus/client_golang/prometheus"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/metrics"

"github.com/vmware-tanzu/nsx-operator/pkg/config"
"github.com/vmware-tanzu/nsx-operator/pkg/logger"
)

const (
Expand All @@ -24,7 +24,7 @@ const (
ScrapeTimeout = 30
)

var log = logf.Log.WithName("metrics")
var log = logger.Log

var (
NSXOperatorHealthStats = prometheus.NewGauge(
Expand Down
5 changes: 2 additions & 3 deletions pkg/nsx/auth/jwt/JWTtokenprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ package jwt
import (
"time"

logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/vmware-tanzu/nsx-operator/pkg/logger"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/auth"
)

Expand All @@ -18,7 +17,7 @@ const (
)

var (
log = logf.Log.WithName("nsx").WithName("jwt")
log = logger.Log
)

type JWTTokenProvider struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/nsx/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"time"

policyclient "github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/vmware-tanzu/nsx-operator/pkg/logger"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/auth"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/ratelimiter"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/util"
Expand Down Expand Up @@ -54,7 +54,7 @@ type NsxVersion struct {
var (
jarCache = NewJar()
nsxVersion = &NsxVersion{}
log = logf.Log.WithName("nsx").WithName("cluster")
log = logger.Log
)

// NewCluster creates a cluster based on nsx Config.
Expand Down
5 changes: 3 additions & 2 deletions pkg/nsx/ratelimiter/ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"time"

"golang.org/x/time/rate"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/vmware-tanzu/nsx-operator/pkg/logger"
)

// APIReduceRateCodes is http status code set which will trigger rate limiter adjust.
var (
APIReduceRateCodes = [2]int{429, 503}
log = logf.Log.WithName("nsx").WithName("ratelimiter")
log = logger.Log
)

const (
Expand Down
5 changes: 3 additions & 2 deletions pkg/nsx/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import (
"github.com/vmware/vsphere-automation-sdk-go/runtime/bindings"
"github.com/vmware/vsphere-automation-sdk-go/runtime/data"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/vmware-tanzu/nsx-operator/pkg/logger"
)

var log = logf.Log.WithName("nsx").WithName("utils")
var log = logger.Log

// ErrorDetail is error detail which info extracted from http.Response.Body.
type ErrorDetail struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/vmware-tanzu/nsx-operator/pkg/apis/v1alpha1"
"github.com/vmware-tanzu/nsx-operator/pkg/apis/v1alpha2"
"github.com/vmware-tanzu/nsx-operator/pkg/logger"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
)

Expand Down Expand Up @@ -54,7 +54,7 @@ func init() {
}
}

var log = logf.Log.WithName("pkg").WithName("utils")
var log = logger.Log

func NormalizeLabels(matchLabels *map[string]string) *map[string]string {
newLabels := make(map[string]string)
Expand Down

0 comments on commit 21e21d7

Please sign in to comment.