go.temporal.io/server@v1.23.0/common/log/tag/zap_tag.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package tag 26 27 import ( 28 "time" 29 30 "go.uber.org/zap" 31 "go.uber.org/zap/zapcore" 32 "google.golang.org/protobuf/types/known/durationpb" 33 "google.golang.org/protobuf/types/known/timestamppb" 34 ) 35 36 type ( 37 // ZapTag is the wrapper over zap.Field. 38 ZapTag struct { 39 // keep this field private 40 field zap.Field 41 } 42 ) 43 44 // NewZapTag creates new ZapTag from zap.Field. 45 func NewZapTag(field zap.Field) ZapTag { 46 return ZapTag{ 47 field: field, 48 } 49 } 50 51 func (t ZapTag) Field() zap.Field { 52 return t.field 53 } 54 55 func (t ZapTag) Key() string { 56 return t.field.Key 57 } 58 59 func (t ZapTag) Value() interface{} { 60 // Not for production use. 61 enc := zapcore.NewMapObjectEncoder() 62 t.field.AddTo(enc) 63 for _, val := range enc.Fields { 64 return val 65 } 66 return nil 67 } 68 69 func NewBinaryTag(key string, value []byte) ZapTag { 70 return ZapTag{ 71 field: zap.Binary(key, value), 72 } 73 } 74 75 func NewStringTag(key string, value string) ZapTag { 76 return ZapTag{ 77 field: zap.String(key, value), 78 } 79 } 80 81 func NewStringsTag(key string, value []string) ZapTag { 82 return ZapTag{ 83 field: zap.Strings(key, value), 84 } 85 } 86 87 func NewInt64(key string, value int64) ZapTag { 88 return ZapTag{ 89 field: zap.Int64(key, value), 90 } 91 } 92 93 func NewInt(key string, value int) ZapTag { 94 return ZapTag{ 95 field: zap.Int(key, value), 96 } 97 } 98 99 func NewInt32(key string, value int32) ZapTag { 100 return ZapTag{ 101 field: zap.Int32(key, value), 102 } 103 } 104 105 func NewFloat64(key string, value float64) ZapTag { 106 return ZapTag{ 107 field: zap.Float64(key, value), 108 } 109 } 110 111 func NewBoolTag(key string, value bool) ZapTag { 112 return ZapTag{ 113 field: zap.Bool(key, value), 114 } 115 } 116 117 func NewErrorTag(value error) ZapTag { 118 // NOTE: zap already chosen "error" as key 119 return ZapTag{ 120 field: zap.Error(value), 121 } 122 } 123 124 func NewDurationTag(key string, value time.Duration) ZapTag { 125 return ZapTag{ 126 field: zap.Duration(key, value), 127 } 128 } 129 130 func NewDurationPtrTag(key string, value *durationpb.Duration) ZapTag { 131 return ZapTag{ 132 field: zap.Duration(key, value.AsDuration()), 133 } 134 } 135 136 func NewTimeTag(key string, value time.Time) ZapTag { 137 return ZapTag{ 138 field: zap.Time(key, value), 139 } 140 } 141 142 func NewTimePtrTag(key string, value *timestamppb.Timestamp) ZapTag { 143 return ZapTag{ 144 field: zap.Time(key, value.AsTime()), 145 } 146 } 147 148 func NewAnyTag(key string, value interface{}) ZapTag { 149 return ZapTag{ 150 field: zap.Any(key, value), 151 } 152 }