package main import ( "fmt" "log" "path/filepath" "runtime" ) const ( logDebug uint = iota logWarn logAlert ) func failError(err error, format string, a ...interface{}) { if err != nil { _, fn, line, _ := runtime.Caller(1) out := fmt.Sprintf(format, a...) log.Fatalf("[%s:%d] %s : %s", filepath.Base(fn), line, out, err) } } func logError(err error, lvl uint, format string, a ...interface{}) { if err != nil && lvl >= 0 { _, fn, line, _ := runtime.Caller(2) out := fmt.Sprintf(format, a...) log.Printf("[%s:%d] %s : %s", filepath.Base(fn), line, out, err) } } func logInfo(lvl uint, format string, a ...interface{}) { if lvl >= 0 { _, fn, line, _ := runtime.Caller(2) out := fmt.Sprintf(format, a...) log.Printf("[%s:%d] %s", filepath.Base(fn), line, out) } } func logInfoDebug(format string, a ...interface{}) { logInfo(logDebug, format, a...) } func logInfoWarn(format string, a ...interface{}) { logInfo(logWarn, format, a...) } func logInfoAlert(format string, a ...interface{}) { logInfo(logAlert, format, a...) } func logErrorDebug(err error, format string, a ...interface{}) { logError(err, logDebug, format, a...) } func logErrorWarn(err error, format string, a ...interface{}) { logError(err, logWarn, format, a...) } func logErrorAlert(err error, format string, a ...interface{}) { logError(err, logAlert, format, a...) }