comparison pkg/log/log.go @ 5601:1222b777f51f

Made golint finally happy.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sat, 06 Aug 2022 02:09:57 +0200
parents b4f59aef3f9e
children 2dd155cc95ec
comparison
equal deleted inserted replaced
5600:9967a78e43f4 5601:1222b777f51f
20 "strings" 20 "strings"
21 "sync" 21 "sync"
22 "sync/atomic" 22 "sync/atomic"
23 ) 23 )
24 24
25 type LogLevel uint32 25 // Level is an enumeration symbolizing a log level.
26 type Level uint32
26 27
27 const ( 28 const (
28 TraceLogLevel = LogLevel(iota) 29 // TraceLogLevel is the TRACE log level.
30 TraceLogLevel = Level(iota)
31 // DebugLogLevel is the DEBUG log level.
29 DebugLogLevel 32 DebugLogLevel
33 // InfoLogLevel is the INFO log level.
30 InfoLogLevel 34 InfoLogLevel
35 // WarnLogLevel is the WARN log level.
31 WarnLogLevel 36 WarnLogLevel
37 // ErrorLogLevel is the ERROR log level.
32 ErrorLogLevel 38 ErrorLogLevel
39 // FatalLogLevel is the FATAL log level.
33 FatalLogLevel 40 FatalLogLevel
34 ) 41 )
35 42
36 var ( 43 var (
37 logLevel = uint32(InfoLogLevel) 44 logLevel = uint32(InfoLogLevel)
43 lg.SetFlags(lg.LstdFlags | lg.Lshortfile) 50 lg.SetFlags(lg.LstdFlags | lg.Lshortfile)
44 } 51 }
45 52
46 const callDepth = 2 53 const callDepth = 2
47 54
55 // SetupLog redirects the log output to a given file.
56 // The file is opened in append mode with the given
57 // permisssions. A previously opened log file will
58 // be closed.
48 func SetupLog(filename string, perm os.FileMode) error { 59 func SetupLog(filename string, perm os.FileMode) error {
49 f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, perm) 60 f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, perm)
50 if err != nil { 61 if err != nil {
51 return err 62 return err
52 } 63 }
58 logFile = f 69 logFile = f
59 lg.SetOutput(logFile) 70 lg.SetOutput(logFile)
60 return nil 71 return nil
61 } 72 }
62 73
74 // ShutdownLog closes an open log file (if there is any in use)
75 // and redirects the output to stderr.
63 func ShutdownLog() { 76 func ShutdownLog() {
64 logFileMu.Lock() 77 logFileMu.Lock()
65 defer logFileMu.Unlock() 78 defer logFileMu.Unlock()
66 if logFile != nil { 79 if logFile != nil {
67 logFile.Close() 80 logFile.Close()
68 logFile = nil 81 logFile = nil
69 } 82 }
70 lg.SetOutput(os.Stderr) 83 lg.SetOutput(os.Stderr)
71 } 84 }
72 85
73 func ParseLogLevel(s string) LogLevel { 86 // ParseLogLevel converts a strings representation
87 // of a log level to the reprective log level.
88 // If the log level is unknown InfoLogLevel is
89 // returned.
90 func ParseLogLevel(s string) Level {
74 switch strings.ToLower(s) { 91 switch strings.ToLower(s) {
75 case "trace": 92 case "trace":
76 return TraceLogLevel 93 return TraceLogLevel
77 case "debug": 94 case "debug":
78 return DebugLogLevel 95 return DebugLogLevel
87 default: 104 default:
88 return InfoLogLevel 105 return InfoLogLevel
89 } 106 }
90 } 107 }
91 108
92 func (level LogLevel) String() string { 109 // String implements the fmt.Stringer interface.
110 func (level Level) String() string {
93 switch level { 111 switch level {
94 case TraceLogLevel: 112 case TraceLogLevel:
95 return "trace" 113 return "trace"
96 case DebugLogLevel: 114 case DebugLogLevel:
97 return "debug" 115 return "debug"
106 default: 124 default:
107 return "unknown" 125 return "unknown"
108 } 126 }
109 } 127 }
110 128
111 func GetLogLevel() LogLevel { 129 // GetLogLevel returns the currently active log level.
112 return LogLevel(atomic.LoadUint32(&logLevel)) 130 func GetLogLevel() Level {
113 } 131 return Level(atomic.LoadUint32(&logLevel))
114 132 }
115 func SetLogLevel(level LogLevel) { 133
134 // SetLogLevel sets the log level to be active.
135 func SetLogLevel(level Level) {
116 atomic.StoreUint32(&logLevel, uint32(level)) 136 atomic.StoreUint32(&logLevel, uint32(level))
117 } 137 }
118 138
139 // Tracef formats a log message as a TRACE output.
119 func Tracef(f string, args ...interface{}) { 140 func Tracef(f string, args ...interface{}) {
120 if TraceLogLevel >= GetLogLevel() { 141 if TraceLogLevel >= GetLogLevel() {
121 s := fmt.Sprintf(f, args...) 142 s := fmt.Sprintf(f, args...)
122 lg.Output(callDepth, "[TRACE] "+s) 143 lg.Output(callDepth, "[TRACE] "+s)
123 } 144 }
124 } 145 }
125 146
147 // Traceln line prints a log message as a TRACE output.
126 func Traceln(s string) { 148 func Traceln(s string) {
127 if TraceLogLevel >= GetLogLevel() { 149 if TraceLogLevel >= GetLogLevel() {
128 lg.Output(callDepth, "[TRACE] "+s) 150 lg.Output(callDepth, "[TRACE] "+s)
129 } 151 }
130 } 152 }
131 153
154 // Debugf formats a log message as a DEBUG output.
132 func Debugf(f string, args ...interface{}) { 155 func Debugf(f string, args ...interface{}) {
133 if DebugLogLevel >= GetLogLevel() { 156 if DebugLogLevel >= GetLogLevel() {
134 s := fmt.Sprintf(f, args...) 157 s := fmt.Sprintf(f, args...)
135 lg.Output(callDepth, "[DEBUG] "+s) 158 lg.Output(callDepth, "[DEBUG] "+s)
136 } 159 }
137 } 160 }
138 161
162 // Debugln line prints a log message as a DEBUG output.
139 func Debugln(s string) { 163 func Debugln(s string) {
140 if DebugLogLevel >= GetLogLevel() { 164 if DebugLogLevel >= GetLogLevel() {
141 lg.Output(callDepth, "[DEBUG] "+s) 165 lg.Output(callDepth, "[DEBUG] "+s)
142 } 166 }
143 } 167 }
144 168
169 // Infof formats a log message as a INFO output.
145 func Infof(f string, args ...interface{}) { 170 func Infof(f string, args ...interface{}) {
146 if InfoLogLevel >= GetLogLevel() { 171 if InfoLogLevel >= GetLogLevel() {
147 s := fmt.Sprintf(f, args...) 172 s := fmt.Sprintf(f, args...)
148 lg.Output(callDepth, "[INFO] "+s) 173 lg.Output(callDepth, "[INFO] "+s)
149 } 174 }
150 } 175 }
151 176
177 // Infoln line prints a log message as a INFO output.
152 func Infoln(s string) { 178 func Infoln(s string) {
153 if InfoLogLevel >= GetLogLevel() { 179 if InfoLogLevel >= GetLogLevel() {
154 lg.Output(callDepth, "[INFO] "+s) 180 lg.Output(callDepth, "[INFO] "+s)
155 } 181 }
156 } 182 }
157 183
184 // Warnf formats a log message as a WARN output.
158 func Warnf(f string, args ...interface{}) { 185 func Warnf(f string, args ...interface{}) {
159 if WarnLogLevel >= GetLogLevel() { 186 if WarnLogLevel >= GetLogLevel() {
160 s := fmt.Sprintf(f, args...) 187 s := fmt.Sprintf(f, args...)
161 lg.Output(callDepth, "[WARN] "+s) 188 lg.Output(callDepth, "[WARN] "+s)
162 } 189 }
163 } 190 }
164 191
192 // Warnln line prints a log message as a WARN output.
165 func Warnln(s string) { 193 func Warnln(s string) {
166 if WarnLogLevel >= GetLogLevel() { 194 if WarnLogLevel >= GetLogLevel() {
167 lg.Output(callDepth, "[WARN] "+s) 195 lg.Output(callDepth, "[WARN] "+s)
168 } 196 }
169 } 197 }
170 198
199 // Errorf formats a log message as an ERROR output.
171 func Errorf(f string, args ...interface{}) { 200 func Errorf(f string, args ...interface{}) {
172 if ErrorLogLevel >= GetLogLevel() { 201 if ErrorLogLevel >= GetLogLevel() {
173 s := fmt.Sprintf(f, args...) 202 s := fmt.Sprintf(f, args...)
174 lg.Output(callDepth, "[ERROR] "+s) 203 lg.Output(callDepth, "[ERROR] "+s)
175 } 204 }
176 } 205 }
177 206
207 // Errorln line prints a log message as an ERROR output.
178 func Errorln(s string) { 208 func Errorln(s string) {
179 if ErrorLogLevel >= GetLogLevel() { 209 if ErrorLogLevel >= GetLogLevel() {
180 lg.Output(callDepth, "[ERROR] "+s) 210 lg.Output(callDepth, "[ERROR] "+s)
181 } 211 }
182 } 212 }
183 213
214 // Fatalf formats a log message as a FATAL output
215 // and terminates the programs with an error code (1).
184 func Fatalf(f string, args ...interface{}) { 216 func Fatalf(f string, args ...interface{}) {
185 if FatalLogLevel >= GetLogLevel() { 217 if FatalLogLevel >= GetLogLevel() {
186 s := fmt.Sprintf(f, args...) 218 s := fmt.Sprintf(f, args...)
187 lg.Output(callDepth, "[FATAL] "+s) 219 lg.Output(callDepth, "[FATAL] "+s)
188 os.Exit(1) 220 os.Exit(1)
189 } 221 }
190 } 222 }
191 223
224 // Fatalln line prints a log message as a FATAL output
225 // and terminates the programs with an error code (1).
192 func Fatalln(s string) { 226 func Fatalln(s string) {
193 if FatalLogLevel >= GetLogLevel() { 227 if FatalLogLevel >= GetLogLevel() {
194 lg.Output(callDepth, "[FATAL] "+s) 228 lg.Output(callDepth, "[FATAL] "+s)
195 os.Exit(1) 229 os.Exit(1)
196 } 230 }
197 } 231 }
198 232
233 // Panicf formats a log message as a PANIC output
234 // and throws a panic.
199 func Panicf(f string, args ...interface{}) { 235 func Panicf(f string, args ...interface{}) {
200 s := fmt.Sprintf(f, args...) 236 s := fmt.Sprintf(f, args...)
201 lg.Output(callDepth, "[PANIC] "+s) 237 lg.Output(callDepth, "[PANIC] "+s)
202 panic(s) 238 panic(s)
203 } 239 }
204 240
241 // Panicln line prints a log message as a PANIC output
242 // and throws a panic.
205 func Panicln(s string) { 243 func Panicln(s string) {
206 lg.Output(callDepth, "[PANIC] "+s) 244 lg.Output(callDepth, "[PANIC] "+s)
207 panic(s) 245 panic(s)
208 } 246 }