mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-27 13:48:49 +08:00

* Change import name aliases Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Change import name aliases Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Change import aliases Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Remove unused aliases Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> Fix aliases Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> FIx errors Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> Fix error Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> FIx merge Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> FIx merge Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> FIx merge Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Fix import alias Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Fix errors Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Fix linter Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Fix linter Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Fix import Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Add linter to CI pipeline Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Changes Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Remove unused aliases Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Fix merge issues Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Fix gci Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Fix gci Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Fix gci Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Add gofumpt Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Remove multiple gofupmt in CI Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Remove unnecessary changes Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Fix linter Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> * Fix CI pipeline Signed-off-by: WashingtonKK <washingtonkigan@gmail.com> --------- Signed-off-by: WashingtonKK <washingtonkigan@gmail.com>
86 lines
2.9 KiB
Go
86 lines
2.9 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//go:build !test
|
|
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/mainflux/mainflux/coap"
|
|
mflog "github.com/mainflux/mainflux/logger"
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
|
)
|
|
|
|
var _ coap.Service = (*loggingMiddleware)(nil)
|
|
|
|
type loggingMiddleware struct {
|
|
logger mflog.Logger
|
|
svc coap.Service
|
|
}
|
|
|
|
// LoggingMiddleware adds logging facilities to the adapter.
|
|
func LoggingMiddleware(svc coap.Service, logger mflog.Logger) coap.Service {
|
|
return &loggingMiddleware{logger, svc}
|
|
}
|
|
|
|
// Publish logs the publish request. It logs the channel ID, subtopic (if any) and the time it took to complete the request.
|
|
// If the request fails, it logs the error.
|
|
func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messaging.Message) (err error) {
|
|
defer func(begin time.Time) {
|
|
destChannel := msg.Channel
|
|
if msg.Subtopic != "" {
|
|
destChannel = fmt.Sprintf("%s.%s", destChannel, msg.Subtopic)
|
|
}
|
|
message := fmt.Sprintf("Method publish to %s took %s to complete", destChannel, 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.Publish(ctx, key, msg)
|
|
}
|
|
|
|
// Subscribe logs the subscribe request. It logs the channel ID, subtopic (if any) and the time it took to complete the request.
|
|
// If the request fails, it logs the error.
|
|
func (lm *loggingMiddleware) Subscribe(ctx context.Context, key, chanID, subtopic string, c coap.Client) (err error) {
|
|
defer func(begin time.Time) {
|
|
destChannel := chanID
|
|
if subtopic != "" {
|
|
destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic)
|
|
}
|
|
message := fmt.Sprintf("Method subscribe to %s for client %s took %s to complete", destChannel, c.Token(), 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.Subscribe(ctx, key, chanID, subtopic, c)
|
|
}
|
|
|
|
// Unsubscribe logs the unsubscribe request. It logs the channel ID, subtopic (if any) and the time it took to complete the request.
|
|
// If the request fails, it logs the error.
|
|
func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, key, chanID, subtopic, token string) (err error) {
|
|
defer func(begin time.Time) {
|
|
destChannel := chanID
|
|
if subtopic != "" {
|
|
destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic)
|
|
}
|
|
message := fmt.Sprintf("Method unsubscribe for the client %s from the channel %s took %s to complete", token, destChannel, 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.Unsubscribe(ctx, key, chanID, subtopic, token)
|
|
}
|