github.com/verrazzano/verrazzano@v1.7.1/pkg/log/vzlog/doc.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package vzlog
     5  
     6  // The vzlog package provides logging for messages in controllers and other
     7  // code that wants to throttle messages.  The Verrazzano Platform Operator controller for
     8  // the Verrazzano resource is an example where this should be used.  During a reconcile cycle,
     9  // the controller-runtime calls the controller Reconcile method repeatedly until the resource has
    10  // been reconciled.  For something like Verrazzano install, this can translate into scores of
    11  // reconcile calls.  The controller Reconcile code has no way of knowing if it already displayed
    12  // an info message, such as "Keyclaok waiting for Verrazzano secret".  Using normal logging, that message
    13  // might be displayed dozens of times.  With the progress logger, the message is only displayed
    14  // periodically, once a minute by default.  Furthermore, once a message is display, and a new message
    15  // is logged, that old message will never be displayed again.  This allows controllers to log
    16  // informative progress messages, without overwhelming the log files with superfluous information.
    17  //
    18  // The 'Once' and 'Progress' logging is done in the context of a reconcile session for a resource change.
    19  // This means if you change resource foo, and the controller reconciler is called 100 times, A call to log.Once will
    20  // log the message once.  When the resource foo is finally reconciled (return ctrl.Result{}, nil to controller runtime),
    21  // then we delete the logging context. So, if you changed the resource foo an hour later,
    22  // and the same code path is executed, then the message will be displayed once, for the new reconcile session.
    23  //
    24  // The main purpose of this package is to provide logging during Kubernetes resource reconciliation.  For that
    25  // use case, use the EnsureResourceLogger method as follows.  See the function descrition for more details.
    26  //
    27  //   	log, err := vzlog.EnsureResourceLogger(&vzlog.ResourceConfig{
    28  //		Name:           vz.Name,
    29  //		Namespace:      vz.Namespace,
    30  //		ID:             string(vz.UID),
    31  //		Generation:     vz.Generation,
    32  //		ControllerName: "verrazzano",
    33  //	})
    34  //
    35  // For other use cases, you can call the lower level functions to explicitly create a LogContext and VerrazzanoLogger
    36  // as described here. The logger is initialized with the zap.SugaredLogger and then used instead of the zap logger directly.
    37  // The same SugaredLogger calls can be made: Debug, Debugf, Info, Infof, Error, and Errorf.  The
    38  // two new calls are Progress and Progressf. The S() method will return the underlying SugaredLogger.
    39  // The following pseudo-code shows how this should be used:
    40  //
    41  //   log := vzlog.EnsureContext(key).EnsureLogger("default", zaplog, zaplog)
    42  //
    43  // Display info and errors as usual
    44  //   p.Errorf(...)
    45  //   p.Info(...)
    46  //
    47  // Display progress
    48  //   p.Progress("Reconciling namespace/resource")
    49  //
    50  // Display Keycloak progress
    51  //   cl := l.EnsureContext().EnsureLogger("Keycloak")
    52  //   cl.Progress("Waiting for Verrazzano secret")
    53  //   cl.Errorf(...)
    54  //
    55  // Display Istio progress
    56  //   cl := l.EnsureContext().EnsureLogger("Istio")
    57  //   cl.Progress("Waiting for Istio to start")
    58  //   cl.Errorf(...)