Skip to content

Commit

Permalink
链路增加ctx传递方便日志记录traceid,日志配置增加stdout选项,配置该选项为true时日志只从终端输出
Browse files Browse the repository at this point in the history
  • Loading branch information
linganmin authored and syd committed Dec 23, 2024
1 parent 7ab22fc commit 1bfa103
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
32 changes: 14 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package main
import (
"context"
"fmt"
"os"
"strconv"

fmt2 "github.com/ArtisanCloud/PowerLibs/v3/fmt"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/power"
Expand All @@ -14,8 +17,6 @@ import (
"github.com/ArtisanCloud/PowerWeChat/v3/src/work"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/trace"
"os"
"strconv"
)

func GetOfficialConfig() *officialAccount.UserConfig {
Expand All @@ -39,7 +40,6 @@ func GetOfficialConfig() *officialAccount.UserConfig {
HttpDebug: false,
Debug: false,
}

}

func GetWorkConfig() *work.UserConfig {
Expand Down Expand Up @@ -67,7 +67,7 @@ func GetWorkConfig() *work.UserConfig {
DB: 1,
}),

//HttpDebug: true,
// HttpDebug: true,
Debug: true,

// server config
Expand Down Expand Up @@ -113,15 +113,14 @@ func GetPaymentConfig() *payment.UserConfig {
//"sandbox": true,

// server config
//Token: os.Getenv("token"),
//AESKey: os.Getenv("aes_key"),
// Token: os.Getenv("token"),
// AESKey: os.Getenv("aes_key"),

}
}

func GetMiniProgramConfig() *miniProgram.UserConfig {
return &miniProgram.UserConfig{

AppID: os.Getenv("miniprogram_app_id"), // 小程序、公众号或者企业微信的appid
Secret: os.Getenv("miniprogram_secret"), // 商户号 appID

Expand All @@ -141,7 +140,6 @@ func GetMiniProgramConfig() *miniProgram.UserConfig {
//"sandbox": true,

}

}

func GetOpenPlatformConfig() *openPlatform.UserConfig {
Expand All @@ -161,11 +159,11 @@ func GetOpenPlatformConfig() *openPlatform.UserConfig {
Password: "",
DB: 1,
}),
//OAuth: "",
//HttpDebug: "",
//Debug: "",
//NotifyURL: "",
//Sandbox: "",
// OAuth: "",
// HttpDebug: "",
// Debug: "",
// NotifyURL: "",
// Sandbox: "",
}
}

Expand All @@ -180,7 +178,6 @@ func init() {
}

func main() {

fmt.Printf("hello Wechat! \n")

tracer := otel.Tracer("example-tracer")
Expand All @@ -193,9 +190,9 @@ func main() {
if err != nil {
fmt.Println(err.Error())
}
//officialAccountApp.Logger.Info("custom info log")
//officialAccountApp.Logger.Error("custom error log")
//officialAccountApp.Logger.Warn("custom warn log")
// officialAccountApp.Logger.Info("custom info log")
// officialAccountApp.Logger.Error("custom error log")
// officialAccountApp.Logger.Warn("custom warn log")

officialAccountApp.TemplateMessage.Send(ctx, &request.RequestTemlateMessage{
ToUser: "",
Expand Down Expand Up @@ -251,5 +248,4 @@ func main() {
fmt.Println(err.Error())
}
fmt2.Dump("openPlatform config:", openPlatform.GetConfig().All())

}
32 changes: 15 additions & 17 deletions src/kernel/accessToken.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (accessToken *AccessToken) requestToken(ctx context.Context, credentials *o
token.AuthorizerRefreshToken == "" &&
token.SuiteAccessToken == "" &&
token.ProviderAccessToken == "") {
return nil, errors.New(fmt.Sprintf("Request access_token fail: %v", res))
return nil, fmt.Errorf("Request access_token fail: %v", res)
}

return token, nil
Expand Down Expand Up @@ -247,26 +247,24 @@ func (accessToken *AccessToken) sendRequest(ctx context.Context, credential *obj
df := accessToken.HttpHelper.Df().WithContext(ctx).Uri(strEndpoint).
Method(accessToken.RequestMethod)

// 检查是否需要有请求参数配置
if options != nil {
// set query key values
if (*options)["query"] != nil {
queries := (*options)["query"].(*object.StringMap)
if queries != nil {
for k, v := range *queries {
df.Query(k, v)
}
// 检查是否需要有请求参数配置
// set query key values
if (*options)["query"] != nil {
queries := (*options)["query"].(*object.StringMap)
if queries != nil {
for k, v := range *queries {
df.Query(k, v)
}
}
}

// set body json
if (*options)["json"] != nil {
df.Json((*options)["json"])
}
//if (*options)["form_params"] != nil {
// df.Json((*options)["form_params"])
//}
// set body json
if (*options)["json"] != nil {
df.Json((*options)["json"])
}
//if (*options)["form_params"] != nil {
// df.Json((*options)["form_params"])
//}

rs, err := df.Request()
if err != nil {
Expand Down

0 comments on commit 1bfa103

Please sign in to comment.