1 Star 0 Fork 0

magicianlyx/GoLog

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
msg_official.go 4.67 KB
一键复制 编辑 原始数据 按行查看 历史
magicianlyx 提交于 2021-11-20 20:59 . -
package GoLog
import (
"fmt"
"gitee.com/magicianlyx/GoLog/errors"
"gitee.com/magicianlyx/GoLog/location"
jsoniter "github.com/json-iterator/go"
"time"
)
// 日志信息
type Msg struct {
Service string
LogType Kind // 日志类型
Locations []*location.Location // 定位
Level Level // 报错级别
Error *errors.Error // 错误抽象
LogDetail string // 详细信息
Parameter []interface{} // 参数
CreateTime time.Time // 创建时间
}
// 创建一个日志记录
func NewInfoMsg(service string, locations []*location.Location, detail string, params ...interface{}) *Msg {
r := &Msg{}
r.Service = service
r.LogType = typeOfficial
if locations != nil {
r.Locations = locations
}
r.Level = LevelInfo
r.LogDetail = detail
r.Parameter = params
r.CreateTime = time.Now()
return r
}
// 创建一个错误日志记录
func NewErrorMsg(service string, level Level, err *errors.Error, locations []*location.Location, v ...interface{}) *Msg {
r := &Msg{}
r.Service = service
r.LogType = typeOfficial
if locations != nil {
r.Locations = locations
}
r.Level = level
if err != nil {
r.Error = err
r.LogDetail = err.Detail.Error()
}
r.Parameter = v
r.CreateTime = time.Now()
return r
}
func (msg *Msg) GetType() Kind {
return msg.LogType
}
func (msg *Msg) GetLevel() Level {
return msg.Level
}
func (msg *Msg) ToContent() string {
level := "|" + msg.Level + "|"
service := msg.Service
t := time.Now().Format("2006-01-02 15:04:05")
tp := ""
if msg.Error != nil && msg.Error.GetCode() != 0 {
tp = fmt.Sprintf("[%v]", msg.Error.GetCode())
}
param := ""
if msg.Parameter != nil {
if paramsJson := toJSON(msg.Parameter); paramsJson != "" {
param = fmt.Sprintf(" params: %s", paramsJson)
}
}
message := ""
if msg.LogDetail != "" {
message = fmt.Sprintf("{msg: %s}", msg.LogDetail)
}
locations := ""
if msg.Locations != nil {
for _, l := range msg.Locations {
locations += "\r\n"
locations += l.ToContent()
}
}
text := fmt.Sprintf("%s %s %s => %s %s %s %s \r\n\r\n",
level,
service,
t,
tp,
message,
param,
locations,
)
return text
}
func (msg *Msg) ToColorfulContent() string {
level := colorForLevel(msg.Level)
service := msg.Service
t := time.Now().Format("2006-01-02 15:04:05")
tp := ""
if msg.Error != nil && msg.Error.GetCode() != 0 {
tp = fmt.Sprintf("[%v]", msg.Error.GetCode())
}
param := ""
if msg.Parameter != nil {
if paramsJson := toJSON(msg.Parameter); paramsJson != "" {
param = fmt.Sprintf(" params: %s", paramsJson)
}
}
message := ""
if msg.LogDetail != "" {
message = fmt.Sprintf("{msg: %s}", msg.LogDetail)
}
locations := ""
if msg.Locations != nil {
for _, l := range msg.Locations {
locations += "\r\n"
locations += l.ToContent()
}
}
text := fmt.Sprintf("%s %s %s => %s %s %s %s \r\n\r\n",
level,
service,
t,
tp,
message,
param,
locations,
)
return text
}
func (msg *Msg) ToMap() map[string]interface{} {
r := make(map[string]interface{})
if msg != nil {
r["log_type"] = msg.LogType
r["service"] = msg.Service
if msg.Locations != nil {
locations := make([]interface{}, 0)
for _, l := range msg.Locations {
locationMap := make(map[string]interface{})
locationMap["file_name"] = l.FileName
locationMap["line"] = l.Line
locationMap["function"] = l.Function
locations = append(locations, locationMap)
}
r["locations"] = locations
}
r["level"] = msg.Level
if msg.Error != nil {
r["error_code"] = msg.Error.GetCode()
r["error_msg"] = msg.Error.Error()
}
r["error_detail"] = msg.LogDetail
r["parameter"] = msg.Parameter
r["create_time"] = msg.CreateTime.Format("2006/01/02 - 15:04:05")
}
return r
}
func (msg *Msg) ToAlarm(attach map[string]interface{}) string {
r := make(map[string]interface{})
if msg != nil {
r["log_type"] = msg.LogType
r["service"] = msg.Service
r["level"] = msg.Level
if msg.Error != nil {
r["error_code"] = msg.Error.GetCode()
r["error_msg"] = msg.Error.Error()
}
r["error_detail"] = msg.LogDetail
r["parameter"] = msg.Parameter
r["create_time"] = msg.CreateTime.Format("2006/01/02 - 15:04:05")
}
for k, v := range attach {
r[k] = v
}
js, _ := jsoniter.MarshalToString(r)
return js
}
func (msg *Msg) ToJson() string {
js, _ := jsoniter.MarshalToString(msg.ToMap())
return js
}
func toJSON(parameter []interface{}) string {
paramsJson := ""
if parameter != nil && len(parameter) != 0 {
var err error
paramsJson, err = jsoniter.MarshalToString(parameter)
if err != nil {
// json格式化失败 直接%v输出
paramsJson = fmt.Sprintf("{%v}", parameter)
}
}
return paramsJson
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/magicianlyx/GoLog.git
git@gitee.com:magicianlyx/GoLog.git
magicianlyx
GoLog
GoLog
20c45f9b998d

搜索帮助