istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/security/authz/builder/logger.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package builder
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/hashicorp/go-multierror"
    22  
    23  	"istio.io/istio/pkg/log"
    24  	"istio.io/istio/pkg/util/istiomultierror"
    25  )
    26  
    27  var authzLog = log.RegisterScope("authorization", "Istio Authorization Policy")
    28  
    29  type AuthzLogger struct {
    30  	debugMsg []string
    31  	errMsg   *multierror.Error
    32  }
    33  
    34  func (al *AuthzLogger) AppendDebugf(format string, args ...any) {
    35  	al.debugMsg = append(al.debugMsg, fmt.Sprintf(format, args...))
    36  }
    37  
    38  func (al *AuthzLogger) AppendError(err error) {
    39  	al.errMsg = multierror.Append(al.errMsg, err)
    40  }
    41  
    42  func (al *AuthzLogger) Report() {
    43  	if al.errMsg != nil {
    44  		al.errMsg.ErrorFormat = istiomultierror.MultiErrorFormat()
    45  		authzLog.Errorf("Processed authorization policy: %s", al.errMsg)
    46  	}
    47  	if authzLog.DebugEnabled() && len(al.debugMsg) != 0 {
    48  		out := strings.Join(al.debugMsg, "\n\t* ")
    49  		authzLog.Debugf("Processed authorization policy with details:\n\t* %v", out)
    50  	} else {
    51  		authzLog.Debugf("Processed authorization policy")
    52  	}
    53  }