Last active
August 29, 2015 14:03
-
-
Save dbehnke/bd9408451419d983b536 to your computer and use it in GitHub Desktop.
Simple wrapper to Go's built-in logging to add a name field and support DEBUG, INFO, WARN, ERROR levels
This file contains 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 logging | |
import ( | |
"fmt" | |
"log" | |
) | |
const ( | |
DEBUG = 0 | |
INFO = 1 | |
WARN = 2 | |
ERROR = 3 | |
) | |
type Log struct { | |
Name string | |
Loglevel int | |
} | |
func (l *Log) Debug(v ...interface{}) { | |
if l.Loglevel <= DEBUG { | |
log.Print(fmt.Sprintf("D [%s] ", l.Name), fmt.Sprint(v...)) | |
} | |
} | |
func (l *Log) Debugf(format string, v ...interface{}) { | |
if l.Loglevel <= DEBUG { | |
log.Print(fmt.Sprintf("D [%s] ", l.Name), fmt.Sprintf(format, v...)) | |
} | |
} | |
func (l *Log) Info(v ...interface{}) { | |
if l.Loglevel <= INFO { | |
log.Print(fmt.Sprintf("I [%s] ", l.Name), fmt.Sprint(v...)) | |
} | |
} | |
func (l *Log) Infof(format string, v ...interface{}) { | |
if l.Loglevel <= INFO { | |
log.Print(fmt.Sprintf("I [%s] ", l.Name), fmt.Sprintf(format, v...)) | |
} | |
} | |
func (l *Log) Warn(v ...interface{}) { | |
if l.Loglevel <= WARN { | |
log.Print(fmt.Sprintf("W [%s] ", l.Name), fmt.Sprint(v...)) | |
} | |
} | |
func (l *Log) Warnf(format string, v ...interface{}) { | |
if l.Loglevel <= WARN { | |
log.Print(fmt.Sprintf("W [%s] ", l.Name), fmt.Sprintf(format, v...)) | |
} | |
} | |
func (l *Log) Error(v ...interface{}) { | |
if l.Loglevel <= ERROR { | |
log.Print(fmt.Sprintf("E [%s] ", l.Name), fmt.Sprint(v...)) | |
} | |
} | |
func (l *Log) Errorf(format string, v ...interface{}) { | |
if l.Loglevel <= ERROR { | |
log.Print(fmt.Sprintf("E [%s] ", l.Name), fmt.Sprintf(format, v...)) | |
} | |
} |
This file contains 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 logging | |
import ( | |
"testing" | |
) | |
func logtest(log *Log, level int) { | |
log.Loglevel = level | |
teststring := "Testing" | |
log.Debug("Test") | |
log.Debugf("Test %s", teststring) | |
log.Info("Test") | |
log.Infof("Test %s", teststring) | |
log.Warn("Test") | |
log.Warnf("Test %s", teststring) | |
log.Error("Test") | |
log.Errorf("Test %s", teststring) | |
} | |
func TestLogging(t *testing.T) { | |
log := Log{"test", DEBUG} | |
logtest(&log, DEBUG) | |
logtest(&log, INFO) | |
logtest(&log, WARN) | |
logtest(&log, ERROR) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment