diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2017-12-18 22:34:56 +0100 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2017-12-18 22:35:09 +0100 |
commit | b01cef071c5e95f73e756c3d039e2ddf1a6ed3df (patch) | |
tree | 12573257a20a055e86526602535018756c9e9527 /logging/log.go | |
parent | 2cfd9f146d042a1c30176ad91fea730df439704d (diff) | |
download | petrific-b01cef071c5e95f73e756c3d039e2ddf1a6ed3df.tar.gz petrific-b01cef071c5e95f73e756c3d039e2ddf1a6ed3df.tar.bz2 petrific-b01cef071c5e95f73e756c3d039e2ddf1a6ed3df.zip |
Add logging
Diffstat (limited to 'logging/log.go')
-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 +} |