提交 3b61a05a authored 作者: 赵雪如's avatar 赵雪如

style(log): 修改了日志部分 以及fmt代码

上级 14216093
......@@ -9,21 +9,21 @@ import (
)
var (
codeNil = BizCode{0, "", ""}
CodeSuccess = BizCode{1, "操作成功", ""} // It is success.
CodeInternalError = BizCode{50, "Internal Error", ""}
CodeUnknown = BizCode{500, "An internal server error occurred", ""}
codeNil = BizCode{0, "", ""}
CodeSuccess = BizCode{1, "操作成功", ""} // It is success.
CodeInternalError = BizCode{50, "Internal Error", ""}
CodeUnknown = BizCode{500, "An internal server error occurred", ""}
)
type BizCode struct {
// C refers to the integer code of the ErrCode.
C int
C int
// M (user) facing error text.
M string
// Ref specify the reference document.
Ref interface{}
Ref interface{}
}
func init() {
......@@ -38,7 +38,6 @@ func (c BizCode) Message() string {
return c.M
}
func (c BizCode) Detail() interface{} {
return c.Ref
}
......@@ -47,7 +46,7 @@ var codes = map[int]gcode.Code{}
var codeMux = &sync.Mutex{}
func MustRegister(code int, message string) {
if code < 1000 {
if code < 1000 {
panic("Codes less than 1000 are reserved as internal codes")
}
codeMux.Lock()
......@@ -72,9 +71,9 @@ func ParseCoder(err error) gcode.Code {
if v, ok := code.(BizCode); ok {
if coder, ok := codes[v.C]; ok {
return BizCode{
C: coder.Code(),
M: strings.Replace(coder.Message(),"%%0", v.M, -1),
Ref : coder.Detail(),
C: coder.Code(),
M: strings.Replace(coder.Message(), "%%0", v.M, -1),
Ref: coder.Detail(),
}
}
}
......@@ -82,10 +81,10 @@ func ParseCoder(err error) gcode.Code {
return CodeUnknown
}
func New( code int, message string, reference string) error {
func New(code int, message string, reference string) error {
return gerror.NewCode(BizCode{
C: code,
M: message,
Ref : reference,
C: code,
M: message,
Ref: reference,
})
}
\ No newline at end of file
}
......@@ -5,7 +5,7 @@ import (
"testing"
)
func init() {
func init() {
MustRegister(100001, "我是%%0")
MustRegister(100002, "%%0不是我")
MustRegister(100003, "不是我")
......
......@@ -9,13 +9,13 @@ type MyWriter struct {
logger *glog.Logger
}
func init() {
func init() {
glog.SetWriter(&MyWriter{
logger: glog.New(),
})
}
func (w *MyWriter)Write(p []byte)(n int, err error) {
func (w *MyWriter) Write(p []byte) (n int, err error) {
//s := string(p)
//if gregex.IsMatchString(`PANI|FATA|ERRO`, s) {
// fmt.Println("SERIOUS ISSUE OCCURRED!! I'd better tell monitor in first time!")
......@@ -24,20 +24,20 @@ func (w *MyWriter)Write(p []byte)(n int, err error) {
return w.logger.Write(p)
}
type log struct {
Url string
Request interface{}
Respone interface{}
Error string
type Log struct {
Url string `json:"Url,omitempty"` //请求路径
Method string `json:"Method,omitempty"`
Request interface{} `json:"Request,omitempty"` //请求参数
Respone interface{} `json:"Respone,omitempty"` //响应参数
ResponeTime interface{} `json:"ResponeTime,omitempty"` //响应时间
Error error `json:"Error,omitempty"` //错误
Ext interface{} `json:"Ext,omitempty"` //扩展字段
}
func Info(ctx context.Context, cat string, url string, req interface{}, res interface{}) {
info := log{Url: url, Request: req, Respone: res}
glog.Ctx(ctx).Cat(cat).Info(info)
func Info(ctx context.Context, cat string, params *Log) {
glog.Ctx(ctx).Cat(cat).Info(params)
}
func Error(ctx context.Context, cat, url string, req interface{}, err error) {
info := log{Url: url, Request: req, Error:err.Error()}
glog.Ctx(ctx).Cat(cat).Error(info)
}
\ No newline at end of file
func Error(ctx context.Context, cat string, params *Log) {
glog.Ctx(ctx).Cat(cat).Error(params)
}
......@@ -2,14 +2,15 @@ package log
import (
"context"
"errors"
"github.com/gogf/gf/frame/g"
"gitlab.jxhh.com/zhaoxueru/common-base.git/pkg/errors"
"testing"
)
func TestLog(t *testing.T) {
Error(context.TODO(), "easy", "http://url/com", g.Map{
"a" : 1,
},errors.New("yyy"))
}
\ No newline at end of file
Error(context.TODO(), "easy", &Log{
Request: g.Map{"a": 1},
Error: errors.New(100000, "wocuole", ""),
})
}
......@@ -9,10 +9,10 @@ import (
// 数据返回通用JSON数据结构
type JsonResponse struct {
Id string `json:"id"` //请求id
Code int `json:"code"` // 错误码((1:成功, >1:错误码))
Message string `json:"message"` // 提示信息
Data interface{} `json:"data"` // 返回数据(业务接口定义具体数据结构)
Id string `json:"id"` //请求id
Code int `json:"code"` // 错误码((1:成功, >1:错误码))
Message string `json:"message"` // 提示信息
Data interface{} `json:"data"` // 返回数据(业务接口定义具体数据结构)
Redirect string `json:"redirect,omitempty"` // 引导客户端跳转到指定路由
}
......@@ -28,15 +28,13 @@ func Json(r *ghttp.Request, code int, message string, data ...interface{}) {
jsonResponse.Message = message
jsonResponse.Data = responseData
jsonResponse.Id = grand.S(20)
if !r.GetCtxVar("RequestId").IsNil(){
if !r.GetCtxVar("RequestId").IsNil() {
jsonResponse.Id = gconv.String(r.GetCtxVar("RequestId"))
}
r.Response.WriteJson(jsonResponse)
}
// 返回JSON数据并退出当前HTTP执行函数。
func Error(r *ghttp.Request, err error) {
coder := errors.ParseCoder(err)
......@@ -56,5 +54,3 @@ func SuccessWithData(r *ghttp.Request, data interface{}) {
Json(r, errors.CodeSuccess.Code(), errors.CodeSuccess.Message(), data)
r.Exit()
}
......@@ -4,7 +4,8 @@ import (
"gitlab.jxhh.com/zhaoxueru/common-base.git/pkg/errors"
"testing"
)
func init() {
func init() {
errors.MustRegister(100001,
"success")
errors.MustRegister(100002,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论