diff options
Diffstat (limited to 'logger.go')
-rw-r--r-- | logger.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/logger.go b/logger.go new file mode 100644 index 0000000..0872ef9 --- /dev/null +++ b/logger.go @@ -0,0 +1,50 @@ +package main + +import ( + "io" + "io/ioutil" + "log" + "os" +) + +const ( + LogLevelError = iota + LogLevelInfo + LogLevelDebug +) + +type Logger struct { + Debug *log.Logger + Info *log.Logger + Error *log.Logger +} + +func NewLogger(level int, prepend string) *Logger { + output := os.Stdout + logger := new(Logger) + + logErr, logInfo, logDebug := func() (io.Writer, io.Writer, io.Writer) { + if level >= LogLevelDebug { + return output, output, output + } + if level >= LogLevelInfo { + return output, output, ioutil.Discard + } + return output, ioutil.Discard, ioutil.Discard + }() + + logger.Debug = log.New(logDebug, + "DEBUG: "+prepend, + log.Ldate|log.Ltime|log.Lshortfile, + ) + + logger.Info = log.New(logInfo, + "INFO: "+prepend, + log.Ldate|log.Ltime, + ) + logger.Error = log.New(logErr, + "ERROR: "+prepend, + log.Ldate|log.Ltime, + ) + return logger +} |