支持日志打印
This commit is contained in:
@@ -168,6 +168,7 @@ func (c *Curlx) sendExec(ctx context.Context, ps ...Param) (req *http.Request, r
|
|||||||
for _, param := range ps {
|
for _, param := range ps {
|
||||||
param(&p)
|
param(&p)
|
||||||
}
|
}
|
||||||
|
c.opts.Logger.Infof(ctx,"curlx.sendExec params:%+v", p)
|
||||||
|
|
||||||
err = p.parseMethod()
|
err = p.parseMethod()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -177,12 +178,14 @@ func (c *Curlx) sendExec(ctx context.Context, ps ...Param) (req *http.Request, r
|
|||||||
// 判断和处理url
|
// 判断和处理url
|
||||||
err = p.parseUrl()
|
err = p.parseUrl()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.opts.Logger.Errorf(ctx,"curlx.sendExec parseUrl err:%v", err)
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理参数
|
// 处理参数
|
||||||
reqParams, err := p.parseParams()
|
reqParams, err := p.parseParams()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.opts.Logger.Errorf(ctx,"curlx.sendExec parseParams err:%v", err)
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,6 +196,7 @@ func (c *Curlx) sendExec(ctx context.Context, ps ...Param) (req *http.Request, r
|
|||||||
reqParams,
|
reqParams,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.opts.Logger.Errorf(ctx,"curlx.sendExec NewRequest err:%v", err)
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,6 +215,7 @@ func (c *Curlx) sendExec(ctx context.Context, ps ...Param) (req *http.Request, r
|
|||||||
// 发起请求
|
// 发起请求
|
||||||
response, err := client.Do(request)
|
response, err := client.Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.opts.Logger.Errorf(ctx,"curlx.sendExec client.Do err:%v", err)
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
// response.StatusCode
|
// response.StatusCode
|
||||||
|
|||||||
+33
-1
@@ -1,15 +1,21 @@
|
|||||||
package curlx
|
package curlx
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type clientOptions struct {
|
type clientOptions struct {
|
||||||
TimeOut time.Duration
|
TimeOut time.Duration
|
||||||
InsecureSkipVerify bool
|
InsecureSkipVerify bool
|
||||||
|
Logger OptionLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultOptions() clientOptions {
|
func defaultOptions() clientOptions {
|
||||||
return clientOptions{
|
return clientOptions{
|
||||||
TimeOut: time.Second * 120, // 默认超时120
|
TimeOut: time.Second * 120, // 默认超时120
|
||||||
|
Logger: defaultLogger{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,3 +38,29 @@ func SetOptionTLSInsecureSkipVerify() Option {
|
|||||||
options.InsecureSkipVerify = true
|
options.InsecureSkipVerify = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置日志输出
|
||||||
|
*/
|
||||||
|
func SetOptionLog(log OptionLogger) Option {
|
||||||
|
return func(options *clientOptions) {
|
||||||
|
options.Logger = log
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type OptionLogger interface {
|
||||||
|
Infof(ctx context.Context, format string, args ...interface{})
|
||||||
|
Errorf(ctx context.Context, format string, args ...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type defaultLogger struct{}
|
||||||
|
|
||||||
|
func (defaultLogger) Errorf(ctx context.Context, format string, args ...interface{}) {
|
||||||
|
// 输出日志
|
||||||
|
log.Printf(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (defaultLogger) Infof(ctx context.Context, format string, args ...interface{}) {
|
||||||
|
// 输出日志
|
||||||
|
log.Printf(format, args...)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user