github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/log.go (about)

     1  package kiwi
     2  
     3  import (
     4  	"github.com/15mga/kiwi/sid"
     5  	"github.com/15mga/kiwi/util"
     6  	"os"
     7  	"runtime"
     8  	"strconv"
     9  )
    10  
    11  // ILogger 日志
    12  type ILogger interface {
    13  	// Log 记录日志
    14  	Log(level TLevel, msg, caller string, stack []byte, params util.M)
    15  	// Trace 标记链路
    16  	Trace(pid, tid int64, caller string, params util.M)
    17  	// Span 链路日志
    18  	Span(level TLevel, tid int64, msg, caller string, stack []byte, params util.M)
    19  }
    20  
    21  var (
    22  	TestLevels = []TLevel{TDebug, TInfo, TWarn, TError, TFatal}
    23  	DevLevels  = []TLevel{TInfo, TWarn, TError, TFatal}
    24  	ProdLevels = []TLevel{TWarn, TError, TFatal}
    25  )
    26  
    27  type TLevel = int64
    28  
    29  func StrToLevel(l string) TLevel {
    30  	switch l {
    31  	case SDebug:
    32  		return TDebug
    33  	case SInfo:
    34  		return TInfo
    35  	case SWarn:
    36  		return TWarn
    37  	case SError:
    38  		return TError
    39  	case SFatal:
    40  		return TFatal
    41  	default:
    42  		return TInfo
    43  	}
    44  }
    45  
    46  func LevelToStr(l TLevel) string {
    47  	switch l {
    48  	case TDebug:
    49  		return SDebug
    50  	case TInfo:
    51  		return SInfo
    52  	case TWarn:
    53  		return SWarn
    54  	case TError:
    55  		return SError
    56  	case TFatal:
    57  		return SFatal
    58  	default:
    59  		return SInfo
    60  	}
    61  }
    62  
    63  const (
    64  	TDebug TLevel = 1 << iota
    65  	TInfo
    66  	TWarn
    67  	TError
    68  	TFatal
    69  )
    70  
    71  const (
    72  	SDebug = "debug"
    73  	SInfo  = "info"
    74  	SWarn  = "warn"
    75  	SError = "error"
    76  	SFatal = "fatal"
    77  )
    78  
    79  const (
    80  	DefTimeFormatter = "2006-01-02 15:04:05.999"
    81  )
    82  
    83  func StrLvlToMask(levels ...string) TLevel {
    84  	slc := make([]TLevel, 0, len(levels))
    85  	for _, level := range levels {
    86  		slc = append(slc, StrToLevel(level))
    87  	}
    88  	return util.GenMask(slc...)
    89  }
    90  
    91  func LvlToMask(levels ...int64) TLevel {
    92  	slc := make([]TLevel, 0, len(levels))
    93  	for _, level := range levels {
    94  		slc = append(slc, level)
    95  	}
    96  	return util.GenMask(slc...)
    97  }
    98  
    99  var (
   100  	_LogDefParams    = util.M{}
   101  	_LogDefParamsLen int
   102  )
   103  
   104  func SetLogDefParams(params util.M) {
   105  	for k, v := range params {
   106  		_LogDefParams[k] = v
   107  	}
   108  	_LogDefParamsLen = len(_LogDefParams)
   109  }
   110  
   111  func copyLogParams(params util.M) {
   112  	for k, v := range _LogDefParams {
   113  		params[k] = v
   114  	}
   115  }
   116  
   117  var (
   118  	_Loggers    []ILogger
   119  	_CallerSkip = 2
   120  )
   121  
   122  func AddLogger(logger ILogger) {
   123  	_Loggers = append(_Loggers, logger)
   124  }
   125  
   126  func SetCallerSkip(skip int) {
   127  	_CallerSkip = skip
   128  }
   129  
   130  func log(level TLevel, msg string, stack []byte, params util.M) {
   131  	var caller string
   132  	for _, l := range _Loggers {
   133  		if params == nil && _LogDefParamsLen > 0 {
   134  			params = make(util.M, _LogDefParamsLen)
   135  		}
   136  		copyLogParams(params)
   137  		if caller == "" {
   138  			caller = GetCaller(_CallerSkip + 1)
   139  		}
   140  		l.Log(level, msg, caller, stack, params)
   141  	}
   142  }
   143  
   144  func span(level TLevel, tid int64, msg string, stack []byte, params util.M) {
   145  	var caller string
   146  	for _, l := range _Loggers {
   147  		if params == nil && _LogDefParamsLen > 0 {
   148  			params = make(util.M, _LogDefParamsLen)
   149  		}
   150  		copyLogParams(params)
   151  		if caller == "" {
   152  			caller = GetCaller(_CallerSkip + 1)
   153  		}
   154  		l.Span(level, tid, msg, caller, stack, params)
   155  	}
   156  }
   157  
   158  func Debug(str string, params util.M) {
   159  	log(TDebug, str, nil, params)
   160  }
   161  
   162  func Info(str string, params util.M) {
   163  	log(TInfo, str, nil, params)
   164  }
   165  
   166  func Warn(err *util.Err) {
   167  	if err == nil {
   168  		return
   169  	}
   170  	log(TWarn, err.String(), err.Stack(), err.Params())
   171  }
   172  
   173  func Warn2(code util.TErrCode, m util.M) {
   174  	err := util.NewErr(code, m)
   175  	log(TWarn, err.String(), err.Stack(), err.Params())
   176  }
   177  
   178  func Warn3(code util.TErrCode, e error) {
   179  	err := util.WrapErr(code, e)
   180  	log(TWarn, err.String(), err.Stack(), err.Params())
   181  }
   182  
   183  func Error(err *util.Err) {
   184  	if err == nil {
   185  		return
   186  	}
   187  	log(TError, err.String(), err.Stack(), err.Params())
   188  }
   189  
   190  func Error2(code util.TErrCode, m util.M) {
   191  	err := util.NewErr(code, m)
   192  	log(TError, err.String(), err.Stack(), err.Params())
   193  }
   194  
   195  func Error3(code util.TErrCode, e error) {
   196  	err := util.WrapErr(code, e)
   197  	log(TError, err.String(), err.Stack(), err.Params())
   198  }
   199  
   200  func Fatal(err *util.Err) {
   201  	if err == nil {
   202  		return
   203  	}
   204  	log(TFatal, err.String(), err.Stack(), err.Params())
   205  	os.Exit(1)
   206  }
   207  
   208  func Fatal2(code util.TErrCode, m util.M) {
   209  	err := util.NewErr(code, m)
   210  	log(TFatal, err.String(), err.Stack(), err.Params())
   211  	os.Exit(1)
   212  }
   213  
   214  func Fatal3(code util.TErrCode, e error) {
   215  	err := util.WrapErr(code, e)
   216  	log(TFatal, err.String(), err.Stack(), err.Params())
   217  	os.Exit(1)
   218  }
   219  
   220  // TC 链路标记
   221  func TC(pid int64, params util.M, exclude bool) int64 {
   222  	tid := sid.GetId()
   223  	if !exclude {
   224  		var caller string
   225  		for _, l := range _Loggers {
   226  			if params == nil && _LogDefParamsLen > 0 {
   227  				params = make(util.M, _LogDefParamsLen)
   228  			}
   229  			copyLogParams(params)
   230  			if caller == "" {
   231  				caller = GetCaller(_CallerSkip)
   232  			}
   233  			l.Trace(pid, tid, caller, params)
   234  		}
   235  	}
   236  	return tid
   237  }
   238  
   239  // TD 链路Debug
   240  func TD(tid int64, msg string, params util.M) {
   241  	span(TDebug, tid, msg, nil, params)
   242  }
   243  
   244  // TI 链路Info
   245  func TI(tid int64, msg string, params util.M) {
   246  	span(TInfo, tid, msg, nil, params)
   247  }
   248  
   249  // TW 链路Warn
   250  func TW(tid int64, err *util.Err) {
   251  	if err == nil {
   252  		return
   253  	}
   254  	span(TWarn, tid, err.String(), err.Stack(), err.Params())
   255  }
   256  
   257  func TW2(tid int64, code util.TErrCode, m util.M) {
   258  	err := util.NewErr(code, m)
   259  	span(TWarn, tid, err.String(), err.Stack(), err.Params())
   260  }
   261  
   262  func TW3(tid int64, code util.TErrCode, e error) {
   263  	err := util.WrapErr(code, e)
   264  	span(TWarn, tid, err.String(), err.Stack(), err.Params())
   265  }
   266  
   267  // TE 链路Error
   268  func TE(tid int64, err *util.Err) {
   269  	if err == nil {
   270  		return
   271  	}
   272  	span(TError, tid, err.String(), err.Stack(), err.Params())
   273  }
   274  
   275  func TE2(tid int64, code util.TErrCode, m util.M) {
   276  	err := util.NewErr(code, m)
   277  	span(TError, tid, err.String(), err.Stack(), err.Params())
   278  }
   279  
   280  func TE3(tid int64, code util.TErrCode, e error) {
   281  	err := util.WrapErr(code, e)
   282  	span(TError, tid, err.String(), err.Stack(), err.Params())
   283  }
   284  
   285  // TF 链路Fatal
   286  func TF(tid int64, err *util.Err) {
   287  	if err == nil {
   288  		return
   289  	}
   290  	span(TFatal, tid, err.String(), err.Stack(), err.Params())
   291  }
   292  
   293  func TF2(tid int64, code util.TErrCode, m util.M) {
   294  	err := util.NewErr(code, m)
   295  	span(TFatal, tid, err.String(), err.Stack(), err.Params())
   296  }
   297  
   298  func GetCaller(skip int) string {
   299  	_, file, line, ok := runtime.Caller(skip)
   300  	if !ok {
   301  		return ""
   302  	}
   303  	str := util.LogTrim(file) + ":" + strconv.Itoa(line)
   304  	return str
   305  }