Skip to content

Commit

Permalink
add debug option
Browse files Browse the repository at this point in the history
  • Loading branch information
flyhope committed Jun 4, 2020
1 parent 9efca00 commit 5811998
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 13 deletions.
24 changes: 17 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
@@ -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
}

Expand All @@ -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},
}

Expand All @@ -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
Expand All @@ -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
}
Expand Down
18 changes: 18 additions & 0 deletions comm/log.go
Original file line number Diff line number Diff line change
@@ -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)
}

8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
32 changes: 30 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
6 changes: 3 additions & 3 deletions pack/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions set.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 5811998

Please sign in to comment.