代码拉取完成,页面将自动刷新
package GoLog
import (
"bytes"
"fmt"
"github.com/gin-gonic/gin"
jsoniter "github.com/json-iterator/go"
"io/ioutil"
"net/http"
"time"
)
type GinMsg struct {
Service string // 服务名
LogType Kind // 日志类型
ErrorMessage string // 信息
CreateTime time.Time // 创建时间
Path string // 访问路径
RawQuery string // 参数
Body string // 主题
Latency time.Duration // 执行时长
ClientIP string // 请求ip
StatusCode int // HTTP状态码
Method string // 方法名
}
// example r.Use(log.GinLog(log, service))
// 提供给gin的中间件
func GinLog() gin.HandlerFunc {
return func(context *gin.Context) {
msg := NewGinMsgByContext(context, serviceName)
log.Log(msg)
}
}
func NewGinMsgByContext(c *gin.Context, service string) *GinMsg {
t := timer()
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
body, _ := c.GetRawData()
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
c.Next()
latency := t()
clientIP := c.ClientIP()
method := c.Request.Method
statusCode := c.Writer.Status()
errorMessage := c.Errors.ByType(1 << 0).String()
if raw != "" {
path = path + "?" + raw
}
return newGinMsg(
service,
path,
raw,
string(body),
latency,
clientIP,
statusCode,
method,
errorMessage,
)
}
func newGinMsg(service, path, rawQuery, body string, latency time.Duration, clientIP string, statusCode int, method string, message string) *GinMsg {
return &GinMsg{
Service: service,
LogType: typeGin,
ErrorMessage: message,
CreateTime: time.Now(),
Path: path,
RawQuery: rawQuery,
Body: body,
Latency: latency,
ClientIP: clientIP,
StatusCode: statusCode,
Method: method,
}
}
var (
green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
yellow = string([]byte{27, 91, 57, 48, 59, 52, 51, 109})
red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
reset = string([]byte{27, 91, 48, 109})
)
func colorForLevel(level Level) string {
color := ""
switch level {
case LevelInfo:
color = green
case LevelWarning:
color = blue
case LevelError:
color = yellow
case LevelFatal:
color = red
default:
color = white
}
return fmt.Sprintf("%s %s %s", color, level, reset)
}
// 根据http状态获取颜色
func colorForStatus(code int) string {
color := ""
switch {
case code >= http.StatusOK && code < http.StatusMultipleChoices:
color = green
case code >= http.StatusMultipleChoices && code < http.StatusBadRequest:
color = white
case code >= http.StatusBadRequest && code < http.StatusInternalServerError:
color = yellow
default:
color = red
}
return fmt.Sprintf("%s %3d %s", color, code, reset)
}
// 根据http方法状态获取颜色
func colorForMethod(method string) string {
color := ""
switch method {
case "GET":
color = blue
case "POST":
color = cyan
case "PUT":
color = yellow
case "DELETE":
color = red
case "PATCH":
color = green
case "HEAD":
color = magenta
case "OPTIONS":
color = white
default:
color = reset
}
return fmt.Sprintf("%s %-7s %s", color, method, reset)
}
func (msg *GinMsg) GetType() Kind {
return msg.LogType
}
func (msg *GinMsg) GetLevel() Level {
return LevelNone
}
func (msg *GinMsg) ToContent() string {
return fmt.Sprintf("[GIN]%v | %v | %13v | %15v |%v %v %v\r\n\r\n",
msg.CreateTime.Format("2006/01/02 - 15:04:05"),
msg.StatusCode,
msg.Latency,
msg.ClientIP,
msg.Method,
msg.Path,
msg.ErrorMessage,
)
}
func (msg *GinMsg) ToColorfulContent() string {
var status, method string
status = colorForStatus(msg.StatusCode)
method = colorForMethod(msg.Method)
return fmt.Sprintf("[GIN]%v | %v | %13v | %15v |%v %v %v\r\n\r\n",
msg.CreateTime.Format("2006/01/02 - 15:04:05"),
status,
msg.Latency,
msg.ClientIP,
method,
msg.Path,
msg.ErrorMessage,
)
}
func (msg *GinMsg) ToMap() map[string]interface{} {
r := make(map[string]interface{})
if msg != nil {
r["service"] = msg.Service
r["log_type"] = msg.LogType
r["error_message"] = msg.ErrorMessage
r["create_time"] = msg.CreateTime
r["path"] = msg.Path
r["raw_query"] = msg.RawQuery
r["body"] = msg.Body
r["latency"] = msg.Latency.String()
r["client_ip"] = msg.ClientIP
}
return r
}
func (msg *GinMsg) ToAlarm(attach map[string]interface{}) string {
r := msg.ToMap()
for k, v := range attach {
r[k] = v
}
js, _ := jsoniter.MarshalToString(r)
return js
}
func (msg *GinMsg) ToJson() string {
js, _ := jsoniter.MarshalToString(msg.ToMap())
return js
}
// 计时器
func timer() func() time.Duration {
start := time.Now()
return func() time.Duration {
return time.Now().Sub(start)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。