File: severity.go

package info (click to toggle)
golang-golang-x-exp 0.0~git20231006.7918f67-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 6,492 kB
  • sloc: ansic: 1,900; objc: 276; sh: 272; asm: 48; makefile: 27
file content (154 lines) | stat: -rw-r--r-- 3,387 bytes parent folder | download | duplicates (4)
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
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package severity

import (
	"context"
	"fmt"

	"golang.org/x/exp/event"
)

// Level represents a severity level of an event.
// The basic severity levels are designed to match the levels used in open telemetry.
// Smaller numerical values correspond to less severe events (such as debug events),
// larger numerical values correspond to more severe events (such as errors and critical events).
//
// The following table defines the meaning severity levels:
// 1-4	TRACE	A fine-grained debugging event. Typically disabled in default configurations.
// 5-8	DEBUG	A debugging event.
// 9-12	INFO	An informational event. Indicates that an event happened.
// 13-16	WARN	A warning event. Not an error but is likely more important than an informational event.
// 17-20	ERROR	An error event. Something went wrong.
// 21-24	FATAL	A fatal error such as application or system crash.
//
// See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#severity-fields
// for more details
type Level uint64

const (
	Trace   = Level(1)
	Debug   = Level(5)
	Info    = Level(9)
	Warning = Level(13)
	Error   = Level(17)
	Fatal   = Level(21)
	Max     = Level(24)
)

const Key = "level"

// Of creates a label for the level.
func (l Level) Label() event.Label {
	return event.Value(Key, l)
}

// From can be used to get a value from a Label.
func From(t event.Label) Level {
	return t.Interface().(Level)
}

func (l Level) Log(ctx context.Context, msg string, labels ...event.Label) {
	ev := event.New(ctx, event.LogKind)
	if ev != nil {
		ev.Labels = append(ev.Labels, l.Label())
		ev.Labels = append(ev.Labels, labels...)
		ev.Labels = append(ev.Labels, event.String("msg", msg))
		ev.Deliver()
	}
}

func (l Level) Logf(ctx context.Context, msg string, args ...interface{}) {
	ev := event.New(ctx, event.LogKind)
	if ev != nil {
		ev.Labels = append(ev.Labels, l.Label())
		ev.Labels = append(ev.Labels, event.String("msg", fmt.Sprintf(msg, args...)))
		ev.Deliver()
	}
}

func (l Level) Class() Level {
	switch {
	case l > Max:
		return Max
	case l > Fatal:
		return Fatal
	case l > Error:
		return Error
	case l > Warning:
		return Warning
	case l > Debug:
		return Debug
	case l > Info:
		return Info
	case l > Trace:
		return Trace
	default:
		return 0
	}
}

func (l Level) String() string {
	switch l {
	case 0:
		return "invalid"

	case Trace:
		return "trace"
	case Trace + 1:
		return "trace2"
	case Trace + 2:
		return "trace3"
	case Trace + 3:
		return "trace4"

	case Debug:
		return "debug"
	case Debug + 1:
		return "debug2"
	case Debug + 2:
		return "debug3"
	case Debug + 3:
		return "debug4"

	case Info:
		return "info"
	case Info + 1:
		return "info2"
	case Info + 2:
		return "info3"
	case Info + 3:
		return "info4"

	case Warning:
		return "warning"
	case Warning + 1:
		return "warning2"
	case Warning + 2:
		return "warning3"
	case Warning + 3:
		return "warning4"

	case Error:
		return "error"
	case Error + 1:
		return "error2"
	case Error + 2:
		return "error3"
	case Error + 3:
		return "error4"

	case Fatal:
		return "fatal"
	case Fatal + 1:
		return "fatal2"
	case Fatal + 2:
		return "fatal3"
	case Fatal + 3:
		return "fatal4"
	default:
		return "invalid"
	}
}