2018-08-26 19:15:48 +08:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-08-06 23:06:55 +08:00
|
|
|
// +build !test
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux"
|
2018-09-27 00:58:51 +08:00
|
|
|
"github.com/mainflux/mainflux/logger"
|
2018-08-06 23:06:55 +08:00
|
|
|
"github.com/mainflux/mainflux/readers"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ readers.MessageRepository = (*loggingMiddleware)(nil)
|
|
|
|
|
|
|
|
type loggingMiddleware struct {
|
2018-09-27 00:58:51 +08:00
|
|
|
logger logger.Logger
|
2018-08-06 23:06:55 +08:00
|
|
|
svc readers.MessageRepository
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoggingMiddleware adds logging facilities to the core service.
|
2018-09-27 00:58:51 +08:00
|
|
|
func LoggingMiddleware(svc readers.MessageRepository, logger logger.Logger) readers.MessageRepository {
|
2018-08-06 23:06:55 +08:00
|
|
|
return &loggingMiddleware{
|
|
|
|
logger: logger,
|
|
|
|
svc: svc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 01:38:07 +08:00
|
|
|
func (lm *loggingMiddleware) ReadAll(chanID string, offset, limit uint64, query map[string]string) []mainflux.Message {
|
2018-08-06 23:06:55 +08:00
|
|
|
defer func(begin time.Time) {
|
2018-12-18 22:17:55 +08:00
|
|
|
lm.logger.Info(fmt.Sprintf(`Method read_all for offset %d and limit %d took %s to complete without errors.`, offset, limit, time.Since(begin)))
|
2018-08-06 23:06:55 +08:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-03-16 01:38:07 +08:00
|
|
|
return lm.svc.ReadAll(chanID, offset, limit, query)
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|