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
|
// Package capture implements the Logger interface by capturing logged
// lines. This is useful for log inspection during unit-testing,
// if you want to assert that a particular line has, or has not, been
// logged.
package capture
import (
"fmt"
"sync"
)
// Logger implements the log.Logger interface by capturing logged
// lines.
type Logger struct {
mutex *sync.Mutex
// Entries holds logged entries in submission order.
Entries []string
}
func (logger *Logger) Log(v ...interface{}) {
logger.log(fmt.Sprint(v...))
}
func (logger *Logger) Logf(format string, v ...interface{}) {
logger.log(fmt.Sprintf(format, v...))
}
func (logger *Logger) log(entry string) {
logger.mutex.Lock()
defer logger.mutex.Unlock()
logger.Entries = append(logger.Entries, entry)
}
func New() *Logger {
return &Logger{
mutex: &sync.Mutex{},
Entries: []string{},
}
}
|