Skip to content

Commit

Permalink
通过debug模式打开日志
Browse files Browse the repository at this point in the history
  • Loading branch information
paddy committed Mar 19, 2024
1 parent 1797041 commit b7b450b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
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{}

Check failure on line 9 in util/logger.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.16)

exported var `Logger` should have comment or be unexported (golint)

Check failure on line 9 in util/logger.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.17)

exported var `Logger` should have comment or be unexported (golint)

Check failure on line 9 in util/logger.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.18)

exported var `Logger` should have comment or be unexported (golint)

Check failure on line 9 in util/logger.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.19)

exported var `Logger` should have comment or be unexported (golint)

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

Check failure on line 29 in util/logger.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.16)

SA4005: ineffective assignment to field logger.debug (staticcheck)

Check failure on line 29 in util/logger.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.17)

SA4005: ineffective assignment to field logger.debug (staticcheck)

Check failure on line 29 in util/logger.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.18)

SA4005: ineffective assignment to field logger.debug (staticcheck)

Check failure on line 29 in util/logger.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.19)

SA4005: ineffective assignment to field logger.debug (staticcheck)
}

// 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

0 comments on commit b7b450b

Please sign in to comment.