Debian Codegraph @

event/severity/severity.go

raw symbols SCIP
1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package severity
6
7 import (
8 "context"
9 "fmt"
10
11 "golang.org/x/exp/event"
12 )
13
14 // Level represents a severity level of an event.
15 // The basic severity levels are designed to match the levels used in open telemetry.
16 // Smaller numerical values correspond to less severe events (such as debug events),
17 // larger numerical values correspond to more severe events (such as errors and critical events).
18 //
19 // The following table defines the meaning severity levels:
20 // 1-4 TRACE A fine-grained debugging event. Typically disabled in default configurations.
21 // 5-8 DEBUG A debugging event.
22 // 9-12 INFO An informational event. Indicates that an event happened.
23 // 13-16 WARN A warning event. Not an error but is likely more important than an informational event.
24 // 17-20 ERROR An error event. Something went wrong.
25 // 21-24 FATAL A fatal error such as application or system crash.
26 //
27 // See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#severity-fields
28 // for more details
29 type Level uint64
30
31 const (
32 Trace = Level(1)
33 Debug = Level(5)
34 Info = Level(9)
35 Warning = Level(13)
36 Error = Level(17)
37 Fatal = Level(21)
38 Max = Level(24)
39 )
40
41 const Key = "level"
42
43 // Of creates a label for the level.
44 func (l Level) Label() event.Label {
45 return event.Value(Key, l)
46 }
47
48 // From can be used to get a value from a Label.
49 func From(t event.Label) Level {
50 return t.Interface().(Level)
51 }
52
53 func (l Level) Log(ctx context.Context, msg string, labels ...event.Label) {
54 ev := event.New(ctx, event.LogKind)
55 if ev != nil {
56 ev.Labels = append(ev.Labels, l.Label())
57 ev.Labels = append(ev.Labels, labels...)
58 ev.Labels = append(ev.Labels, event.String("msg", msg))
59 ev.Deliver()
60 }
61 }
62
63 func (l Level) Logf(ctx context.Context, msg string, args ...interface{}) {
64 ev := event.New(ctx, event.LogKind)
65 if ev != nil {
66 ev.Labels = append(ev.Labels, l.Label())
67 ev.Labels = append(ev.Labels, event.String("msg", fmt.Sprintf(msg, args...)))
68 ev.Deliver()
69 }
70 }
71
72 func (l Level) Class() Level {
73 switch {
74 case l > Max:
75 return Max
76 case l > Fatal:
77 return Fatal
78 case l > Error:
79 return Error
80 case l > Warning:
81 return Warning
82 case l > Debug:
83 return Debug
84 case l > Info:
85 return Info
86 case l > Trace:
87 return Trace
88 default:
89 return 0
90 }
91 }
92
93 func (l Level) String() string {
94 switch l {
95 case 0:
96 return "invalid"
97
98 case Trace:
99 return "trace"
100 case Trace + 1:
101 return "trace2"
102 case Trace + 2:
103 return "trace3"
104 case Trace + 3:
105 return "trace4"
106
107 case Debug:
108 return "debug"
109 case Debug + 1:
110 return "debug2"
111 case Debug + 2:
112 return "debug3"
113 case Debug + 3:
114 return "debug4"
115
116 case Info:
117 return "info"
118 case Info + 1:
119 return "info2"
120 case Info + 2:
121 return "info3"
122 case Info + 3:
123 return "info4"
124
125 case Warning:
126 return "warning"
127 case Warning + 1:
128 return "warning2"
129 case Warning + 2:
130 return "warning3"
131 case Warning + 3:
132 return "warning4"
133
134 case Error:
135 return "error"
136 case Error + 1:
137 return "error2"
138 case Error + 2:
139 return "error3"
140 case Error + 3:
141 return "error4"
142
143 case Fatal:
144 return "fatal"
145 case Fatal + 1:
146 return "fatal2"
147 case Fatal + 2:
148 return "fatal3"
149 case Fatal + 3:
150 return "fatal4"
151 default:
152 return "invalid"
153 }
154 }