Allow interface{} keys when using logger

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day 2015-09-10 20:40:01 -07:00
parent b0e94fb154
commit 360c24d975
3 changed files with 10 additions and 5 deletions

View File

@ -54,8 +54,14 @@ func GetLoggerWithField(ctx Context, key, value interface{}, keys ...interface{}
// GetLoggerWithFields returns a logger instance with the specified fields // GetLoggerWithFields returns a logger instance with the specified fields
// without affecting the context. Extra specified keys will be resolved from // without affecting the context. Extra specified keys will be resolved from
// the context. // the context.
func GetLoggerWithFields(ctx Context, fields map[string]interface{}, keys ...interface{}) Logger { func GetLoggerWithFields(ctx Context, fields map[interface{}]interface{}, keys ...interface{}) Logger {
return getLogrusLogger(ctx, keys...).WithFields(logrus.Fields(fields)) // must convert from interface{} -> interface{} to string -> interface{} for logrus.
lfields := make(logrus.Fields, len(fields))
for key, value := range fields {
lfields[fmt.Sprint(key)] = value
}
return getLogrusLogger(ctx, keys...).WithFields(lfields)
} }
// GetLogger returns the logger from the current context, if present. If one // GetLogger returns the logger from the current context, if present. If one
@ -89,7 +95,6 @@ func getLogrusLogger(ctx Context, keys ...interface{}) *logrus.Entry {
} }
fields := logrus.Fields{} fields := logrus.Fields{}
for _, key := range keys { for _, key := range keys {
v := ctx.Value(key) v := ctx.Value(key)
if v != nil { if v != nil {

View File

@ -20,7 +20,7 @@ func Since(ctx Context, key interface{}) time.Duration {
// GetStringValue returns a string value from the context. The empty string // GetStringValue returns a string value from the context. The empty string
// will be returned if not found. // will be returned if not found.
func GetStringValue(ctx Context, key string) (value string) { func GetStringValue(ctx Context, key interface{}) (value string) {
stringi := ctx.Value(key) stringi := ctx.Value(key)
if stringi != nil { if stringi != nil {
if valuev, ok := stringi.(string); ok { if valuev, ok := stringi.(string); ok {

View File

@ -241,7 +241,7 @@ func (bw *blobWriter) validateBlob(ctx context.Context, desc distribution.Descri
if !verified { if !verified {
context.GetLoggerWithFields(ctx, context.GetLoggerWithFields(ctx,
map[string]interface{}{ map[interface{}]interface{}{
"canonical": canonical, "canonical": canonical,
"provided": desc.Digest, "provided": desc.Digest,
}, "canonical", "provided"). }, "canonical", "provided").