diff options
Diffstat (limited to 'logging')
-rw-r--r-- | logging/log.go | 56 |
1 files changed, 56 insertions, 0 deletions
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 +} |