github.com/Tyktechnologies/tyk@v2.9.5+incompatible/gateway/redis_logrus_hook.go (about)

     1  package gateway
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/sirupsen/logrus"
     7  
     8  	"github.com/TykTechnologies/tyk/storage"
     9  )
    10  
    11  type redisChannelHook struct {
    12  	notifier  RedisNotifier
    13  	formatter logrus.Formatter
    14  }
    15  
    16  func newRedisHook() *redisChannelHook {
    17  	hook := &redisChannelHook{}
    18  	hook.formatter = new(logrus.JSONFormatter)
    19  	hook.notifier.store = &storage.RedisCluster{KeyPrefix: "gateway-notifications:"}
    20  	hook.notifier.channel = "dashboard.ui.messages"
    21  	return hook
    22  }
    23  
    24  func (hook *redisChannelHook) Fire(entry *logrus.Entry) error {
    25  
    26  	orgId, found := entry.Data["org_id"]
    27  	if !found {
    28  		return nil
    29  	}
    30  
    31  	newEntry, err := hook.formatter.Format(entry)
    32  	if err != nil {
    33  		log.Error(err)
    34  		return nil
    35  	}
    36  
    37  	msg := string(newEntry)
    38  
    39  	n := InterfaceNotification{
    40  		Type:      "gateway-log",
    41  		Message:   msg,
    42  		OrgID:     orgId.(string),
    43  		Timestamp: time.Now(),
    44  	}
    45  
    46  	go hook.notifier.Notify(n)
    47  	return nil
    48  }
    49  
    50  type InterfaceNotification struct {
    51  	Type      string
    52  	Message   string
    53  	OrgID     string
    54  	Timestamp time.Time
    55  }
    56  
    57  func (hook *redisChannelHook) Levels() []logrus.Level {
    58  	return []logrus.Level{
    59  		logrus.InfoLevel,
    60  		logrus.ErrorLevel,
    61  		logrus.FatalLevel,
    62  		logrus.PanicLevel,
    63  	}
    64  }