sigs.k8s.io/cluster-api-provider-azure@v1.14.3/util/tele/composite_logger.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tele
    18  
    19  import (
    20  	"github.com/go-logr/logr"
    21  )
    22  
    23  type compositeLogSink struct {
    24  	logSinks []logr.LogSink
    25  }
    26  
    27  func (c *compositeLogSink) Init(info logr.RuntimeInfo) {
    28  	// we change the depth of the stack so that we can get the real
    29  	// line where the log statement was called. We need to do this because the composite logger adds to the
    30  	// call stack due to wrapping the internal logger.
    31  	info.CallDepth += 2
    32  	for _, l := range c.logSinks {
    33  		l.Init(info)
    34  	}
    35  }
    36  
    37  func (c *compositeLogSink) Enabled(v int) bool {
    38  	for _, l := range c.logSinks {
    39  		if !l.Enabled(v) {
    40  			return false
    41  		}
    42  	}
    43  	return true
    44  }
    45  
    46  func (c *compositeLogSink) iter(fn func(l logr.LogSink)) {
    47  	for _, l := range c.logSinks {
    48  		fn(l)
    49  	}
    50  }
    51  
    52  func (c *compositeLogSink) Info(level int, msg string, keysAndValues ...interface{}) {
    53  	c.iter(func(l logr.LogSink) {
    54  		l.Info(level, msg, keysAndValues...)
    55  	})
    56  }
    57  
    58  func (c *compositeLogSink) Error(err error, msg string, keysAndValues ...interface{}) {
    59  	c.iter(func(l logr.LogSink) {
    60  		l.Error(err, msg, keysAndValues...)
    61  	})
    62  }
    63  
    64  func (c *compositeLogSink) WithValues(keysAndValues ...interface{}) logr.LogSink {
    65  	var logSinks = make([]logr.LogSink, len(c.logSinks))
    66  	for i, l := range c.logSinks {
    67  		logSinks[i] = l.WithValues(keysAndValues...)
    68  	}
    69  
    70  	return &compositeLogSink{
    71  		logSinks: logSinks,
    72  	}
    73  }
    74  
    75  func (c *compositeLogSink) WithName(name string) logr.LogSink {
    76  	var logSinks = make([]logr.LogSink, len(c.logSinks))
    77  	for i, l := range c.logSinks {
    78  		logSinks[i] = l.WithName(name)
    79  	}
    80  
    81  	return &compositeLogSink{
    82  		logSinks: logSinks,
    83  	}
    84  }
    85  
    86  // NewCompositeLogger is the main entry-point to this implementation.
    87  func NewCompositeLogger(logSinks []logr.LogSink) logr.Logger {
    88  	sink := &compositeLogSink{
    89  		logSinks: logSinks,
    90  	}
    91  	return logr.New(sink)
    92  }