github.com/jtzjtz/kit@v1.0.2/log/logger.go (about)

     1  package log
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/gin-gonic/gin"
     7  	"github.com/jtzjtz/kit/http"
     8  	"runtime"
     9  	"strings"
    10  	"time"
    11  )
    12  
    13  type InfoEntity struct {
    14  	Data    interface{} `json:"data"` //额外保存的数据
    15  	Msg     string      `json:"msg"`
    16  	TraceId string      `json:"trace_id"`
    17  	Env     string      `json:"env"` //环境
    18  	Caller  string      `json:"caller"`
    19  }
    20  type ErrEntity struct {
    21  	Data    interface{} `json:"data"` //额外保存的数据
    22  	Msg     string      `json:"msg"`
    23  	TraceId string      `json:"trace_id"`
    24  	Env     string      `json:"env"` //环境
    25  	Caller  string      `json:"caller"`
    26  }
    27  
    28  //初始化日志服务 handlerCnt:4 处理日志协程数量  logChannelCnt:1000 日志通道缓存大小
    29  
    30  func InitLogger(appName, logDir string, handlerCnt, logChannelCnt int) error {
    31  	er := initLog(appName, logDir)
    32  	initHandler(handlerCnt, logChannelCnt)
    33  	return er
    34  }
    35  
    36  //添加info日志到本地 ,ctx 没有传nil
    37  
    38  func LogInfo(info InfoEntity, ctx *gin.Context) error {
    39  	if Logger == nil || logChannel == nil {
    40  		return errors.New("日志服务未初始化")
    41  	}
    42  	if info.TraceId == "" && ctx != nil {
    43  		info.TraceId = http.GetTraceId(ctx)
    44  	}
    45  	info.Caller = caller(3)
    46  	pushChannel(logEntity{LogLevel: INFOLEVEL, LogData: info})
    47  	return nil
    48  }
    49  func LogInfoData(data interface{}, ctx *gin.Context) error {
    50  	return LogInfo(InfoEntity{Data: data}, ctx)
    51  }
    52  
    53  //记录info msg ,ctx 没有传nil
    54  
    55  func LogInfoMsg(msg string, ctx *gin.Context) error {
    56  	return LogInfo(InfoEntity{Msg: msg}, ctx)
    57  }
    58  func LogInfoMsgData(msg string, data interface{}, ctx *gin.Context) error {
    59  	return LogInfo(InfoEntity{Msg: msg, Data: data}, ctx)
    60  }
    61  
    62  //添加error日志到本地,ctx 没有传nil
    63  
    64  func LogError(logError ErrEntity, ctx *gin.Context) error {
    65  	if Logger == nil || logChannel == nil {
    66  		return errors.New("日志服务未初始化")
    67  	}
    68  	if logError.TraceId == "" && ctx != nil {
    69  		logError.TraceId = http.GetTraceId(ctx)
    70  
    71  	}
    72  	logError.Caller = caller(3)
    73  
    74  	pushChannel(logEntity{LogLevel: ERRORLEVEL, LogData: logError})
    75  	return nil
    76  }
    77  
    78  //记录error msg ,ctx 没有传nil
    79  
    80  func LogErrorMsg(msg string, ctx *gin.Context) error {
    81  	return LogError(ErrEntity{Msg: msg}, ctx)
    82  }
    83  func LogErrorData(data interface{}, ctx *gin.Context) error {
    84  	return LogError(ErrEntity{Data: data}, ctx)
    85  }
    86  func LogErrorMsgData(msg string, data interface{}, ctx *gin.Context) error {
    87  	return LogError(ErrEntity{Data: data, Msg: msg}, ctx)
    88  }
    89  
    90  //记录accesslog
    91  
    92  func LogAccess(data map[string]string) error {
    93  	if Logger == nil || logChannel == nil {
    94  		return errors.New("日志服务未初始化")
    95  	}
    96  	//Logger.Debugw("access", "logdata", data)
    97  	pushChannel(logEntity{LogLevel: ACCESSLEVEL, LogData: data})
    98  	return nil
    99  }
   100  
   101  func pushChannel(entity logEntity) {
   102  	c := time.After(time.Millisecond * 500)
   103  	select {
   104  	case <-c:
   105  		println("push log channel timeout 500ms")
   106  	case logChannel <- entity:
   107  	}
   108  }
   109  func caller(skip int) string {
   110  	var sb strings.Builder
   111  	layer := 1
   112  	for ; skip < 6; skip++ {
   113  		pc, file, line, ok := runtime.Caller(skip)
   114  		if ok {
   115  			pcName := runtime.FuncForPC(pc).Name() //获取函数名
   116  			sb.WriteString(fmt.Sprintf("【%d】 %s [%s:%d]\n", layer, file, pcName, line))
   117  			layer++
   118  		}
   119  
   120  	}
   121  	return sb.String()
   122  
   123  }