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

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

上级 14216093
...@@ -38,7 +38,6 @@ func (c BizCode) Message() string { ...@@ -38,7 +38,6 @@ func (c BizCode) Message() string {
return c.M return c.M
} }
func (c BizCode) Detail() interface{} { func (c BizCode) Detail() interface{} {
return c.Ref return c.Ref
} }
...@@ -73,8 +72,8 @@ func ParseCoder(err error) gcode.Code { ...@@ -73,8 +72,8 @@ func ParseCoder(err error) gcode.Code {
if coder, ok := codes[v.C]; ok { if coder, ok := codes[v.C]; ok {
return BizCode{ return BizCode{
C: coder.Code(), C: coder.Code(),
M: strings.Replace(coder.Message(),"%%0", v.M, -1), M: strings.Replace(coder.Message(), "%%0", v.M, -1),
Ref : coder.Detail(), Ref: coder.Detail(),
} }
} }
} }
...@@ -82,10 +81,10 @@ func ParseCoder(err error) gcode.Code { ...@@ -82,10 +81,10 @@ func ParseCoder(err error) gcode.Code {
return CodeUnknown return CodeUnknown
} }
func New( code int, message string, reference string) error { func New(code int, message string, reference string) error {
return gerror.NewCode(BizCode{ return gerror.NewCode(BizCode{
C: code, C: code,
M: message, M: message,
Ref : reference, Ref: reference,
}) })
} }
...@@ -15,7 +15,7 @@ func init() { ...@@ -15,7 +15,7 @@ func init() {
}) })
} }
func (w *MyWriter)Write(p []byte)(n int, err error) { func (w *MyWriter) Write(p []byte) (n int, err error) {
//s := string(p) //s := string(p)
//if gregex.IsMatchString(`PANI|FATA|ERRO`, s) { //if gregex.IsMatchString(`PANI|FATA|ERRO`, s) {
// fmt.Println("SERIOUS ISSUE OCCURRED!! I'd better tell monitor in first time!") // 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) { ...@@ -24,20 +24,20 @@ func (w *MyWriter)Write(p []byte)(n int, err error) {
return w.logger.Write(p) return w.logger.Write(p)
} }
type Log struct {
type log struct { Url string `json:"Url,omitempty"` //请求路径
Url string Method string `json:"Method,omitempty"`
Request interface{} Request interface{} `json:"Request,omitempty"` //请求参数
Respone interface{} Respone interface{} `json:"Respone,omitempty"` //响应参数
Error string 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{}) { func Info(ctx context.Context, cat string, params *Log) {
info := log{Url: url, Request: req, Respone: res} glog.Ctx(ctx).Cat(cat).Info(params)
glog.Ctx(ctx).Cat(cat).Info(info)
} }
func Error(ctx context.Context, cat, url string, req interface{}, err error) { func Error(ctx context.Context, cat string, params *Log) {
info := log{Url: url, Request: req, Error:err.Error()} glog.Ctx(ctx).Cat(cat).Error(params)
glog.Ctx(ctx).Cat(cat).Error(info)
} }
...@@ -2,14 +2,15 @@ package log ...@@ -2,14 +2,15 @@ package log
import ( import (
"context" "context"
"errors"
"github.com/gogf/gf/frame/g" "github.com/gogf/gf/frame/g"
"gitlab.jxhh.com/zhaoxueru/common-base.git/pkg/errors"
"testing" "testing"
) )
func TestLog(t *testing.T) { func TestLog(t *testing.T) {
Error(context.TODO(), "easy", "http://url/com", g.Map{ Error(context.TODO(), "easy", &Log{
"a" : 1, Request: g.Map{"a": 1},
},errors.New("yyy")) Error: errors.New(100000, "wocuole", ""),
})
} }
...@@ -28,15 +28,13 @@ func Json(r *ghttp.Request, code int, message string, data ...interface{}) { ...@@ -28,15 +28,13 @@ func Json(r *ghttp.Request, code int, message string, data ...interface{}) {
jsonResponse.Message = message jsonResponse.Message = message
jsonResponse.Data = responseData jsonResponse.Data = responseData
jsonResponse.Id = grand.S(20) jsonResponse.Id = grand.S(20)
if !r.GetCtxVar("RequestId").IsNil(){ if !r.GetCtxVar("RequestId").IsNil() {
jsonResponse.Id = gconv.String(r.GetCtxVar("RequestId")) jsonResponse.Id = gconv.String(r.GetCtxVar("RequestId"))
} }
r.Response.WriteJson(jsonResponse) r.Response.WriteJson(jsonResponse)
} }
// 返回JSON数据并退出当前HTTP执行函数。 // 返回JSON数据并退出当前HTTP执行函数。
func Error(r *ghttp.Request, err error) { func Error(r *ghttp.Request, err error) {
coder := errors.ParseCoder(err) coder := errors.ParseCoder(err)
...@@ -56,5 +54,3 @@ func SuccessWithData(r *ghttp.Request, data interface{}) { ...@@ -56,5 +54,3 @@ func SuccessWithData(r *ghttp.Request, data interface{}) {
Json(r, errors.CodeSuccess.Code(), errors.CodeSuccess.Message(), data) Json(r, errors.CodeSuccess.Code(), errors.CodeSuccess.Message(), data)
r.Exit() r.Exit()
} }
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"gitlab.jxhh.com/zhaoxueru/common-base.git/pkg/errors" "gitlab.jxhh.com/zhaoxueru/common-base.git/pkg/errors"
"testing" "testing"
) )
func init() { func init() {
errors.MustRegister(100001, errors.MustRegister(100001,
"success") "success")
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论