-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep_options.go
More file actions
73 lines (61 loc) · 2.21 KB
/
step_options.go
File metadata and controls
73 lines (61 loc) · 2.21 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
package sqlslog
import "log/slog"
type EventOptions struct {
Msg string
Level Level
}
// StepEventMsgBuilder is the function type to format the step log message.
type StepEventMsgBuilder func(step Step, event Event) string
// StepEventMsgWithEventName returns the formatted step log message with the event name.
func StepEventMsgWithEventName(step Step, event Event) string {
return step.String() + " " + event.String()
}
// StepEventMsgWithoutEventName returns the formatted step log message without the event name.
func StepEventMsgWithoutEventName(step Step, _ Event) string {
return step.String()
}
// StepOptions is an struct that expresses the options for the step.
type StepOptions struct {
Start EventOptions
Error EventOptions
Complete EventOptions
// ErrorHandler is the function to handle the error.
// When the error should not be logged as an error but as complete, it should return true.
// It can also add attributes to the log.
ErrorHandler func(error) (bool, []slog.Attr)
}
const defaultSlogLevelDiff = 4
func (o *StepOptions) SetLevel(lv Level) {
o.Start.Level = lv - defaultSlogLevelDiff
o.Complete.Level = lv
}
func (o *StepOptions) compare(other *StepOptions) bool {
return o.Start.Level == other.Start.Level &&
o.Error.Level == other.Error.Level &&
o.Complete.Level == other.Complete.Level
}
func newStepOptions(f StepEventMsgBuilder, step Step, startLevel, errorLevel, completeLevel Level) *StepOptions {
return &StepOptions{
Start: EventOptions{Msg: f(step, EventStart), Level: startLevel},
Error: EventOptions{Msg: f(step, EventError), Level: errorLevel},
Complete: EventOptions{Msg: f(step, EventComplete), Level: completeLevel},
}
}
func defaultStepOptions(msgb StepEventMsgBuilder, step Step, completeLevel Level, errHandlers ...func(error) (bool, []slog.Attr)) *StepOptions { // nolint:unparam
var startLevel Level
switch completeLevel { // nolint:exhaustive
case LevelError:
startLevel = LevelDebug
case LevelInfo:
startLevel = LevelTrace
case LevelDebug:
startLevel = LevelVerbose
default:
startLevel = LevelVerbose
}
r := newStepOptions(msgb, step, startLevel, LevelError, completeLevel)
if len(errHandlers) > 0 {
r.ErrorHandler = errHandlers[0]
}
return r
}