Skip to content

Commit

Permalink
adds initLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
vbnrh committed Dec 10, 2019
1 parent 8b00f7c commit 19c1345
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import (
"context"
"flag"
"fmt"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"io"
"os"
"runtime"
"time"

monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
nbapis "github.com/noobaa/noobaa-operator/v2/pkg/apis"
Expand All @@ -17,17 +22,17 @@ import (
"github.com/operator-framework/operator-sdk/pkg/leader"
"github.com/operator-framework/operator-sdk/pkg/ready"
sdkVersion "github.com/operator-framework/operator-sdk/version"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"sigs.k8s.io/controller-runtime/pkg/client/config"
kzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"

cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
)

var log = logf.Log.WithName("cmd")
Expand All @@ -45,7 +50,7 @@ func main() {
// implementing the logr.Logger interface. This logger will
// be propagated through the whole operator, generating
// uniform and structured logs.
logf.SetLogger(logf.ZapLogger(false))
initLogger()

printVersion()

Expand Down Expand Up @@ -148,3 +153,36 @@ func main() {
os.Exit(1)
}
}

func initLogger() {
logger := zapr.NewLogger(getZapLogger(os.Stderr, false))
logf.SetLogger(logger)
}

func getZapLogger(destWriter io.Writer, development bool, opts ...zap.Option) *zap.Logger {
sink := zapcore.AddSync(destWriter)

var enc zapcore.Encoder
var lvl zap.AtomicLevel
if development {
encCfg := zap.NewDevelopmentEncoderConfig()
encCfg.EncodeTime = zapcore.ISO8601TimeEncoder
enc = zapcore.NewConsoleEncoder(encCfg)

lvl = zap.NewAtomicLevelAt(zap.DebugLevel)
opts = append(opts, zap.Development(), zap.AddStacktrace(zap.ErrorLevel))
} else {
encCfg := zap.NewProductionEncoderConfig()
encCfg.EncodeTime = zapcore.ISO8601TimeEncoder
enc = zapcore.NewJSONEncoder(encCfg)
lvl = zap.NewAtomicLevelAt(zap.InfoLevel)
opts = append(opts, zap.AddStacktrace(zap.WarnLevel),
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewSampler(core, time.Second, 100, 100)
}))
}
opts = append(opts, zap.AddCallerSkip(1), zap.ErrorOutput(sink))
log := zap.New(zapcore.NewCore(&kzap.KubeAwareEncoder{Encoder: enc, Verbose: development}, sink, lvl))
log = log.WithOptions(opts...)
return log
}

0 comments on commit 19c1345

Please sign in to comment.