github.com/go-spring/spring-base@v1.1.3/log/field.go (about) 1 /* 2 * Copyright 2012-2019 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package log 18 19 // Field is used to replace printf operation with lower cost. 20 type Field struct { 21 Key string 22 Val Value 23 } 24 25 // Nil constructs a field that carries a nil. 26 func Nil(key string) Field { 27 return Reflect(key, nil) 28 } 29 30 // Bool constructs a field that carries a bool. 31 func Bool(key string, val bool) Field { 32 return Field{Key: key, Val: BoolValue(val)} 33 } 34 35 // BoolPtr constructs a field that carries a *bool. 36 func BoolPtr(key string, val *bool) Field { 37 if val == nil { 38 return Nil(key) 39 } 40 return Bool(key, *val) 41 } 42 43 // Int constructs a field that carries an int. 44 func Int(key string, val int) Field { 45 return Field{Key: key, Val: Int64Value(val)} 46 } 47 48 // IntPtr constructs a field that carries a *int. 49 func IntPtr(key string, val *int) Field { 50 if val == nil { 51 return Nil(key) 52 } 53 return Int(key, *val) 54 } 55 56 // Int8 constructs a field that carries an int8. 57 func Int8(key string, val int8) Field { 58 return Field{Key: key, Val: Int64Value(val)} 59 } 60 61 // Int8Ptr constructs a field that carries a *int8. 62 func Int8Ptr(key string, val *int8) Field { 63 if val == nil { 64 return Nil(key) 65 } 66 return Int8(key, *val) 67 } 68 69 // Int16 constructs a field that carries an int16. 70 func Int16(key string, val int16) Field { 71 return Field{Key: key, Val: Int64Value(val)} 72 } 73 74 // Int16Ptr constructs a field that carries a *int16. 75 func Int16Ptr(key string, val *int16) Field { 76 if val == nil { 77 return Nil(key) 78 } 79 return Int16(key, *val) 80 } 81 82 // Int32 constructs a field that carries an int32. 83 func Int32(key string, val int32) Field { 84 return Field{Key: key, Val: Int64Value(val)} 85 } 86 87 // Int32Ptr constructs a field that carries a *int32. 88 func Int32Ptr(key string, val *int32) Field { 89 if val == nil { 90 return Nil(key) 91 } 92 return Int32(key, *val) 93 } 94 95 // Int64 constructs a field that carries an int64. 96 func Int64(key string, val int64) Field { 97 return Field{Key: key, Val: Int64Value(val)} 98 } 99 100 // Int64Ptr constructs a field that carries a *int64. 101 func Int64Ptr(key string, val *int64) Field { 102 if val == nil { 103 return Nil(key) 104 } 105 return Int64(key, *val) 106 } 107 108 // Uint constructs a field that carries an uint. 109 func Uint(key string, val uint) Field { 110 return Field{Key: key, Val: Uint64Value(val)} 111 } 112 113 // UintPtr constructs a field that carries a *uint. 114 func UintPtr(key string, val *uint) Field { 115 if val == nil { 116 return Nil(key) 117 } 118 return Uint(key, *val) 119 } 120 121 // Uint8 constructs a field that carries an uint8. 122 func Uint8(key string, val uint8) Field { 123 return Field{Key: key, Val: Uint64Value(val)} 124 } 125 126 // Uint8Ptr constructs a field that carries a *uint8. 127 func Uint8Ptr(key string, val *uint8) Field { 128 if val == nil { 129 return Nil(key) 130 } 131 return Uint8(key, *val) 132 } 133 134 // Uint16 constructs a field that carries an uint16. 135 func Uint16(key string, val uint16) Field { 136 return Field{Key: key, Val: Uint64Value(val)} 137 } 138 139 // Uint16Ptr constructs a field that carries a *uint16. 140 func Uint16Ptr(key string, val *uint16) Field { 141 if val == nil { 142 return Nil(key) 143 } 144 return Uint16(key, *val) 145 } 146 147 // Uint32 constructs a field that carries an uint32. 148 func Uint32(key string, val uint32) Field { 149 return Field{Key: key, Val: Uint64Value(val)} 150 } 151 152 // Uint32Ptr constructs a field that carries a *uint32. 153 func Uint32Ptr(key string, val *uint32) Field { 154 if val == nil { 155 return Nil(key) 156 } 157 return Uint32(key, *val) 158 } 159 160 // Uint64 constructs a field that carries an uint64. 161 func Uint64(key string, val uint64) Field { 162 return Field{Key: key, Val: Uint64Value(val)} 163 } 164 165 // Uint64Ptr constructs a field that carries a *uint64. 166 func Uint64Ptr(key string, val *uint64) Field { 167 if val == nil { 168 return Nil(key) 169 } 170 return Uint64(key, *val) 171 } 172 173 // Float32 constructs a field that carries a float32. 174 func Float32(key string, val float32) Field { 175 return Field{Key: key, Val: Float64Value(val)} 176 } 177 178 // Float32Ptr constructs a field that carries a *float32. 179 func Float32Ptr(key string, val *float32) Field { 180 if val == nil { 181 return Nil(key) 182 } 183 return Float32(key, *val) 184 } 185 186 // Float64 constructs a field that carries a float64. 187 func Float64(key string, val float64) Field { 188 return Field{Key: key, Val: Float64Value(val)} 189 } 190 191 // Float64Ptr constructs a field that carries a *float64. 192 func Float64Ptr(key string, val *float64) Field { 193 if val == nil { 194 return Nil(key) 195 } 196 return Float64(key, *val) 197 } 198 199 // String constructs a field that carries a string. 200 func String(key string, val string) Field { 201 return Field{Key: key, Val: StringValue(val)} 202 } 203 204 // StringPtr constructs a field that carries a *string. 205 func StringPtr(key string, val *string) Field { 206 if val == nil { 207 return Nil(key) 208 } 209 return String(key, *val) 210 } 211 212 // Reflect constructs a field that carries an interface{}, 213 // which should be serialized using reflection. 214 func Reflect(key string, val interface{}) Field { 215 return Field{Key: key, Val: ReflectValue{Val: val}} 216 } 217 218 // Array constructs a field that carries a slice of Value. 219 func Array(key string, val ...Value) Field { 220 return Field{Key: key, Val: ArrayValue(val)} 221 } 222 223 // Object constructs a field that carries a slice of Field. 224 func Object(key string, fields ...Field) Field { 225 return Field{Key: key, Val: ObjectValue(fields)} 226 } 227 228 // Bools constructs a field that carries a slice of bool. 229 func Bools(key string, val []bool) Field { 230 return Field{Key: key, Val: BoolsValue(val)} 231 } 232 233 // Ints constructs a field that carries a slice of int. 234 func Ints(key string, val []int) Field { 235 return Field{Key: key, Val: IntsValue(val)} 236 } 237 238 // Int8s constructs a field that carries a slice of int8. 239 func Int8s(key string, val []int8) Field { 240 return Field{Key: key, Val: Int8sValue(val)} 241 } 242 243 // Int16s constructs a field that carries a slice of int16. 244 func Int16s(key string, val []int16) Field { 245 return Field{Key: key, Val: Int16sValue(val)} 246 } 247 248 // Int32s constructs a field that carries a slice of int32. 249 func Int32s(key string, val []int32) Field { 250 return Field{Key: key, Val: Int32sValue(val)} 251 } 252 253 // Int64s constructs a field that carries a slice of int64. 254 func Int64s(key string, val []int64) Field { 255 return Field{Key: key, Val: Int64sValue(val)} 256 } 257 258 // Uints constructs a field that carries a slice of uint. 259 func Uints(key string, val []uint) Field { 260 return Field{Key: key, Val: UintsValue(val)} 261 } 262 263 // Uint8s constructs a field that carries a slice of uint8. 264 func Uint8s(key string, val []uint8) Field { 265 return Field{Key: key, Val: Uint8sValue(val)} 266 } 267 268 // Uint16s constructs a field that carries a slice of uint16. 269 func Uint16s(key string, val []uint16) Field { 270 return Field{Key: key, Val: Uint16sValue(val)} 271 } 272 273 // Uint32s constructs a field that carries a slice of uint32. 274 func Uint32s(key string, val []uint32) Field { 275 return Field{Key: key, Val: Uint32sValue(val)} 276 } 277 278 // Uint64s constructs a field that carries a slice of uint64. 279 func Uint64s(key string, val []uint64) Field { 280 return Field{Key: key, Val: Uint64sValue(val)} 281 } 282 283 // Float32s constructs a field that carries a slice of float32. 284 func Float32s(key string, val []float32) Field { 285 return Field{Key: key, Val: Float32sValue(val)} 286 } 287 288 // Float64s constructs a field that carries a slice of float64. 289 func Float64s(key string, val []float64) Field { 290 return Field{Key: key, Val: Float64sValue(val)} 291 } 292 293 // Strings constructs a field that carries a slice of string. 294 func Strings(key string, val []string) Field { 295 return Field{Key: key, Val: StringsValue(val)} 296 } 297 298 // Any takes a key and an arbitrary value and chooses the best way 299 // to represent them as a field, falling back to a reflection-based 300 // approach only if necessary. 301 func Any(key string, value interface{}) Field { 302 switch val := value.(type) { 303 case nil: 304 return Nil(key) 305 case bool: 306 return Bool(key, val) 307 case *bool: 308 return BoolPtr(key, val) 309 case []bool: 310 return Bools(key, val) 311 case int: 312 return Int(key, val) 313 case *int: 314 return IntPtr(key, val) 315 case []int: 316 return Ints(key, val) 317 case int8: 318 return Int8(key, val) 319 case *int8: 320 return Int8Ptr(key, val) 321 case []int8: 322 return Int8s(key, val) 323 case int16: 324 return Int16(key, val) 325 case *int16: 326 return Int16Ptr(key, val) 327 case []int16: 328 return Int16s(key, val) 329 case int32: 330 return Int32(key, val) 331 case *int32: 332 return Int32Ptr(key, val) 333 case []int32: 334 return Int32s(key, val) 335 case int64: 336 return Int64(key, val) 337 case *int64: 338 return Int64Ptr(key, val) 339 case []int64: 340 return Int64s(key, val) 341 case uint: 342 return Uint(key, val) 343 case *uint: 344 return UintPtr(key, val) 345 case []uint: 346 return Uints(key, val) 347 case uint8: 348 return Uint8(key, val) 349 case *uint8: 350 return Uint8Ptr(key, val) 351 case []uint8: 352 return Uint8s(key, val) 353 case uint16: 354 return Uint16(key, val) 355 case *uint16: 356 return Uint16Ptr(key, val) 357 case []uint16: 358 return Uint16s(key, val) 359 case uint32: 360 return Uint32(key, val) 361 case *uint32: 362 return Uint32Ptr(key, val) 363 case []uint32: 364 return Uint32s(key, val) 365 case uint64: 366 return Uint64(key, val) 367 case *uint64: 368 return Uint64Ptr(key, val) 369 case []uint64: 370 return Uint64s(key, val) 371 case float32: 372 return Float32(key, val) 373 case *float32: 374 return Float32Ptr(key, val) 375 case []float32: 376 return Float32s(key, val) 377 case float64: 378 return Float64(key, val) 379 case *float64: 380 return Float64Ptr(key, val) 381 case []float64: 382 return Float64s(key, val) 383 case string: 384 return String(key, val) 385 case *string: 386 return StringPtr(key, val) 387 case []string: 388 return Strings(key, val) 389 default: 390 return Reflect(key, val) 391 } 392 }