...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/log/global_logger.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/log

     1  // Copyright 2022 Chaos Mesh 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  
    16  package log
    17  
    18  import (
    19  	"sync"
    20  
    21  	"github.com/go-logr/logr"
    22  )
    23  
    24  // the way for management and access global logger is referenced from zap.
    25  var (
    26  	globalMu     sync.RWMutex
    27  	globalLogger = logr.Discard()
    28  )
    29  
    30  // L is the way to access the global logger. You could use it if there are no logger in your code context. Please notice
    31  // that the default value of "global logger" is a "discard logger" which means that all logs will be ignored. Make sure
    32  // that initialize the global logger by ReplaceGlobals before using it, for example, calling ReplaceGlobals at the beginning
    33  // of your main function.
    34  //
    35  // Do NOT save the global logger to a variable for long-term using, because it is possible that the global logger is
    36  // replaced by another. Keep calling L at each time.
    37  //
    38  // Deprecated: Do not use global logger anymore. For more detail, see
    39  // https://github.com/chaos-mesh/rfcs/blob/main/text/2021-12-09-logging.md#global-logger
    40  func L() logr.Logger {
    41  	globalMu.RLock()
    42  	result := globalLogger
    43  	globalMu.RUnlock()
    44  	return result
    45  }
    46  
    47  // ReplaceGlobals would replace the global logger with the given logger. It should be used when your application starting.
    48  //
    49  // Deprecated: Do not use global logger anymore. For more detail, see
    50  // https://github.com/chaos-mesh/rfcs/blob/main/text/2021-12-09-logging.md#global-logger
    51  func ReplaceGlobals(logger logr.Logger) {
    52  	globalMu.Lock()
    53  	globalLogger = logger
    54  	globalMu.Unlock()
    55  }
    56