File: capture.go

package info (click to toggle)
golang-github-go-log-log 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 120 kB
  • sloc: makefile: 2
file content (40 lines) | stat: -rw-r--r-- 872 bytes parent folder | download
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{},
	}
}