github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/client/logger.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/sirupsen/logrus"
     6  )
     7  
     8  type Logger interface {
     9  	// Log a message at the given level with context key/value pairs
    10  	Trace(msg string, ctx ...interface{})
    11  	Debug(msg string, ctx ...interface{})
    12  	Info(msg string, ctx ...interface{})
    13  	Warn(msg string, ctx ...interface{})
    14  	Error(msg string, ctx ...interface{})
    15  }
    16  
    17  type log struct {
    18  	entry *logrus.Entry
    19  }
    20  
    21  func NewLog(field ...interface{}) Logger {
    22  	return &log{logrus.WithFields(transField(field))}
    23  }
    24  
    25  func (l *log) Trace(msg string, ctx ...interface{}) {
    26  	l.entry.WithFields(transField(ctx)).Trace(msg)
    27  }
    28  
    29  func (l *log) Debug(msg string, ctx ...interface{}) {
    30  	l.entry.WithFields(transField(ctx)).Debug(msg)
    31  }
    32  
    33  func (l *log) Info(msg string, ctx ...interface{}) {
    34  	l.entry.WithFields(transField(ctx)).Info(msg)
    35  }
    36  
    37  func (l *log) Warn(msg string, ctx ...interface{}) {
    38  	l.entry.WithFields(transField(ctx)).Warn(msg)
    39  }
    40  
    41  func (l *log) Error(msg string, ctx ...interface{}) {
    42  	l.entry.WithFields(transField(ctx)).Error(msg)
    43  }
    44  
    45  func transField(datas []interface{}) logrus.Fields {
    46  	field := make(logrus.Fields)
    47  	for i := 0; i < len(datas); i += 2 {
    48  		key := fmt.Sprintf("%v", datas[i])
    49  		if i+1 < len(datas) {
    50  			field[key] = datas[i+1]
    51  		} else {
    52  			field["field"] = datas[i]
    53  		}
    54  	}
    55  	return field
    56  }