...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/common/logger.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/common

     1  // Copyright 2020 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package common
    15  
    16  import (
    17  	"flag"
    18  
    19  	"github.com/go-logr/logr"
    20  	"github.com/spf13/pflag"
    21  	"k8s.io/klog"
    22  	"k8s.io/klog/klogr"
    23  )
    24  
    25  type LoggerFlushFunc func()
    26  
    27  func SetupKlog() error {
    28  	// setup klog
    29  	klog.InitFlags(flag.CommandLine)
    30  	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
    31  	return flag.Set("logtostderr", "true")
    32  }
    33  
    34  func NewStderrLogger() (logr.Logger, LoggerFlushFunc, error) {
    35  	logger := klogr.New()
    36  	return logger, klog.Flush, nil
    37  }
    38  
    39  var globalLogger logr.Logger
    40  
    41  func SetupGlobalLogger(logger logr.Logger) {
    42  	globalLogger = logger
    43  }
    44  
    45  func L() logr.Logger {
    46  	if globalLogger == nil {
    47  		panic("global logger not initialized")
    48  	}
    49  	return globalLogger
    50  }
    51