Skip to content

Commit 6415450

Browse files
authored
Merge pull request #2 from alexsn/zap-types
Add support for zap.Stringer and zap.Duration types
2 parents 65aa7fc + 113b907 commit 6415450

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

utils/fields.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package utils
22

33
import (
4+
"fmt"
45
"math"
6+
"time"
57

68
opentracinglog "github.com/opentracing/opentracing-go/log"
79
"go.uber.org/zap/zapcore"
@@ -40,10 +42,14 @@ func ZapFieldToOpentracing(zapField zapcore.Field) opentracinglog.Field {
4042
return opentracinglog.Int32(zapField.Key, int32(zapField.Integer))
4143
case zapcore.StringType:
4244
return opentracinglog.String(zapField.Key, zapField.String)
45+
case zapcore.StringerType:
46+
return opentracinglog.String(zapField.Key, zapField.Interface.(fmt.Stringer).String())
4347
case zapcore.Uint64Type:
4448
return opentracinglog.Uint64(zapField.Key, uint64(zapField.Integer))
4549
case zapcore.Uint32Type:
4650
return opentracinglog.Uint32(zapField.Key, uint32(zapField.Integer))
51+
case zapcore.DurationType:
52+
return opentracinglog.String(zapField.Key, time.Duration(zapField.Integer).String())
4753
case zapcore.ErrorType:
4854
return opentracinglog.Error(zapField.Interface.(error))
4955
default:

utils/fields_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ package utils
22

33
import (
44
"testing"
5+
"time"
56

67
opentracinglog "github.com/opentracing/opentracing-go/log"
78
"go.uber.org/zap"
89
"go.uber.org/zap/zapcore"
910
)
1011

12+
type stringer struct {
13+
string
14+
}
15+
16+
func (s stringer) String() string {
17+
return s.string
18+
}
19+
1120
func TestFieldsConversion(t *testing.T) {
1221

1322
TestData := []struct {
@@ -26,6 +35,14 @@ func TestFieldsConversion(t *testing.T) {
2635
zap.String("", "123"),
2736
opentracinglog.String("", "123"),
2837
},
38+
{
39+
zap.Stringer("namespace", stringer{""}),
40+
opentracinglog.String("namespace", ""),
41+
},
42+
{
43+
zap.Stringer("", stringer{"123"}),
44+
opentracinglog.String("", "123"),
45+
},
2946
{
3047
zap.Int("namespace", 1),
3148
opentracinglog.Int64("namespace", 1),
@@ -46,6 +63,10 @@ func TestFieldsConversion(t *testing.T) {
4663
zap.Uint64("namespace", 1),
4764
opentracinglog.Uint64("namespace", 1),
4865
},
66+
{
67+
zap.Duration("namespace", time.Second),
68+
opentracinglog.String("namespace", "1s"),
69+
},
4970
{
5071
zap.Float32("namespace", 1),
5172
opentracinglog.Float32("namespace", 1),

0 commit comments

Comments
 (0)