package api import ( "fmt" "time" "github.com/mainflux/mainflux" log "github.com/mainflux/mainflux/logger" "github.com/mainflux/mainflux/ws" ) var _ ws.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { logger log.Logger svc ws.Service } // LoggingMiddleware adds logging facilities to the adapter. func LoggingMiddleware(svc ws.Service, logger log.Logger) ws.Service { return &loggingMiddleware{logger, svc} } func (lm *loggingMiddleware) Publish(msg mainflux.RawMessage) (err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method publish to channel %s took %s to complete", msg.Channel, 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(msg) } func (lm *loggingMiddleware) Subscribe(chanID string, channel ws.Channel) (err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method subscribe to channel %s took %s to complete", chanID, 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(chanID, channel) }