summaryrefslogtreecommitdiffhomepage
path: root/logger.go
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2018-02-04 16:08:26 +0100
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2018-02-04 16:08:26 +0100
commita0f54cbe5ac2cd8b8296c2c57c30029dd349cff0 (patch)
tree64574090d79ff3899c5c18e5268e450028e4656b /logger.go
parent5871ec04deb8f4715cab37146940baa35c08cbee (diff)
Align with go library layout
Diffstat (limited to 'logger.go')
-rw-r--r--logger.go50
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
+}