-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
148 lines (118 loc) · 3.06 KB
/
logging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package jasper
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/tychoish/fun/erc"
"github.com/tychoish/jasper/options"
)
// LoggingCache provides an interface to a cache of loggers.
type LoggingCache interface {
Create(id string, opts *options.Output) (*options.CachedLogger, error)
Put(id string, logger *options.CachedLogger) error
Get(id string) *options.CachedLogger
// Remove removes an existing logger from the logging cache.
Remove(id string)
// CloseAndRemove closes and removes an existing logger from the
// logging cache.
CloseAndRemove(ctx context.Context, id string) error
// Clear closes and removes any remaining loggers in the logging cache.
Clear(ctx context.Context) error
// Prune removes all loggers that were last accessed before the given
// timestamp.
Prune(lastAccessed time.Time)
Len() int
}
// NewLoggingCache produces a thread-safe implementation of a logging
// cache for use in manager implementations.
func NewLoggingCache() LoggingCache {
return &loggingCacheImpl{
cache: map[string]*options.CachedLogger{},
}
}
type loggingCacheImpl struct {
cache map[string]*options.CachedLogger
mu sync.RWMutex
}
func (c *loggingCacheImpl) Create(id string, opts *options.Output) (*options.CachedLogger, error) {
c.mu.Lock()
defer c.mu.Unlock()
if _, ok := c.cache[id]; ok {
return nil, fmt.Errorf("logger named %s exists", id)
}
logger := opts.CachedLogger(id)
c.cache[id] = logger
return logger, nil
}
func (c *loggingCacheImpl) Len() int {
c.mu.RLock()
defer c.mu.RUnlock()
return len(c.cache)
}
func (c *loggingCacheImpl) Prune(ts time.Time) {
c.mu.Lock()
defer c.mu.Unlock()
for k, v := range c.cache {
if v.Accessed.Before(ts) {
delete(c.cache, k)
}
}
}
func (c *loggingCacheImpl) Get(id string) *options.CachedLogger {
c.mu.Lock()
defer c.mu.Unlock()
if _, ok := c.cache[id]; !ok {
return nil
}
item := c.cache[id]
item.Accessed = time.Now()
c.cache[id] = item
return item
}
func (c *loggingCacheImpl) Put(id string, logger *options.CachedLogger) error {
if logger == nil {
return errors.New("cannot cache nil logger")
}
c.mu.Lock()
defer c.mu.Unlock()
if _, ok := c.cache[id]; ok {
return fmt.Errorf("cannot cache with existing logger '%s'", id)
}
logger.Accessed = time.Now()
c.cache[id] = logger
return nil
}
func (c *loggingCacheImpl) Remove(id string) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.cache, id)
}
func (c *loggingCacheImpl) CloseAndRemove(_ context.Context, id string) error {
c.mu.Lock()
defer c.mu.Unlock()
var err error
logger, ok := c.cache[id]
if ok {
err = logger.Close()
delete(c.cache, id)
}
if err != nil {
return fmt.Errorf("problem closing logger with id %s: %w", id, err)
}
return nil
}
func (c *loggingCacheImpl) Clear(_ context.Context) error {
c.mu.Lock()
defer c.mu.Unlock()
catcher := &erc.Collector{}
for _, logger := range c.cache {
catcher.Add(logger.Close())
}
c.cache = map[string]*options.CachedLogger{}
if catcher.HasErrors() {
return fmt.Errorf("problem clearing logger cache: %w", catcher.Resolve())
}
return nil
}