diff --git a/client.go b/client.go index 5c92aed..0ee9ac0 100644 --- a/client.go +++ b/client.go @@ -1,16 +1,18 @@ package yar import ( + "github.com/flyhope/go-yar/comm" "github.com/flyhope/go-yar/pack" + "github.com/sirupsen/logrus" "io/ioutil" "net/http" "time" ) type Client struct { - Request *pack.Request - Response *pack.Response - Http *http.Request + Request *pack.Request + Response *pack.Response + Http *http.Request HttpClient *http.Client } @@ -23,9 +25,9 @@ func NewClient(addr string, method string, params interface{}) (*Client, error) httpRequest.Header.Set("User-Agent", "Go Yar Rpc-0.1") c := &Client{ - Request: pack.NewRequest(addr, method, params), - Response: new(pack.Response), - Http: httpRequest, + Request: pack.NewRequest(addr, method, params), + Response: new(pack.Response), + Http: httpRequest, HttpClient: &http.Client{Timeout: time.Second}, } @@ -51,10 +53,12 @@ func (c *Client) Send() error { buffer := header.Bytes() buffer.Write(data) - // 发送请求 c.Http.Body = ioutil.NopCloser(buffer) c.Http.Header.Set("Content-Type", packHandler.ContentType()) + comm.Log.WithFields(logrus.Fields{"YAR": "Request"}).Debug(string(data)) + + // 发送请求 resp, err := c.HttpClient.Do(c.Http) if err != nil { return err @@ -67,10 +71,16 @@ func (c *Client) Send() error { packHandler = pack.GetPackHandler(headerData.Packager) bodyContent := body[pack.ProtocolLength+pack.PackagerLength:] err = packHandler.Decode(bodyContent, c.Response) + + if c.Response.Except != nil { + comm.Log.WithFields(logrus.Fields{"YAR": "Except"}).Debug(c.Response.Except) + } + if err != nil { return err } + comm.Log.WithFields(logrus.Fields{"YAR": "RetVal"}).Debug(c.Response.Retval) if c.Response.Except != nil { return c.Response.Except } diff --git a/comm/log.go b/comm/log.go new file mode 100644 index 0000000..b6114a5 --- /dev/null +++ b/comm/log.go @@ -0,0 +1,18 @@ +package comm + +import "github.com/sirupsen/logrus" + +var Log = logrus.New() + +func init() { + // 配置日志 + var formatter logrus.Formatter + formatter = &logrus.TextFormatter{ + ForceColors: true , + FullTimestamp: true, + TimestampFormat: "2006-01-02 15:04:05", + } + Log.SetFormatter(formatter) + Log.SetLevel(logrus.WarnLevel) +} + diff --git a/go.mod b/go.mod index 05109a1..11a8b10 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,10 @@ module github.com/flyhope/go-yar go 1.13 -require github.com/vmihailenco/msgpack v4.0.4+incompatible +require ( + github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect + github.com/sirupsen/logrus v1.6.0 + github.com/vmihailenco/msgpack v4.0.4+incompatible + google.golang.org/appengine v1.6.6 // indirect + gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect +) diff --git a/go.sum b/go.sum index da143fb..6cecc75 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,32 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= +github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= +github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= -github.com/weixinhost/yar.go v0.0.0-20170116030201-896d9cac4c34 h1:BKXFn0Yu1pWkE1hiKfCv90r0xHXk6twFHct7zAPj4wY= -github.com/weixinhost/yar.go v0.0.0-20170116030201-896d9cac4c34/go.mod h1:lyxsw/abS0I218efOb3WHPhcFwGCDemTEQ8LksWDtn8= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65 h1:+rhAzEzT3f4JtomfC371qB+0Ola2caSKcY69NUBZrRQ= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +google.golang.org/appengine v1.6.6 h1:lMO5rYAqUxkmaj76jAkRUvt5JZgFymx/+Q5Mzfivuhc= +google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/pack/response.go b/pack/response.go index 8e46339..e06ff11 100644 --- a/pack/response.go +++ b/pack/response.go @@ -22,7 +22,7 @@ const ( // 基础异常结构体 type exceptionDefine struct { Type string `json:"_type"` - Code int32 `json:"code"` + Code int32 `json:"code"` File string `json:"file"` Line uint `json:"line"` Message string `json:"message"` @@ -41,8 +41,8 @@ func (e *Exception) GetCode() int32 { return e.Code } -func (e *Exception) GetMeta() (result map[string] interface{}) { - result = make(map[string] interface{}) +func (e *Exception) GetMeta() (result map[string]interface{}) { + result = make(map[string]interface{}) result["Type"] = e.Type result["File"] = e.File result["Line"] = e.Line diff --git a/set.go b/set.go new file mode 100644 index 0000000..b43789f --- /dev/null +++ b/set.go @@ -0,0 +1,22 @@ +package yar + +import ( + "github.com/flyhope/go-yar/comm" + "github.com/sirupsen/logrus" +) + +type Level logrus.Level + +const ( + LevelTrace Level = Level(logrus.TraceLevel) + LevelDebug Level = Level(logrus.DebugLevel) + LevelInfo Level = Level(logrus.InfoLevel) + LevelWarning Level = Level(logrus.WarnLevel) + LevelFatal Level = Level(logrus.FatalLevel) + LevelPanic Level = Level(logrus.PanicLevel) +) + +// 设置日志输出级别 +func SetLevel(level Level) { + comm.Log.SetLevel(logrus.Level(level)) +}