This repository was archived by the owner on Jul 19, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathparams.go
More file actions
177 lines (164 loc) · 5.81 KB
/
Copy pathparams.go
File metadata and controls
177 lines (164 loc) · 5.81 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package logtic
import (
"fmt"
"reflect"
"sort"
"time"
)
// StringFromParameters return a key=value string for the given parameters. Depending on the type of the parameter
// value, it may be wrapped in single quotes. Byte slices are represented as hexadecimal strings. Parameters are always
// alphabetically sorted in the outputted string.
func StringFromParameters(parameters map[string]any) string {
if parameters == nil {
return ""
}
out := ""
last := len(parameters) - 1
i := 0
keys := make([]string, len(parameters))
for k := range parameters {
keys[i] = k
i++
}
sort.Strings(keys)
for i, k := range keys {
v := parameters[k]
t := reflect.TypeOf(v)
out += k + "="
if t == nil {
out += "nil"
} else {
switch t.Kind() {
case reflect.String:
if t.AssignableTo(reflect.TypeOf("")) {
out += "'" + v.(string) + "'"
} else {
out += fmt.Sprintf("'%v'", v)
}
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
out += fmt.Sprintf("%d", v)
case reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
out += fmt.Sprintf("%f", v)
case reflect.Slice, reflect.Array:
if b, isBytes := v.([]byte); isBytes {
out += fmt.Sprintf("%x", b)
} else {
out += fmt.Sprintf("'%v'", v)
}
case reflect.Struct:
if t, isTime := v.(time.Time); isTime {
out += "'" + t.Format(time.RFC3339) + "'"
} else {
out += fmt.Sprintf("'%v'", v)
}
default:
out += fmt.Sprintf("'%v'", v)
}
}
if i != last {
out += " "
}
}
return out
}
// FormatBytesB takes in a number of bytes and returns a human readable string with binary units (up-to Exbibyte)
func FormatBytesB(b uint64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}
// FormatBytesB takes in a number of bytes and returns a human readable string with decimal units (up-to Exabyte)
func FormatBytesD(b uint64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "KMGTPE"[exp])
}
// PDebug will log a debug parameterized message.
// Parameterized messages are formatted as key=value strings. Depending on the type of the parameter value, it may
// be wrapped in single quotes. Byte slices are represented as hexadecimal strings. Parameters are always alphabetically
// sorted in the outputted string.
func (s *Source) PDebug(event string, parameters map[string]any) {
s.Debug("%s: %s", event, StringFromParameters(parameters))
}
// PInfo will log an informational parameterized message.
// Parameterized messages are formatted as key=value strings. Depending on the type of the parameter value, it may
// be wrapped in single quotes. Byte slices are represented as hexadecimal strings. Parameters are always alphabetically
// sorted in the outputted string.
func (s *Source) PInfo(event string, parameters map[string]any) {
s.Info("%s: %s", event, StringFromParameters(parameters))
}
// PWarn will log a warning parameterized message.
// Parameterized messages are formatted as key=value strings. Depending on the type of the parameter value, it may
// be wrapped in single quotes. Byte slices are represented as hexadecimal strings. Parameters are always alphabetically
// sorted in the outputted string.
func (s *Source) PWarn(event string, parameters map[string]any) {
s.Warn("%s: %s", event, StringFromParameters(parameters))
}
// PError will log an error parameterized message. Errors are printed to stderr.
// Parameterized messages are formatted as key=value strings. Depending on the type of the parameter value, it may
// be wrapped in single quotes. Byte slices are represented as hexadecimal strings. Parameters are always alphabetically
// sorted in the outputted string.
func (s *Source) PError(event string, parameters map[string]any) {
s.Error("%s: %s", event, StringFromParameters(parameters))
}
// PFatal will log a fatal parameterized error message and exit the application with status 1.
// Fatal messages are printed to stderr.
// Parameterized messages are formatted as key=value strings. Depending on the type of the parameter value, it may
// be wrapped in single quotes. Byte slices are represented as hexadecimal strings. Parameters are always alphabetically
// sorted in the outputted string.
func (s *Source) PFatal(event string, parameters map[string]any) {
s.Fatal("%s: %s", event, StringFromParameters(parameters))
}
// PPanic functions like source.PFatal() but panics rather than exits.
// Parameterized messages are formatted as key=value strings. Depending on the type of the parameter value, it may
// be wrapped in single quotes. Byte slices are represented as hexadecimal strings. Parameters are always alphabetically
// sorted in the outputted string.
func (s *Source) PPanic(event string, parameters map[string]any) {
s.Panic("%s: %s", event, StringFromParameters(parameters))
}
// PWrite will call the matching write function for the given level, printing the provided message.
// For example:
//
// source.PWrite(logtic.LevelDebug, "My Event", map[string]any{"key": "value"})
//
// is the same as:
//
// source.PDebug("My Event", map[string]any{"key": "value"})
func (s *Source) PWrite(level LogLevel, event string, parameters map[string]any) {
defer panicRecover()
switch level {
case LevelDebug:
s.PDebug(event, parameters)
case LevelInfo:
s.PInfo(event, parameters)
case LevelWarn:
s.PWarn(event, parameters)
case LevelError:
s.PError(event, parameters)
default:
return
}
}