Package list golang-github-go-kit-kit / 83d577e9-6bde-44ab-b9f1-1c182b806b8a/main examples / stringsvc3 / logging.go
83d577e9-6bde-44ab-b9f1-1c182b806b8a/main

Tree @83d577e9-6bde-44ab-b9f1-1c182b806b8a/main (Download .tar.gz)

logging.go @83d577e9-6bde-44ab-b9f1-1c182b806b8a/mainraw · history · blame

package main

import (
	"time"

	"github.com/go-kit/kit/log"
)

func loggingMiddleware(logger log.Logger) ServiceMiddleware {
	return func(next StringService) StringService {
		return logmw{logger, next}
	}
}

type logmw struct {
	logger log.Logger
	StringService
}

func (mw logmw) Uppercase(s string) (output string, err error) {
	defer func(begin time.Time) {
		_ = mw.logger.Log(
			"method", "uppercase",
			"input", s,
			"output", output,
			"err", err,
			"took", time.Since(begin),
		)
	}(time.Now())

	output, err = mw.StringService.Uppercase(s)
	return
}

func (mw logmw) Count(s string) (n int) {
	defer func(begin time.Time) {
		_ = mw.logger.Log(
			"method", "count",
			"input", s,
			"n", n,
			"took", time.Since(begin),
		)
	}(time.Now())

	n = mw.StringService.Count(s)
	return
}