1 Star 0 Fork 13

skymysky/go-logger

forked from phachon/go-logger 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
api.go 2.50 KB
一键复制 编辑 原始数据 按行查看 历史
phachon 提交于 2018-04-23 12:26 . update config call
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)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/skymysky/go-logger.git
[email protected]:skymysky/go-logger.git
skymysky
go-logger
go-logger
master

搜索帮助