...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/logger/logger.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/logger

     1  // Copyright 2022 Google LLC
     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 logger
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  
    21  	"k8s.io/klog/v2"
    22  )
    23  
    24  var dclDebug = os.Getenv("DCL_DEBUG")
    25  
    26  // A wrapper of klog that implements DCL logger interface and only logs fatal and warnings, unless DCL_DEBUG env variable is set.
    27  type dclLogger struct {
    28  	debug bool
    29  }
    30  
    31  func SimpleDCLLogger() dclLogger {
    32  	return dclLogger{
    33  		debug: dclDebug != "",
    34  	}
    35  }
    36  
    37  func (l dclLogger) Fatal(args ...interface{}) {
    38  	klog.Fatal(args...)
    39  }
    40  
    41  func (l dclLogger) Fatalf(format string, args ...interface{}) {
    42  	klog.Fatalf(fmt.Sprintf("[DCL FATAL] %s", format), args...)
    43  }
    44  
    45  func (l dclLogger) Info(args ...interface{}) {
    46  	if l.debug {
    47  		klog.Info(args...)
    48  	}
    49  }
    50  
    51  func (l dclLogger) Infof(format string, args ...interface{}) {
    52  	if l.debug {
    53  		klog.Infof(fmt.Sprintf("[DCL INFO] %s", format), args...)
    54  	}
    55  }
    56  
    57  func (l dclLogger) Warningf(format string, args ...interface{}) {
    58  	klog.Warningf(fmt.Sprintf("[DCL WARNING] %s", format), args...)
    59  }
    60  
    61  func (l dclLogger) Warning(args ...interface{}) {
    62  	klog.Warning(args...)
    63  }
    64  

View as plain text