Skip to content

Commit

Permalink
feat: implement logging contract
Browse files Browse the repository at this point in the history
This commit removes the hard requirement to use logrus, and enables any log lib to be used, which fulfills the LoggerContract.

For backwards compatibility, logrus is kept as the default though.
  • Loading branch information
kernle32dll committed May 2, 2019
1 parent 0dd91f7 commit 8aca247
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
13 changes: 10 additions & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ var (
ErrNotImplemented = errors.New("not implemented")
)

// LoggerContract defines the logging methods required by the cache.
// This allows to use different kinds of logging libraries.
type LoggerContract interface {
Infof(format string, args ...interface{})
Debugf(format string, args ...interface{})
}

// Cache is a simple caching implementation to reuse JWTs till they expire.
type Cache struct {
jwt string
validity time.Time

name string
logger *logrus.Logger
logger LoggerContract
headroom time.Duration
tokenFunc func(ctx context.Context) (string, error)
}
Expand Down Expand Up @@ -53,7 +60,7 @@ func NewCache(opts ...Option) *Cache {

type config struct {
name string
logger *logrus.Logger
logger LoggerContract
headroom time.Duration
tokenFunc func(ctx context.Context) (string, error)
}
Expand All @@ -71,7 +78,7 @@ func Name(name string) Option {

// Name sets the logger to be used.
// The default is the logrus default logger.
func Logger(logger *logrus.Logger) Option {
func Logger(logger LoggerContract) Option {
return func(c *config) {
c.logger = logger
}
Expand Down
6 changes: 3 additions & 3 deletions cachemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type CacheMap struct {
lock *sync.RWMutex

name string
logger *logrus.Logger
logger LoggerContract
headroom time.Duration
tokenFunc func(ctx context.Context, key string) (string, error)
}
Expand Down Expand Up @@ -51,7 +51,7 @@ func NewCacheMap(opts ...MapOption) *CacheMap {

type mapConfig struct {
name string
logger *logrus.Logger
logger LoggerContract
headroom time.Duration
tokenFunc func(ctx context.Context, key string) (string, error)
}
Expand All @@ -69,7 +69,7 @@ func MapName(name string) MapOption {

// MapLogger sets the logger to be used.
// The default is the logrus default logger.
func MapLogger(logger *logrus.Logger) MapOption {
func MapLogger(logger LoggerContract) MapOption {
return func(c *mapConfig) {
c.logger = logger
}
Expand Down

0 comments on commit 8aca247

Please sign in to comment.