github.com/furusax0621/goa-v1@v1.4.3/logging/log15/adapter.go (about)

     1  /*
     2  Package goalog15 contains an adapter that makes it possible to configure goa so it uses log15
     3  as logger backend.
     4  Usage:
     5  
     6      logger := log15.New()
     7      // ... Initialize logger handler using log15 package
     8      service.WithLogger(goalog15.New(logger))
     9      // ... Proceed with configuring and starting the goa service
    10  
    11      // In handlers:
    12      goalog15.Logger(ctx).Info("foo")
    13  */
    14  package goalog15
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/goadesign/goa"
    20  	"github.com/inconshreveable/log15"
    21  )
    22  
    23  // adapter is the log15 goa adapter logger.
    24  type adapter struct {
    25  	log15.Logger
    26  }
    27  
    28  // New wraps a log15 logger into a goa logger adapter.
    29  func New(logger log15.Logger) goa.LogAdapter {
    30  	return &adapter{Logger: logger}
    31  }
    32  
    33  // Logger returns the log15 logger stored in the given context if any, nil otherwise.
    34  func Logger(ctx context.Context) log15.Logger {
    35  	logger := goa.ContextLogger(ctx)
    36  	if a, ok := logger.(*adapter); ok {
    37  		return a.Logger
    38  	}
    39  	return nil
    40  }
    41  
    42  // Info logs informational messages using log15.
    43  func (a *adapter) Info(msg string, data ...interface{}) {
    44  	a.Logger.Info(msg, data...)
    45  }
    46  
    47  // Error logs error messages using log15.
    48  func (a *adapter) Error(msg string, data ...interface{}) {
    49  	a.Logger.Error(msg, data...)
    50  }
    51  
    52  // New creates a new logger given a context.
    53  func (a *adapter) New(data ...interface{}) goa.LogAdapter {
    54  	return &adapter{Logger: a.Logger.New(data...)}
    55  }