-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlogging_params.go
54 lines (41 loc) · 1.39 KB
/
logging_params.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
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package middleware
import (
"time"
"github.com/ssgreg/logf"
"github.com/acronis/go-appkit/log"
)
type loggableIntMap map[string]int64
func (lm loggableIntMap) EncodeLogfObject(e logf.FieldEncoder) error {
for key, value := range lm {
e.EncodeFieldInt64(key, value)
}
return nil
}
// LoggingParams stores parameters for the Logging middleware
// that may be modified dynamically by the other underlying middlewares/handlers.
type LoggingParams struct {
fields []log.Field
timeSlots loggableIntMap
}
// ExtendFields extends list of fields that will be logged by the Logging middleware.
func (lp *LoggingParams) ExtendFields(fields ...log.Field) {
lp.fields = append(lp.fields, fields...)
}
// AddTimeSlotInt sets (if new) or adds duration value to the element of the time_slots map
func (lp *LoggingParams) AddTimeSlotInt(name string, dur int64) {
lp.setIntMapFieldValue(name, dur)
}
// AddTimeSlotDurationInMs sets (if new) or adds duration value in milliseconds to the element of the time_slots map
func (lp *LoggingParams) AddTimeSlotDurationInMs(name string, dur time.Duration) {
lp.AddTimeSlotInt(name, dur.Milliseconds())
}
func (lp *LoggingParams) setIntMapFieldValue(fieldName string, value int64) {
if lp.timeSlots == nil {
lp.timeSlots = make(loggableIntMap, 1)
}
lp.timeSlots[fieldName] += value
}