chore(log): let log toString same (#2414)

pull/2428/head
180909 2 years ago committed by GitHub
parent ff59a89b06
commit 9301bfa407
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      contrib/log/aliyun/aliyun.go
  2. 1
      contrib/log/aliyun/aliyun_test.go
  3. 8
      contrib/log/tencent/tencent.go

@ -2,6 +2,7 @@ package aliyun
import ( import (
"encoding/json" "encoding/json"
"fmt"
"strconv" "strconv"
"time" "time"
@ -164,6 +165,8 @@ func toString(v interface{}) string {
key = strconv.FormatBool(v) key = strconv.FormatBool(v)
case []byte: case []byte:
key = string(v) key = string(v)
case fmt.Stringer:
key = v.String()
default: default:
newValue, _ := json.Marshal(v) newValue, _ := json.Marshal(v)
key = string(newValue) key = string(newValue)

@ -121,6 +121,7 @@ func TestToString(t *testing.T) {
{math.MaxInt64, "9223372036854775807"}, {math.MaxInt64, "9223372036854775807"},
{uint64(math.MaxUint64), "18446744073709551615"}, {uint64(math.MaxUint64), "18446744073709551615"},
{"abc", "abc"}, {"abc", "abc"},
{false, "false"},
{[]byte("abc"), "abc"}, {[]byte("abc"), "abc"},
} }
for _, test := range tests { for _, test := range tests {

@ -115,7 +115,7 @@ func NewLogger(options ...Option) (Logger, error) {
}, nil }, nil
} }
// toString any type to string // toString convert any type to string
func toString(v interface{}) string { func toString(v interface{}) string {
var key string var key string
if v == nil { if v == nil {
@ -146,6 +146,12 @@ func toString(v interface{}) string {
key = strconv.FormatInt(v, 10) key = strconv.FormatInt(v, 10)
case uint64: case uint64:
key = strconv.FormatUint(v, 10) key = strconv.FormatUint(v, 10)
case string:
key = v
case bool:
key = strconv.FormatBool(v)
case []byte:
key = string(v)
case fmt.Stringer: case fmt.Stringer:
key = v.String() key = v.String()
default: default:

Loading…
Cancel
Save