async calls return the call_id/task_id

This commit is contained in:
Seif Lotfy
2016-10-01 04:04:51 +02:00
parent d8801d5be7
commit a8535120da
3 changed files with 39 additions and 7 deletions

29
api/common/ctx.go Normal file
View File

@@ -0,0 +1,29 @@
package common
import (
"github.com/Sirupsen/logrus"
"golang.org/x/net/context"
)
// WithLogger stores the logger.
func WithLogger(ctx context.Context, l logrus.FieldLogger) context.Context {
return context.WithValue(ctx, "logger", l)
}
// Logger returns the structured logger.
func Logger(ctx context.Context) logrus.FieldLogger {
l, ok := ctx.Value("logger").(logrus.FieldLogger)
if !ok {
return logrus.StandardLogger()
}
return l
}
// Attempt at simplifying this whole logger in the context thing
// Could even make this take a generic map, then the logger that gets returned could be used just like the stdlib too, since it's compatible
func LoggerWithFields(ctx context.Context, fields logrus.Fields) (context.Context, logrus.FieldLogger) {
l := Logger(ctx)
l = l.WithFields(fields)
ctx = WithLogger(ctx, l)
return ctx, l
}