You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
2.9 KiB
105 lines
2.9 KiB
1 year ago
|
package sls
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"time"
|
||
|
|
||
|
sls "github.com/aliyun/aliyun-log-go-sdk"
|
||
|
"google.golang.org/protobuf/proto"
|
||
|
)
|
||
|
|
||
|
// GenerateLog 传入固定请求日志,生成sls对应格式日志
|
||
|
func GenerateLog(request *Request) *sls.Log {
|
||
|
var log sls.Log
|
||
|
// 获取当前时间
|
||
|
log.Time = proto.Uint32(uint32(time.Now().Unix()))
|
||
|
contents := make([]*sls.LogContent, 0)
|
||
|
// time
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("time"),
|
||
|
Value: proto.String(request.Time.Format("2006-01-02 15:04:05")),
|
||
|
})
|
||
|
// remote_addr
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("remote_addr"),
|
||
|
Value: proto.String(request.RemoteAddr),
|
||
|
})
|
||
|
// query_string
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("query_string"),
|
||
|
Value: proto.String(request.QueryString),
|
||
|
})
|
||
|
// request_method
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("request_method"),
|
||
|
Value: proto.String(request.RequestMethod),
|
||
|
})
|
||
|
// request_uri
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("request_uri"),
|
||
|
Value: proto.String(request.RequestUri),
|
||
|
})
|
||
|
// host
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("host"),
|
||
|
Value: proto.String(request.Host),
|
||
|
})
|
||
|
// request_body
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("request_body"),
|
||
|
Value: proto.String(request.RequestBody),
|
||
|
})
|
||
|
// http_user_agent
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("http_user_agent"),
|
||
|
Value: proto.String(request.HttpUserAgent),
|
||
|
})
|
||
|
// http_finger
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("http_finger"),
|
||
|
Value: proto.String(request.HttpFinger),
|
||
|
})
|
||
|
// http_referrer
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("http_referrer"),
|
||
|
Value: proto.String(request.HttpReferrer),
|
||
|
})
|
||
|
// http_trace_id
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("http_trace_id"),
|
||
|
Value: proto.String(request.HttpTraceId),
|
||
|
})
|
||
|
// x_app_id
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("x_app_id"),
|
||
|
Value: proto.String(request.XAppId),
|
||
|
})
|
||
|
// x_user_data
|
||
|
userData, _ := json.Marshal(request.XUserData)
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("x_user_data"),
|
||
|
Value: proto.String(string(userData)),
|
||
|
})
|
||
|
// x_dsm_data
|
||
|
dsmData, _ := json.Marshal(request.XDsmData)
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("x_dsm_data"),
|
||
|
Value: proto.String(string(dsmData)),
|
||
|
})
|
||
|
// http_trace_id
|
||
|
customData, _ := json.Marshal(request.XCustomData)
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("x_custom_data"),
|
||
|
Value: proto.String(string(customData)),
|
||
|
})
|
||
|
// response_data
|
||
|
respData, _ := json.Marshal(request.ResponseData)
|
||
|
contents = append(contents, &sls.LogContent{
|
||
|
Key: proto.String("response_data"),
|
||
|
Value: proto.String(string(respData)),
|
||
|
})
|
||
|
|
||
|
log.Contents = contents
|
||
|
return &log
|
||
|
}
|