2018-08-26 19:15:48 +08:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-06-01 21:50:23 +08:00
|
|
|
// +build !test
|
|
|
|
|
2019-05-08 01:09:28 +08:00
|
|
|
package api
|
2018-06-01 21:50:23 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
log "github.com/mainflux/mainflux/logger"
|
2019-05-08 01:09:28 +08:00
|
|
|
"github.com/mainflux/mainflux/writers"
|
2018-06-01 21:50:23 +08:00
|
|
|
)
|
|
|
|
|
2019-05-08 01:09:28 +08:00
|
|
|
var _ writers.MessageRepository = (*loggingMiddleware)(nil)
|
2018-06-01 21:50:23 +08:00
|
|
|
|
|
|
|
type loggingMiddleware struct {
|
|
|
|
logger log.Logger
|
2019-05-08 01:09:28 +08:00
|
|
|
svc writers.MessageRepository
|
2018-06-01 21:50:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoggingMiddleware adds logging facilities to the adapter.
|
2019-05-08 01:09:28 +08:00
|
|
|
func LoggingMiddleware(svc writers.MessageRepository, logger log.Logger) writers.MessageRepository {
|
2018-06-01 21:50:23 +08:00
|
|
|
return &loggingMiddleware{logger, svc}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lm *loggingMiddleware) Save(msg mainflux.Message) (err error) {
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
message := fmt.Sprintf("Method Save took %s to complete", time.Since(begin))
|
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
return lm.svc.Save(msg)
|
|
|
|
}
|