From 706ac9a0abe3502b160c9ebf3bfcd2401106f4b6 Mon Sep 17 00:00:00 2001 From: Maciek Borzecki Date: Sat, 20 Jan 2018 20:40:09 +0100 Subject: [PATCH] logger: simple logger --- logger/context.go | 22 ++++++++++++++++++++++ logger/logger.go | 27 +++++++++++++++++++++++++++ logger/logrus.go | 24 ++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 logger/context.go create mode 100644 logger/logger.go create mode 100644 logger/logrus.go diff --git a/logger/context.go b/logger/context.go new file mode 100644 index 0000000..545bd08 --- /dev/null +++ b/logger/context.go @@ -0,0 +1,22 @@ +package logger + +import ( + "context" +) + +type loggerKeyType int + +const ( + loggerContextKey loggerKeyType = 1 +) + +func FromContext(ctx context.Context) Logger { + if logger, _ := ctx.Value(loggerContextKey).(Logger); logger != nil { + return logger + } + return New() +} + +func WithContext(ctx context.Context, logger Logger) context.Context { + return context.WithValue(ctx, loggerContextKey, logger) +} diff --git a/logger/logger.go b/logger/logger.go new file mode 100644 index 0000000..7e889e1 --- /dev/null +++ b/logger/logger.go @@ -0,0 +1,27 @@ +package logger + +type Logger interface { + Debug(args ...interface{}) + Debugf(format string, args ...interface{}) + Debugln(args ...interface{}) + + Info(args ...interface{}) + Infof(format string, args ...interface{}) + Infoln(args ...interface{}) + + Error(args ...interface{}) + Errorf(format string, args ...interface{}) + Errorln(args ...interface{}) + + Warning(args ...interface{}) + Warningf(format string, args ...interface{}) + Warningln(args ...interface{}) + + Panic(args ...interface{}) + Panicf(format string, args ...interface{}) + Panicln(args ...interface{}) + + Print(args ...interface{}) + Printf(format string, args ...interface{}) + Println(args ...interface{}) +} diff --git a/logger/logrus.go b/logger/logrus.go new file mode 100644 index 0000000..b97cfeb --- /dev/null +++ b/logger/logrus.go @@ -0,0 +1,24 @@ +package logger + +import ( + "os" + + "github.com/Sirupsen/logrus" +) + +var log *logrus.Logger + +func init() { + log = &logrus.Logger{ + Out: os.Stderr, + Formatter: &logrus.TextFormatter{ + FullTimestamp: true, + }, + Hooks: make(logrus.LevelHooks), + Level: logrus.DebugLevel, + } +} + +func New() Logger { + return log +}