123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package main
- import (
- "context"
- "github.com/go-kit/kit/log"
- "github.com/longjoy/micro-go-course/section35/zipkin-kit/string-service/service"
- "time"
- )
- // loggingMiddleware Make a new type
- // that contains Service interface and logger instance
- type loggingMiddleware struct {
- service.Service
- logger log.Logger
- }
- // LoggingMiddleware make logging middleware
- func LoggingMiddleware(logger log.Logger) service.ServiceMiddleware {
- return func(next service.Service) service.Service {
- return loggingMiddleware{next, logger}
- }
- }
- func (mw loggingMiddleware) Concat(a, b string) (ret string, err error) {
- defer func(begin time.Time) {
- mw.logger.Log(
- "function", "Concat",
- "a", a,
- "b", b,
- "result", ret,
- "took", time.Since(begin),
- )
- }(time.Now())
- ret, err = mw.Service.Concat(a, b)
- return ret, err
- }
- func (mw loggingMiddleware) Diff(ctx context.Context, a, b string) (ret string, err error) {
- defer func(begin time.Time) {
- mw.logger.Log(
- "function", "Diff",
- "a", a,
- "b", b,
- "result", ret,
- "took", time.Since(begin),
- )
- }(time.Now())
- ret, err = mw.Service.Diff(ctx, a, b)
- return ret, err
- }
- func (mw loggingMiddleware) HealthCheck() (result bool) {
- defer func(begin time.Time) {
- mw.logger.Log(
- "function", "HealthChcek",
- "result", result,
- "took", time.Since(begin),
- )
- }(time.Now())
- result = mw.Service.HealthCheck()
- return
- }
|