diff --git a/README.md b/README.md index c3b3a4cba..1d4ce8f77 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ import "github.com/silenceper/wechat/v2" ```go // 使用memcache保存access_token,也可选择redis或自定义cache wc := wechat.NewWechat() +// 调试模式 +wc.SetDebug(true) memory := cache.NewMemory() cfg := &offConfig.Config{ AppID: "xxx", diff --git a/officialaccount/server/server.go b/officialaccount/server/server.go index b0547dfdb..70c24dace 100644 --- a/officialaccount/server/server.go +++ b/officialaccount/server/server.go @@ -12,7 +12,6 @@ import ( "strconv" "strings" - log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" "github.com/silenceper/wechat/v2/officialaccount/context" @@ -59,7 +58,7 @@ func (srv *Server) SkipValidate(skip bool) { // Serve 处理微信的请求消息 func (srv *Server) Serve() error { if !srv.Validate() { - log.Error("Validate Signature Failed.") + util.Logger.Error("Validate Signature Failed.") return fmt.Errorf("请求校验失败") } @@ -80,7 +79,7 @@ func (srv *Server) Serve() error { } // debug print request msg - log.Debugf("request msg =%s", string(srv.RequestRawXMLMsg)) + util.Logger.Debugf("request msg =%s", string(srv.RequestRawXMLMsg)) return srv.buildResponse(response) } @@ -93,7 +92,7 @@ func (srv *Server) Validate() bool { timestamp := srv.Query("timestamp") nonce := srv.Query("nonce") signature := srv.Query("signature") - log.Debugf("validate signature, timestamp=%s, nonce=%s", timestamp, nonce) + util.Logger.Debugf("validate signature, timestamp=%s, nonce=%s", timestamp, nonce) return signature == util.Signature(srv.Token, timestamp, nonce) } @@ -276,7 +275,7 @@ func (srv *Server) buildResponse(reply *message.Reply) (err error) { // Send 将自定义的消息发送 func (srv *Server) Send() (err error) { replyMsg := srv.ResponseMsg - log.Debugf("response msg =%+v", replyMsg) + util.Logger.Debugf("response msg =%+v", replyMsg) if srv.isSafeMode { // 安全模式下对消息进行加密 var encryptedMsg []byte diff --git a/util/http.go b/util/http.go index 26e4a7be4..904b6e52d 100644 --- a/util/http.go +++ b/util/http.go @@ -9,7 +9,6 @@ import ( "encoding/xml" "fmt" "io" - "log" "mime/multipart" "net/http" "os" @@ -267,7 +266,7 @@ func pkcs12ToPem(p12 []byte, password string) tls.Certificate { blocks, err := pkcs12.ToPEM(p12, password) defer func() { if x := recover(); x != nil { - log.Print(x) + Logger.Error(x) } }() if err != nil { diff --git a/util/logger.go b/util/logger.go new file mode 100644 index 000000000..77afab04a --- /dev/null +++ b/util/logger.go @@ -0,0 +1,44 @@ +package util + +import ( + "os" + + log "github.com/sirupsen/logrus" +) + +var Logger = logger{} + +type logger struct { + debug bool +} + +func init() { + // Log as JSON instead of the default ASCII formatter. + log.SetFormatter(&log.TextFormatter{}) + + // Output to stdout instead of the default stderr + // Can be any io.Writer, see below for File example + log.SetOutput(os.Stdout) + + // Only log the warning severity or above. + log.SetLevel(log.DebugLevel) +} + +// SetDebug sets the debug flag +func (r logger) SetDebug(isDebug bool) { + r.debug = isDebug +} + +// Debugf logs a message at level Debug on the standard logger. +func (r logger) Debugf(format string, args ...interface{}) { + if r.debug { + log.Debugf(format, args...) + } +} + +// Error logs a message at level Error on the standard logger. +func (r logger) Error(args ...interface{}) { + if r.debug { + log.Error(args...) + } +} diff --git a/wechat.go b/wechat.go index dc2585fea..ca923119e 100644 --- a/wechat.go +++ b/wechat.go @@ -2,9 +2,6 @@ package wechat import ( "net/http" - "os" - - log "github.com/sirupsen/logrus" "github.com/silenceper/wechat/v2/cache" "github.com/silenceper/wechat/v2/miniprogram" @@ -20,18 +17,6 @@ import ( workConfig "github.com/silenceper/wechat/v2/work/config" ) -func init() { - // Log as JSON instead of the default ASCII formatter. - log.SetFormatter(&log.TextFormatter{}) - - // Output to stdout instead of the default stderr - // Can be any io.Writer, see below for File example - log.SetOutput(os.Stdout) - - // Only log the warning severity or above. - log.SetLevel(log.DebugLevel) -} - // Wechat struct type Wechat struct { cache cache.Cache