This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
/
level.go
134 lines (107 loc) · 2.78 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
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
/**
* CommonLog
*
* Copyright © 2015 Trevor N. Suarez (Rican7)
*/
// Package level defines the standard levels and their validations
package level
import (
"strings"
)
/**
* Types
*/
// A LogLevel defines a logging severity level, akin to the severity levels used
// in the Syslog standard. (https://tools.ietf.org/html/rfc5424)
type LogLevel uint8
// InvalidLogLevelError is an error representing an invalid log level occurrence
type InvalidLogLevelError struct {
InvalidValue *LogLevel
InvalidName *string
}
/**
* Constants
*/
// The log severity levels, as defined in RFC 5424 (Section 6.2.1):
// https://tools.ietf.org/html/rfc5424#section-6.2.1
const (
EMERGENCY LogLevel = iota
ALERT
CRITICAL
ERROR
WARNING
NOTICE
INFO
DEBUG
)
// errorValue defines the value returned for failed log level creations
const errorValue = ^LogLevel(0)
/**
* Variables
*/
// logLevelNames is a map of log level constants to their string names
var logLevelNames = map[LogLevel]string{
EMERGENCY: "EMERGENCY",
ALERT: "ALERT",
CRITICAL: "CRITICAL",
ERROR: "ERROR",
WARNING: "WARNING",
NOTICE: "NOTICE",
INFO: "INFO",
DEBUG: "DEBUG",
}
// logLevelNamesInverse is an inverted map of logLevelNames
var logLevelNamesInverse map[string]LogLevel
/**
* Functions
*/
// init initializes the package
func init() {
// Initialize our inverted log level name map
logLevelNamesInverse = make(map[string]LogLevel, len(logLevelNames))
for level, name := range logLevelNames {
logLevelNamesInverse[name] = level
}
}
// All returns an array of the standard defined log levels
func All() []LogLevel {
all := make([]LogLevel, len(logLevelNames))
for k := range logLevelNames {
all[k] = k
}
return all
}
// NewLogLevel gets a log level value by a string name
func NewLogLevel(name string) (LogLevel, error) {
// Cleanup the input
name = strings.ToUpper(strings.TrimSpace(name))
level, ok := logLevelNamesInverse[name]
if !ok {
return errorValue, &InvalidLogLevelError{InvalidName: &name}
}
return level, nil
}
// IsValid checks if a log level is valid based on the standard defined levels
func (l LogLevel) IsValid() (bool, error) {
if _, ok := logLevelNames[l]; !ok {
return false, &InvalidLogLevelError{InvalidValue: &l}
}
return true, nil
}
// String gets the name of a given log level constant
func (l LogLevel) String() string {
if valid, err := l.IsValid(); !valid {
return err.Error()
}
return logLevelNames[l]
}
// Error satisfies the error interface by returning a string message
func (e InvalidLogLevelError) Error() string {
msg := "Unknown error"
if nil != e.InvalidValue {
msg = "Invalid log level constant. Must be out of range."
} else if nil != e.InvalidName {
msg = "Invalid log level name. No log level exists with that given name."
}
return msg
}