-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathlevel.go
68 lines (55 loc) · 1.76 KB
/
level.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package auto
import (
"bytes"
"errors"
"fmt"
)
// LogLevel defines the log level which instrumentation uses.
type LogLevel string
const (
// logLevelUndefined is an unset log level, it should not be used.
logLevelUndefined LogLevel = ""
// LogLevelDebug sets the logging level to log all messages.
LogLevelDebug LogLevel = "debug"
// LogLevelInfo sets the logging level to log only informational, warning, and error messages.
LogLevelInfo LogLevel = "info"
// LogLevelWarn sets the logging level to log only warning and error messages.
LogLevelWarn LogLevel = "warn"
// LogLevelError sets the logging level to log only error messages.
LogLevelError LogLevel = "error"
)
var errInvalidLogLevel = errors.New("invalid LogLevel")
// String returns the string encoding of the LogLevel l.
func (l LogLevel) String() string {
switch l {
case LogLevelDebug, LogLevelInfo, LogLevelWarn, LogLevelError, logLevelUndefined:
return string(l)
default:
return fmt.Sprintf("Level(%s)", string(l))
}
}
// UnmarshalText applies the LogLevel type when inputted text is valid.
func (l *LogLevel) UnmarshalText(text []byte) error {
*l = LogLevel(bytes.ToLower(text))
return l.validate()
}
func (l *LogLevel) validate() error {
if l == nil {
return errors.New("nil LogLevel")
}
switch *l {
case LogLevelDebug, LogLevelInfo, LogLevelWarn, LogLevelError:
// Valid.
default:
return fmt.Errorf("%w: %s", errInvalidLogLevel, l.String())
}
return nil
}
// ParseLogLevel return a new LogLevel parsed from text. A non-nil error is returned if text is not a valid LogLevel.
func ParseLogLevel(text string) (LogLevel, error) {
var level LogLevel
err := level.UnmarshalText([]byte(text))
return level, err
}