Skip to content

通过debug模式打开日志 #773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 4 additions & 5 deletions officialaccount/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strconv"
"strings"

log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"

"github.com/silenceper/wechat/v2/officialaccount/context"
Expand Down Expand Up @@ -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("请求校验失败")
}

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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/xml"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"os"
Expand Down Expand Up @@ -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 {
Expand Down
44 changes: 44 additions & 0 deletions util/logger.go
Original file line number Diff line number Diff line change
@@ -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...)
}
}
15 changes: 0 additions & 15 deletions wechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down