From b01cef071c5e95f73e756c3d039e2ddf1a6ed3df Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Mon, 18 Dec 2017 22:34:56 +0100 Subject: Add logging --- logging/log.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 logging/log.go (limited to 'logging') diff --git a/logging/log.go b/logging/log.go new file mode 100644 index 0000000..9bef591 --- /dev/null +++ b/logging/log.go @@ -0,0 +1,56 @@ +package logging + +import ( + "io" + "log" +) + +type Logger interface { + Print(v ...interface{}) + Printf(format string, v ...interface{}) + Println(v ...interface{}) +} + +type NopLogger struct{} + +func (NopLogger) Print(v ...interface{}) {} +func (NopLogger) Printf(format string, v ...interface{}) {} +func (NopLogger) Println(v ...interface{}) {} + +type Level int + +const ( + LQuiet Level = iota + LError + LWarn + LInfo + LDebug +) + +type Log struct { + err, warn, info, debug Logger +} + +func (l Log) Error() Logger { return l.err } +func (l Log) Warn() Logger { return l.warn } +func (l Log) Info() Logger { return l.info } +func (l Log) Debug() Logger { return l.debug } + +func buildLogger(out io.Writer, prefix string, lWant, lHave Level) Logger { + if lWant <= lHave { + return log.New(out, prefix, log.LstdFlags) + } else { + return NopLogger{} + } +} + +func NewLog(out io.Writer, level Level) *Log { + l := new(Log) + + l.err = buildLogger(out, "[Err]", LError, level) + l.warn = buildLogger(out, "[Warn]", LWarn, level) + l.info = buildLogger(out, "[Info]", LInfo, level) + l.debug = buildLogger(out, "[Debug]", LDebug, level) + + return l +} -- cgit v1.2.3-70-g09d2