代码拉取完成,页面将自动刷新
同步操作将从 phachon/go-logger 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package go_logger
import (
"fmt"
"strconv"
"github.com/phachon/go-logger/utils"
"errors"
"reflect"
)
const API_ADAPTER_NAME = "api"
// adapter api
type AdapterApi struct {
config *ApiConfig
}
// api config
type ApiConfig struct {
// request url adddress
Url string
// request method
// GET, POST
Method string
// request headers
Headers map[string]string
// is verify response code
IsVerify bool
// verify response http code
VerifyCode int
}
func (ac *ApiConfig) Name() string {
return API_ADAPTER_NAME
}
func NewAdapterApi() LoggerAbstract {
return &AdapterApi{}
}
func (adapterApi *AdapterApi) Init(apiConfig Config) error {
if apiConfig.Name() != API_ADAPTER_NAME {
return errors.New("logger api adapter init error, config must ApiConfig")
}
vc := reflect.ValueOf(apiConfig)
ac := vc.Interface().(*ApiConfig)
adapterApi.config = ac
if adapterApi.config.Url == "" {
return errors.New("config Url cannot be empty!")
}
if adapterApi.config.Method != "GET" && adapterApi.config.Method != "POST" {
return errors.New("config Method must one of the 'GET', 'POST'!")
}
if adapterApi.config.IsVerify && (adapterApi.config.VerifyCode == 0) {
return errors.New("config if IsVerify is true, VerifyCode cannot be 0!")
}
return nil
}
func (adapterApi *AdapterApi) Write(loggerMsg *loggerMessage) error {
url := adapterApi.config.Url
method := adapterApi.config.Method
isVerify := adapterApi.config.IsVerify
verifyCode := adapterApi.config.VerifyCode
headers := adapterApi.config.Headers
loggerMap := map[string]string {
"timestamp": strconv.FormatInt(loggerMsg.Timestamp, 10),
"timestamp_format": loggerMsg.TimestampFormat,
"millisecond": strconv.FormatInt(loggerMsg.Millisecond, 10),
"millisecond_format": loggerMsg.MillisecondFormat,
"level": strconv.Itoa(loggerMsg.Level),
"level_string": loggerMsg.LevelString,
"body": loggerMsg.Body,
"file": loggerMsg.File,
"line": strconv.Itoa(loggerMsg.Line),
"function": loggerMsg.Function,
}
var err error
var code int
if method == "GET" {
_, code, err = utils.NewMisc().HttpGet(url, loggerMap, headers, 0)
}else {
_, code, err = utils.NewMisc().HttpPost(url, loggerMap, headers, 0)
}
if err != nil {
return err
}
if(isVerify && (code != verifyCode)) {
return fmt.Errorf("%s", "request "+ url +" faild, code=" + strconv.Itoa(code))
}
return nil
}
func (adapterApi *AdapterApi) Flush() {
}
func (adapterApi *AdapterApi)Name() string {
return API_ADAPTER_NAME
}
func init() {
Register(API_ADAPTER_NAME, NewAdapterApi)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。