...

Source file src/github.com/docker/distribution/context/logger.go

Documentation: github.com/docker/distribution/context

     1  package context
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"runtime"
     7  
     8  	"github.com/sirupsen/logrus"
     9  )
    10  
    11  // Logger provides a leveled-logging interface.
    12  type Logger interface {
    13  	// standard logger methods
    14  	Print(args ...interface{})
    15  	Printf(format string, args ...interface{})
    16  	Println(args ...interface{})
    17  
    18  	Fatal(args ...interface{})
    19  	Fatalf(format string, args ...interface{})
    20  	Fatalln(args ...interface{})
    21  
    22  	Panic(args ...interface{})
    23  	Panicf(format string, args ...interface{})
    24  	Panicln(args ...interface{})
    25  
    26  	// Leveled methods, from logrus
    27  	Debug(args ...interface{})
    28  	Debugf(format string, args ...interface{})
    29  	Debugln(args ...interface{})
    30  
    31  	Error(args ...interface{})
    32  	Errorf(format string, args ...interface{})
    33  	Errorln(args ...interface{})
    34  
    35  	Info(args ...interface{})
    36  	Infof(format string, args ...interface{})
    37  	Infoln(args ...interface{})
    38  
    39  	Warn(args ...interface{})
    40  	Warnf(format string, args ...interface{})
    41  	Warnln(args ...interface{})
    42  
    43  	WithError(err error) *logrus.Entry
    44  }
    45  
    46  type loggerKey struct{}
    47  
    48  // WithLogger creates a new context with provided logger.
    49  func WithLogger(ctx context.Context, logger Logger) context.Context {
    50  	return context.WithValue(ctx, loggerKey{}, logger)
    51  }
    52  
    53  // GetLoggerWithField returns a logger instance with the specified field key
    54  // and value without affecting the context. Extra specified keys will be
    55  // resolved from the context.
    56  func GetLoggerWithField(ctx context.Context, key, value interface{}, keys ...interface{}) Logger {
    57  	return getLogrusLogger(ctx, keys...).WithField(fmt.Sprint(key), value)
    58  }
    59  
    60  // GetLoggerWithFields returns a logger instance with the specified fields
    61  // without affecting the context. Extra specified keys will be resolved from
    62  // the context.
    63  func GetLoggerWithFields(ctx context.Context, fields map[interface{}]interface{}, keys ...interface{}) Logger {
    64  	// must convert from interface{} -> interface{} to string -> interface{} for logrus.
    65  	lfields := make(logrus.Fields, len(fields))
    66  	for key, value := range fields {
    67  		lfields[fmt.Sprint(key)] = value
    68  	}
    69  
    70  	return getLogrusLogger(ctx, keys...).WithFields(lfields)
    71  }
    72  
    73  // GetLogger returns the logger from the current context, if present. If one
    74  // or more keys are provided, they will be resolved on the context and
    75  // included in the logger. While context.Value takes an interface, any key
    76  // argument passed to GetLogger will be passed to fmt.Sprint when expanded as
    77  // a logging key field. If context keys are integer constants, for example,
    78  // its recommended that a String method is implemented.
    79  func GetLogger(ctx context.Context, keys ...interface{}) Logger {
    80  	return getLogrusLogger(ctx, keys...)
    81  }
    82  
    83  // GetLogrusLogger returns the logrus logger for the context. If one more keys
    84  // are provided, they will be resolved on the context and included in the
    85  // logger. Only use this function if specific logrus functionality is
    86  // required.
    87  func getLogrusLogger(ctx context.Context, keys ...interface{}) *logrus.Entry {
    88  	var logger *logrus.Entry
    89  
    90  	// Get a logger, if it is present.
    91  	loggerInterface := ctx.Value(loggerKey{})
    92  	if loggerInterface != nil {
    93  		if lgr, ok := loggerInterface.(*logrus.Entry); ok {
    94  			logger = lgr
    95  		}
    96  	}
    97  
    98  	if logger == nil {
    99  		fields := logrus.Fields{}
   100  
   101  		// Fill in the instance id, if we have it.
   102  		instanceID := ctx.Value("instance.id")
   103  		if instanceID != nil {
   104  			fields["instance.id"] = instanceID
   105  		}
   106  
   107  		fields["go.version"] = runtime.Version()
   108  		// If no logger is found, just return the standard logger.
   109  		logger = logrus.StandardLogger().WithFields(fields)
   110  	}
   111  
   112  	fields := logrus.Fields{}
   113  	for _, key := range keys {
   114  		v := ctx.Value(key)
   115  		if v != nil {
   116  			fields[fmt.Sprint(key)] = v
   117  		}
   118  	}
   119  
   120  	return logger.WithFields(fields)
   121  }
   122  

View as plain text