blob: d20e90a05a812f1d21078a7aeb7d014df4343efa (
plain)
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
|
package log
import (
"log"
"os"
"strings"
"testing"
)
// Validate our types implement the required interfaces.
var (
_ Logger = (*log.Logger)(nil)
_ Logger = (*noopLogger)(nil)
_ Logger = (*testLogger)(nil)
)
// Logger defines the Logger interface.
type Logger interface {
Printf(format string, v ...any)
}
// defaultLogger is the default Logger instance.
var defaultLogger Logger = &noopLogger{}
func init() {
// Enable default logger in the testing with a verbose flag.
if testing.Testing() {
// Parse manually because testing.Verbose() panics unless flag.Parse() has done.
for _, arg := range os.Args {
if strings.EqualFold(arg, "-test.v=true") || strings.EqualFold(arg, "-v") {
defaultLogger = log.New(os.Stderr, "", log.LstdFlags)
}
}
}
}
// Default returns the default Logger instance.
func Default() Logger {
return defaultLogger
}
// SetDefault sets the default Logger instance.
func SetDefault(logger Logger) {
defaultLogger = logger
}
func Printf(format string, v ...any) {
defaultLogger.Printf(format, v...)
}
type noopLogger struct{}
// Printf implements Logging.
func (n noopLogger) Printf(_ string, _ ...any) {
// NOOP
}
// TestLogger returns a Logging implementation for testing.TB
// This way logs from testcontainers are part of the test output of a test suite or test case.
func TestLogger(tb testing.TB) Logger {
tb.Helper()
return testLogger{TB: tb}
}
type testLogger struct {
testing.TB
}
// Printf implements Logging.
func (t testLogger) Printf(format string, v ...any) {
t.Helper()
t.Logf(format, v...)
}
|