github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/resource-recommend/log/logger.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 log
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/google/uuid"
    23  	"k8s.io/klog/v2"
    24  )
    25  
    26  // This log is only used in resource-recommend.
    27  // TODO: How to do the common log needs to be discussed again
    28  
    29  var fieldsKey = &[1]int{1}
    30  
    31  const LinkIDKey = "linkId"
    32  
    33  // InitContext set context info such as accountId for logger
    34  func InitContext(ctx context.Context) context.Context {
    35  	fields := []interface{}{
    36  		LinkIDKey, uuid.New(),
    37  	}
    38  	return context.WithValue(ctx, fieldsKey, fields)
    39  }
    40  
    41  // SetKeysAndValues set KeysAndValues into ctx
    42  func SetKeysAndValues(ctx context.Context, keysAndValues ...interface{}) context.Context {
    43  	ctxFields := ctx.Value(fieldsKey)
    44  	if ctxFields == nil {
    45  		return context.WithValue(ctx, fieldsKey, keysAndValues)
    46  	}
    47  	if fields, ok := ctxFields.([]interface{}); !ok {
    48  		return context.WithValue(ctx, fieldsKey, keysAndValues)
    49  	} else {
    50  		fields = append(fields, keysAndValues...)
    51  		return context.WithValue(ctx, fieldsKey, fields)
    52  	}
    53  }
    54  
    55  func GetCtxFields(ctx context.Context) []interface{} {
    56  	ctxFields := ctx.Value(fieldsKey)
    57  	if ctxFields == nil {
    58  		return []interface{}{}
    59  	}
    60  	if fields, ok := ctxFields.([]interface{}); ok {
    61  		return fields
    62  	}
    63  	return []interface{}{}
    64  }
    65  
    66  // ErrorS print error log
    67  func ErrorS(ctx context.Context, err error, msg string, keysAndValues ...interface{}) {
    68  	fields := GetCtxFields(ctx)
    69  	fields = append(fields, keysAndValues...)
    70  	if len(fields) == 0 {
    71  		klog.ErrorSDepth(1, err, msg)
    72  	} else {
    73  		klog.ErrorSDepth(1, err, msg, fields...)
    74  	}
    75  }
    76  
    77  // InfoS print info log
    78  func InfoS(ctx context.Context, msg string, keysAndValues ...interface{}) {
    79  	fields := GetCtxFields(ctx)
    80  	fields = append(fields, keysAndValues...)
    81  	if len(fields) == 0 {
    82  		klog.InfoSDepth(1, msg)
    83  	} else {
    84  		klog.InfoSDepth(1, msg, fields...)
    85  	}
    86  }
    87  
    88  type Verbose struct {
    89  	klog.Verbose
    90  }
    91  
    92  func V(level klog.Level) Verbose {
    93  	return Verbose{Verbose: klog.V(level)}
    94  }
    95  
    96  // InfoS print info log
    97  func (v Verbose) InfoS(ctx context.Context, msg string, keysAndValues ...interface{}) {
    98  	fields := GetCtxFields(ctx)
    99  	fields = append(fields, keysAndValues...)
   100  	if len(fields) == 0 {
   101  		v.Verbose.InfoSDepth(1, msg)
   102  	} else {
   103  		v.Verbose.InfoSDepth(1, msg, fields...)
   104  	}
   105  }