Skip to content

Commit

Permalink
Adding logs with uber/zap
Browse files Browse the repository at this point in the history
  • Loading branch information
Shoaeb Jindani committed Apr 2, 2024
1 parent 6713446 commit f582637
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
3 changes: 1 addition & 2 deletions controllers/ibpca/ibpca_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ type ReconcileIBPCA struct {
func (r *ReconcileIBPCA) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
var err error

reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)

reqLogger := r.Config.Logger
// If ca-restart-config configmap is the object being reconciled, reconcile the
// restart configmap.
if request.Name == "ca-restart-config" {
Expand Down
3 changes: 2 additions & 1 deletion controllers/ibpconsole/ibpconsole_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ type ReconcileIBPConsole struct {
func (r *ReconcileIBPConsole) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
var err error

reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
//reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger := r.Config.Logger
reqLogger.Info(fmt.Sprintf("Reconciling IBPConsole with update values of [ %+v ]", r.update.GetUpdateStackWithTrues()))

Check failure on line 158 in controllers/ibpconsole/ibpconsole_controller.go

View workflow job for this annotation

GitHub Actions / make-checks

reqLogger.Info undefined (type *logr.Logger is pointer to interface, not interface)

// Fetch the IBPConsole instance
Expand Down
4 changes: 2 additions & 2 deletions controllers/ibporderer/ibporderer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ type ReconcileIBPOrderer struct {
func (r *ReconcileIBPOrderer) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
var err error

reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)

//reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger := r.Config.Logger
// If orderer-restart-config configmap is the object being reconciled, reconcile the
// restart configmap.
if request.Name == "orderer-restart-config" {
Expand Down
4 changes: 2 additions & 2 deletions controllers/ibppeer/ibppeer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ type ReconcileIBPPeer struct {
func (r *ReconcileIBPPeer) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
var err error

reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)

//reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger := r.Config.Logger
// If peer-restart-config configmap is the object being reconciled, reconcile the
// restart configmap.
if request.Name == "peer-restart-config" {
Expand Down
45 changes: 36 additions & 9 deletions pkg/command/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

k8sruntime "k8s.io/apimachinery/pkg/runtime"

"github.com/go-logr/zapr"
routev1 "github.com/openshift/api/route/v1"
"github.com/operator-framework/operator-lib/leader"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand All @@ -45,16 +46,18 @@ import (
"github.com/IBM-Blockchain/fabric-operator/pkg/offering"
openshiftv1 "github.com/openshift/api/config/v1"

commonutility "github.com/IBM-Blockchain/fabric-operator/pkg/util"
uberzap "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/apimachinery/pkg/types"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"sigs.k8s.io/controller-runtime/pkg/client"

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

var log = logf.Log.WithName("cmd_operator")

var log *uberzap.SugaredLogger
var c1 commonutility.Client
var (
scheme = k8sruntime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
Expand All @@ -67,7 +70,7 @@ func init() {
}

func printVersion() {
log.Info(fmt.Sprintf("Go Version: %s", runtime.Version()))
log.Info("Go Version: %s", runtime.Version())
log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH))
}

Expand Down Expand Up @@ -102,12 +105,23 @@ func OperatorWithSignal(operatorCfg *oconfig.Config, signalHandler context.Conte
// be propagated through the whole operator, generating
// uniform and structured logs.
if operatorCfg.Logger != nil {
logf.SetLogger(*operatorCfg.Logger)
ctrl.SetLogger(*operatorCfg.Logger)
//logf.SetLogger(*operatorCfg.Logger)
ctrl.SetLogger(zap.New(zap.UseDevMode(false)))
log = operatorCfg.Logger.Sugar().Named("operator")
_ = err
log.Info("Installing operator in all namespace mode")
} else {
// Use the unstructured log formatter when running locally.
logf.SetLogger(zap.New(zap.UseDevMode(local)))
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
//logf.SetLogger(zap.New(zap.UseDevMode(false)))
ctrl.SetLogger(zap.New(zap.UseDevMode(false)))

// Create logger

//logm, err := util.SetupLogging("DEBUG")
log = operatorCfg.Logger.Sugar().Named("operator")
_ = err
log.Info("Installing operator in all namespace mode")

}

printVersion()
Expand Down Expand Up @@ -155,6 +169,18 @@ func OperatorWithSignal(operatorCfg *oconfig.Config, signalHandler context.Conte
}
flag.Parse()

cfg := uberzap.NewProductionConfig()
cfg.OutputPaths = []string{"stdout"}

cfg.Level.SetLevel(zapcore.DebugLevel)

zapLogger, err := cfg.Build()
if err != nil {
panic("failed to create logger: " + err.Error())
}

logger := zapr.NewLogger(zapLogger)

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Expand All @@ -163,6 +189,7 @@ func OperatorWithSignal(operatorCfg *oconfig.Config, signalHandler context.Conte
LeaderElectionID: "c30dd930.ibp.com",
LeaderElectionNamespace: operatorNamespace,
Namespace: watchNamespace,
Logger: logger,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down

0 comments on commit f582637

Please sign in to comment.