log_node.go 934 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package nodes
  2. import (
  3. "encoding/json"
  4. "github.com/Sirupsen/logrus"
  5. "sparrow/pkg/protocol"
  6. "sparrow/pkg/ruleEngine"
  7. )
  8. type LogNodeConfig struct {
  9. Stdout bool `json:"stdout"` // 标准控制台输出
  10. }
  11. // LogNode 日志打印节点
  12. type LogNode struct {
  13. config *LogNodeConfig
  14. mLog *logrus.Logger
  15. }
  16. func (l *LogNode) Init(ctx ruleEngine.Context, config string) error {
  17. if config == "" {
  18. l.config = &LogNodeConfig{
  19. Stdout: false,
  20. }
  21. } else {
  22. c := new(LogNodeConfig)
  23. err := json.Unmarshal([]byte(config), c)
  24. if err != nil {
  25. return err
  26. }
  27. l.config = c
  28. }
  29. l.mLog = logrus.New()
  30. return nil
  31. }
  32. func (l *LogNode) OnMessage(ctx ruleEngine.Context, message *protocol.Message) error {
  33. if l.config.Stdout {
  34. l.mLog.Infof("日志节点:[设备ID:%s][消息类型:%s][消息内容:%s]", message.MetaData["device_id"], message.Type, message.Data)
  35. }
  36. ctx.TellNext(message, protocol.Success)
  37. return nil
  38. }