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

     1  package log
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	sls "github.com/aliyun/aliyun-log-go-sdk"
     7  	"github.com/gin-gonic/gin"
     8  	"github.com/gogo/protobuf/proto"
     9  	"github.com/jtzjtz/kit/conn"
    10  	"os"
    11  
    12  	"time"
    13  )
    14  
    15  //添加access日志到SLS(程序运行前,请初始化conn.InitSLSProducer)
    16  func AddAccessMap(data map[string]string) error {
    17  	logConfig := conn.GetDefaultAccessLogConfig()
    18  	if len(logConfig.Project) == 0 || len(logConfig.Logstore) == 0 {
    19  		return errors.New("未设置logstore")
    20  	}
    21  	return conn.SendMap(data, logConfig)
    22  }
    23  
    24  //添加info日志到SLS(程序运行前,请初始化conn.InitSLSProducer)
    25  func AddInfoMap(data map[string]string, project ...conn.LogProjectConf) error {
    26  	logConfig := conn.GetDefaultInfoLogConfig()
    27  	if len(project) > 0 {
    28  		logConfig = project[0]
    29  	}
    30  	if len(logConfig.Project) == 0 || len(logConfig.Logstore) == 0 {
    31  		return errors.New("")
    32  	}
    33  	return conn.SendMap(data, logConfig)
    34  }
    35  
    36  //添加info日志到SLS(程序运行前,请初始化conn.InitSLSProducer)
    37  func AddInfo(info InfoEntity, ctx *gin.Context, project ...conn.LogProjectConf) error {
    38  	logConfig := conn.GetDefaultInfoLogConfig()
    39  	if len(project) > 0 {
    40  		logConfig = project[0]
    41  	}
    42  	if len(logConfig.Project) == 0 || len(logConfig.Logstore) == 0 {
    43  		return errors.New("")
    44  	}
    45  
    46  	content := []*sls.LogContent{}
    47  	hostname, _ := os.Hostname()
    48  	if ctx != nil {
    49  		content = append(content, []*sls.LogContent{
    50  
    51  			&sls.LogContent{
    52  				Key:   proto.String("customer_id"),
    53  				Value: proto.String(ctx.Param("customer_id")),
    54  			},
    55  			&sls.LogContent{
    56  				Key:   proto.String("request_id"),
    57  				Value: proto.String(ctx.GetHeader("Request_Id")),
    58  			}, &sls.LogContent{
    59  				Key:   proto.String("host"),
    60  				Value: proto.String(ctx.Request.Host),
    61  			}, &sls.LogContent{
    62  				Key:   proto.String("uri"),
    63  				Value: proto.String(ctx.Request.RequestURI),
    64  			},
    65  		}...)
    66  	}
    67  
    68  	content = append(content, []*sls.LogContent{
    69  		&sls.LogContent{
    70  			Key:   proto.String("server_name"),
    71  			Value: proto.String(hostname),
    72  		},
    73  		&sls.LogContent{
    74  			Key:   proto.String("level"),
    75  			Value: proto.String("INFO"),
    76  		}, &sls.LogContent{
    77  			Key:   proto.String("time"),
    78  			Value: proto.String(time.Now().Format("2006-01-02 15:04:05")),
    79  		}, &sls.LogContent{
    80  			Key:   proto.String("msg"),
    81  			Value: proto.String(info.Msg),
    82  		}, &sls.LogContent{
    83  			Key:   proto.String("env"),
    84  			Value: proto.String(info.Env),
    85  		},
    86  	}...)
    87  	byteData, byteEr := json.Marshal(info.Data)
    88  	if byteEr == nil {
    89  		content = append(content, &sls.LogContent{
    90  			Key:   proto.String("data"),
    91  			Value: proto.String(string(byteData)),
    92  		})
    93  	}
    94  	if info.TraceId != "" {
    95  		content = append(content, &sls.LogContent{
    96  			Key:   proto.String("trace_id"),
    97  			Value: proto.String(info.TraceId),
    98  		})
    99  
   100  	} else {
   101  		content = append(content, &sls.LogContent{
   102  			Key:   proto.String("trace_id"),
   103  			Value: proto.String(ctx.GetHeader("Trace_Id")),
   104  		})
   105  	}
   106  
   107  	log := &sls.Log{
   108  		Time:     proto.Uint32(uint32(time.Now().Unix())),
   109  		Contents: content,
   110  	}
   111  	return conn.SendLog(log, logConfig)
   112  }
   113  
   114  //添加error日志到SLS(程序运行前,请初始化conn.InitSLSProducer)
   115  func AddError(logError ErrEntity, ctx *gin.Context, project ...conn.LogProjectConf) error {
   116  	logConfig := conn.GetDefaultErrorLogConfig()
   117  	if len(project) > 0 {
   118  		logConfig = project[0]
   119  	}
   120  	if len(logConfig.Project) == 0 || len(logConfig.Logstore) == 0 {
   121  		return errors.New("")
   122  	}
   123  
   124  	content := []*sls.LogContent{}
   125  	hostname, _ := os.Hostname()
   126  	if ctx != nil {
   127  		content = append(content, []*sls.LogContent{
   128  			&sls.LogContent{
   129  				Key:   proto.String("customer_id"),
   130  				Value: proto.String(ctx.Param("customer_id")),
   131  			},
   132  			&sls.LogContent{
   133  				Key:   proto.String("request_id"),
   134  				Value: proto.String(ctx.GetHeader("Request_Id")),
   135  			}, &sls.LogContent{
   136  				Key:   proto.String("host"),
   137  				Value: proto.String(ctx.Request.Host),
   138  			}, &sls.LogContent{
   139  				Key:   proto.String("uri"),
   140  				Value: proto.String(ctx.Request.RequestURI),
   141  			},
   142  		}...)
   143  	}
   144  
   145  	content = append(content, []*sls.LogContent{
   146  		&sls.LogContent{
   147  			Key:   proto.String("server_name"),
   148  			Value: proto.String(hostname),
   149  		},
   150  		&sls.LogContent{
   151  			Key:   proto.String("level"),
   152  			Value: proto.String("ERROR"),
   153  		}, &sls.LogContent{
   154  			Key:   proto.String("time"),
   155  			Value: proto.String(time.Now().Format("2006-01-02 15:04:05")),
   156  		}, &sls.LogContent{
   157  			Key:   proto.String("msg"),
   158  			Value: proto.String(logError.Msg),
   159  		}, &sls.LogContent{
   160  			Key:   proto.String("env"),
   161  			Value: proto.String(logError.Env),
   162  		},
   163  	}...)
   164  	byteData, byteEr := json.Marshal(logError.Data)
   165  	if byteEr == nil {
   166  		content = append(content, &sls.LogContent{
   167  			Key:   proto.String("data"),
   168  			Value: proto.String(string(byteData)),
   169  		})
   170  	}
   171  	if logError.TraceId != "" {
   172  		content = append(content, &sls.LogContent{
   173  			Key:   proto.String("trace_id"),
   174  			Value: proto.String(logError.TraceId),
   175  		})
   176  
   177  	} else {
   178  		content = append(content, &sls.LogContent{
   179  			Key:   proto.String("trace_id"),
   180  			Value: proto.String(ctx.GetHeader("Trace_Id")),
   181  		})
   182  	}
   183  
   184  	log := &sls.Log{
   185  		Time:     proto.Uint32(uint32(time.Now().Unix())),
   186  		Contents: content,
   187  	}
   188  	return conn.SendLog(log, logConfig)
   189  }
   190  
   191  //添加msg日志到SLS(程序运行前,请初始化conn.InitSLSProducer)
   192  func AddLogStr(msg string, ctx *gin.Context) error {
   193  	return AddInfo(InfoEntity{Msg: msg}, ctx)
   194  }
   195  
   196  //添加error日志到SLS(程序运行前,请初始化conn.InitSLSProducer)
   197  func AddErrorMap(data map[string]string, project ...conn.LogProjectConf) error {
   198  	logConfig := conn.GetDefaultErrorLogConfig()
   199  	if len(project) > 0 {
   200  		logConfig = project[0]
   201  	}
   202  	if len(logConfig.Project) == 0 || len(logConfig.Logstore) == 0 {
   203  		return errors.New("")
   204  	}
   205  	return conn.SendMap(data, logConfig)
   206  }