github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/go_kit_log_wrapper.go (about)

     1  // Copyright 2021 iLogtail 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  package helper
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/alibaba/ilogtail/pkg/logger"
    21  	"github.com/alibaba/ilogtail/pkg/pipeline"
    22  
    23  	"github.com/go-kit/kit/log"
    24  	"github.com/go-kit/kit/log/level"
    25  )
    26  
    27  type goKitLogWrapper struct {
    28  	context   pipeline.Context
    29  	alarmType string
    30  }
    31  
    32  // NewGoKitLogWrapper returns a logger that log with context.
    33  func NewGoKitLogWrapper(context pipeline.Context, alarmType string) log.Logger {
    34  	logger := &goKitLogWrapper{
    35  		context:   context,
    36  		alarmType: alarmType,
    37  	}
    38  	return log.With(level.NewFilter(logger, level.AllowAll()), "caller", log.DefaultCaller())
    39  }
    40  
    41  func (g *goKitLogWrapper) Log(params ...interface{}) error {
    42  	for i := 0; i < len(params); i += 2 {
    43  		if params[i].(string) == "level" {
    44  			levelStr := fmt.Sprintf("%v", params[i+1])
    45  			if levelStr == "warn" || levelStr == "error" {
    46  				break
    47  			}
    48  			if levelStr == "info" {
    49  				logger.Info(g.context.GetRuntimeContext(), params...)
    50  			} else {
    51  				logger.Debug(g.context.GetRuntimeContext(), params...)
    52  			}
    53  			return nil
    54  		}
    55  	}
    56  	logger.Warning(g.context.GetRuntimeContext(), g.alarmType, params...)
    57  	return nil
    58  }