diff --git a/log/logger.go b/log/logger.go index 060ded548d286d1a863f6bd33d9d0b9edd809fb4..3c40a2c807fa8847196d0435f8995de33db8c13b 100644 --- a/log/logger.go +++ b/log/logger.go @@ -22,7 +22,7 @@ type Logger struct { func get() *Logger { once.Do(func() { logger = &Logger{ - Out: os.Stdout, + Out: os.Stderr, Loglevel: INFO, lock: sync.Mutex{}, } @@ -30,6 +30,8 @@ func get() *Logger { return logger } +//Loglevel sets the verbosity of the logger +//Defaults to INFO func Loglevel(level Level) { l := get() l.lock.Lock() @@ -37,6 +39,8 @@ func Loglevel(level Level) { l.Loglevel = level } +//Output defines the output of the logger +//Defaults to os.Stderr func Output(out io.Writer) { l := get() l.lock.Lock() @@ -44,38 +48,62 @@ func Output(out io.Writer) { l.Out = out } +//Debug passes the DEBUG flag and a +//message to the logger func Debug(args ...interface{}) { log(DEBUG, args...) } +//Debug passes the DEBUG flag and a +//message to the logger func Info(args ...interface{}) { log(INFO, args...) } +//Warn passes the WARNING flag and a +//message to the logger func Warn(args ...interface{}) { log(WARNING, args...) } +//Error passes the ERROR flag and a +//message to the logger func Error(args ...interface{}) { log(ERROR, args...) } +//Fatal passes the FATAL flag and a +//message to the logger and calls +//os.Exit(1) func Fatal(args ...interface{}) { log(FATAL, args...) + os.Exit(1) } +//Panic passes the PANIC flag and a +//message to the logger +//Also calls builtin.panic() func Panic(args ...interface{}) { log(PANIC, args...) + panic(args) } func log(level Level, args ...interface{}) { + defer func() { + if r := recover(); r != nil { + fmt.Println("Recovered in f", r) + } + }() l := get() l.lock.Lock() defer l.lock.Unlock() if level <= l.Loglevel { msg := fmt.Sprint(args...) logMessage := time.Now().Format(time.RFC3339) + "\t" + prefix(level) + "\t" + msg + "\n" - l.Out.Write([]byte(logMessage)) + _,err := l.Out.Write([]byte(logMessage)) + if err != nil { + panic(err) + } } }