github.com/avenga/couper@v1.12.2/logging/hooks/context.go (about)

     1  package hooks
     2  
     3  import (
     4  	"sync/atomic"
     5  
     6  	"github.com/sirupsen/logrus"
     7  
     8  	"github.com/avenga/couper/config/request"
     9  	"github.com/avenga/couper/logging"
    10  )
    11  
    12  var _ logrus.Hook = &Context{}
    13  
    14  type Context struct{}
    15  
    16  func (c *Context) Levels() []logrus.Level {
    17  	return logrus.AllLevels
    18  }
    19  
    20  func (c *Context) Fire(entry *logrus.Entry) error {
    21  	_, exist := entry.Data["uid"]
    22  	if entry.Context != nil && !exist {
    23  		if uid := entry.Context.Value(request.UID); uid != nil {
    24  			entry.Data["uid"] = uid
    25  		}
    26  	}
    27  
    28  	if field, ok := entry.Data["type"]; ok && field == beTypeField {
    29  		if bytes, i := entry.Context.Value(request.BackendBytes).(*int64); i {
    30  			response, r := entry.Data["response"].(logging.Fields)
    31  			b := atomic.LoadInt64(bytes)
    32  			if r && b > 0 {
    33  				response["bytes"] = b
    34  			}
    35  		}
    36  	}
    37  
    38  	return nil
    39  }