-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.go
101 lines (87 loc) · 2.89 KB
/
field.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright 2019 Metaleaf.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package log
import (
"reflect"
"strconv"
)
// Field stores a name/value pair to be formatted by the emitter.
type Field struct {
Name string
Type reflect.Kind
BoolValue bool
IntValue int64
StringValue string
}
// Bool returns a Field that contains a boolean value.
func Bool(name string, value bool) Field {
return Field{Name:name, Type:reflect.Bool, BoolValue:value}
}
// Int returns a Field that contains a default integer.
func Int(name string, value int) Field {
return Field{Name:name, Type:reflect.Int, IntValue:int64(value)}
}
// Int8 returns a Field that contains an 8-bit integer
func Int8(name string, value int8) Field {
return Field{Name:name, Type:reflect.Int8, IntValue:int64(value)}
}
// Int16 returns a Field that contains an 16-bit integer
func Int16(name string, value int16) Field {
return Field{Name:name, Type:reflect.Int16, IntValue:int64(value)}
}
// Int32 returns a Field that contains an 32-bit integer
func Int32(name string, value int32) Field {
return Field{Name:name, Type:reflect.Int32, IntValue:int64(value)}
}
// Int64 returns a Field that contains an 64-bit integer
func Int64(name string, value int64) Field {
return Field{Name:name, Type:reflect.Int64, IntValue:value}
}
// String returns a Field that contains a string.
func String(name string, value string) Field {
return Field{Name:name, Type:reflect.String, StringValue:value}
}
// Err returns a Field that contains the message from an error.
func Err(value error) Field {
return Field{Name:"error", Type:reflect.String, StringValue:value.Error()}
}
// Json returns the contents of the Field as `"key":value`.
func (field Field) Json() string {
return "\"" + field.Name + "\":" + stringValue(field, true)
}
// String returns the contents of the Field as `key=value`.
func (field Field) String() string {
return field.Name + "=" + stringValue(field, false)
}
// stringValue converts the field's value into a string.
func stringValue(field Field, quoted bool) string {
switch field.Type {
case reflect.Bool:
if field.BoolValue {
return "true"
}
return "false"
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(field.IntValue, 10)
case reflect.String:
if quoted {
return "\"" + field.StringValue + "\""
}
return field.StringValue
default:
panic("Invalid type")
}
}