log_sink.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sink
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/topo/collector"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "sync"
  20. "time"
  21. )
  22. // NewLogSink log action, no properties now
  23. // example: {"log":{}}
  24. func NewLogSink() api.Sink {
  25. return collector.Func(func(ctx api.StreamContext, data interface{}) error {
  26. log := ctx.GetLogger()
  27. log.Infof("sink result for rule %s: %s", ctx.GetRuleId(), data)
  28. return nil
  29. })
  30. }
  31. type QueryResult struct {
  32. Results []string
  33. LastFetch time.Time
  34. Mux sync.Mutex
  35. }
  36. var QR = &QueryResult{LastFetch: time.Now()}
  37. func NewLogSinkToMemory() api.Sink {
  38. QR.Results = make([]string, 10)
  39. return collector.Func(func(ctx api.StreamContext, data interface{}) error {
  40. QR.Mux.Lock()
  41. QR.Results = append(QR.Results, fmt.Sprintf("%s", data))
  42. QR.Mux.Unlock()
  43. return nil
  44. })
  45. }