Go教程

聊聊dubbo-go-proxy的loggerFilter

本文主要是介绍聊聊dubbo-go-proxy的loggerFilter,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

本文主要研究一下dubbo-go-proxy的loggerFilter

loggerFilter

dubbo-go-proxy/pkg/filter/logger/logger.go

// nolint
func Init() {
    extension.SetFilterFunc(constant.LoggerFilter, loggerFilterFunc())
}

func loggerFilterFunc() context.FilterFunc {
    return New().Do()
}

// loggerFilter is a filter for simple logger.
type loggerFilter struct {
}

// New create logger filter.
func New() filter.Filter {
    return &loggerFilter{}
}
loggerFilter的Init方法往extension设置了loggerFilterFunc,该context.FilterFunc执行loggerFilter的Do方法

Do

dubbo-go-proxy/pkg/filter/logger/logger.go

// Logger logger filter, print url and latency
func (f loggerFilter) Do() context.FilterFunc {
    return func(c context.Context) {
        start := time.Now()

        c.Next()

        latency := time.Now().Sub(start)

        logger.Infof("[dubboproxy go] [UPSTREAM] receive request | %d | %s | %s | %s | ", c.StatusCode(), latency, c.GetMethod(), c.GetUrl())
    }
}
Do方法先记录下start,之后执行c.Next(),最后计算latency,最后打印下日志,输出status code,latency、method、url

小结

dubbo-go-proxy的loggerFilter的Init方法往extension设置了loggerFilterFunc,该context.FilterFunc执行loggerFilter的Do方法;Do方法先记录下start,之后执行c.Next(),最后计算latency,最后打印下日志,输出status code,latency、method、url。

doc

  • dubbo-go-proxy
这篇关于聊聊dubbo-go-proxy的loggerFilter的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!