Created
March 28, 2019 14:50
-
-
Save UlisseMini/7a0d791d47b505fbac9f5392f52f42b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
const ( | |
debugLevel = iota | |
infoLevel | |
warnLevel | |
errorLevel | |
fatalLevel | |
logLevelCount | |
) | |
type logFn func(s string, a ...interface{}) | |
func initLogFuncs() []logFn { | |
logFuncs := make([]logFn, logLevelCount) | |
for level := 0; level <= fatalLevel; level++ { | |
if logLevel <= level { | |
logFuncs[level] = func(s string, a ...interface{}) { | |
fmt.Printf(s, a...) | |
} | |
continue | |
} | |
logFuncs[level] = func(string, ...interface{}) {} | |
} | |
return logFuncs | |
} | |
var logFuncs = initLogFuncs() | |
var ( | |
logDebug = logFuncs[debugLevel] | |
logInfo = logFuncs[infoLevel] | |
logWarn = logFuncs[warnLevel] | |
logError = logFuncs[errorLevel] | |
logFatal = logFuncs[fatalLevel] | |
) | |
const logLevel = infoLevel | |
func main() { | |
logDebug("debug message\n") | |
logInfo("info message\n") | |
logWarn("warning message\n") | |
logError("error message\n") | |
logFatal("fatal message\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment