Skip to content

Commit

Permalink
[Chore] Add a logger
Browse files Browse the repository at this point in the history
  • Loading branch information
maypok86 committed Aug 29, 2024
1 parent 44e3877 commit 198fc27
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
13 changes: 13 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Builder[K comparable, V any] struct {
weigher func(key K, value V) uint32
withWeight bool
deletionListener func(key K, value V, cause DeletionCause)
logger Logger
}

// NewBuilder creates a builder and sets the future cache capacity.
Expand All @@ -39,6 +40,7 @@ func NewBuilder[K comparable, V any](capacity int) *Builder[K, V] {
return 1
},
statsCollector: noopStatsCollector{},
logger: noopLogger{},
}
}

Expand Down Expand Up @@ -92,6 +94,14 @@ func (b *Builder[K, V]) WithVariableTTL() *Builder[K, V] {
return b
}

// Logger specifies the Logger implementation that will be used for logging warning and errors.
//
// Logging is disabled by default.
func (b *Builder[K, V]) Logger(logger Logger) *Builder[K, V] {
b.logger = logger
return b
}

func (b *Builder[K, V]) validate() error {
if b.capacity == nil || *b.capacity <= 0 {
return errors.New("otter: not valid capacity")
Expand All @@ -105,6 +115,9 @@ func (b *Builder[K, V]) validate() error {
if b.statsCollector == nil {
return errors.New("otter: stats collector should not be nil")
}
if b.logger == nil {
return errors.New("otter: logger should not be nil")
}
if b.ttl != nil && *b.ttl <= 0 {
return errors.New("otter: ttl should be positive")
}
Expand Down
6 changes: 6 additions & 0 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ func TestBuilder_NewFailed(t *testing.T) {
if err == nil {
t.Fatalf("should fail with an error")
}

// nil logger
_, err = NewBuilder[int, int](capacity).Logger(nil).Build()
if err == nil {
t.Fatalf("should fail with an error")
}
}

func TestBuilder_BuildSuccess(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type Cache[K comparable, V any] struct {
policy *s3fifo.Policy[K, V]
expiryPolicy expiryPolicy[K, V]
stats statsCollector
logger Logger
stripedBuffer []*lossy.Buffer[K, V]
writeBuffer *queue.Growable[task[K, V]]
evictionMutex sync.Mutex
Expand Down Expand Up @@ -134,6 +135,7 @@ func newCache[K comparable, V any](b *Builder[K, V]) *Cache[K, V] {
nodeManager: nodeManager,
hashmap: hashmap,
stats: newStatsCollector(b.statsCollector),
logger: b.logger,
stripedBuffer: stripedBuffer,
writeBuffer: queue.NewGrowable[task[K, V]](minWriteBufferSize, maxWriteBufferSize),
doneClear: make(chan struct{}),
Expand Down
30 changes: 30 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2024 Alexey Mayshev. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package otter

import "context"

// Logger is the interface used to get log output from otter.
type Logger interface {
// Warn logs a message at the warn level with an error.
Warn(ctx context.Context, msg string, err error)
// Error logs a message at the error level with an error.
Error(ctx context.Context, msg string, err error)
}

type noopLogger struct{}

func (nl noopLogger) Warn(ctx context.Context, msg string, err error) {}
func (nl noopLogger) Error(ctx context.Context, msg string, err error) {}

0 comments on commit 198fc27

Please sign in to comment.