forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
56 lines (45 loc) · 1.32 KB
/
logger.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
package testcontainers
import (
"log"
"os"
"testing"
)
// Logger is the default log instance
var Logger Logging = log.New(os.Stderr, "", log.LstdFlags)
// Logging defines the Logger interface
type Logging interface {
Printf(format string, v ...interface{})
}
// 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) Logging {
tb.Helper()
return testLogger{TB: tb}
}
// WithLogger is a generic option that implements GenericProviderOption, DockerProviderOption and LocalDockerComposeOption
// It replaces the global Logging implementation with a user defined one e.g. to aggregate logs from testcontainers
// with the logs of specific test case
func WithLogger(logger Logging) LoggerOption {
return LoggerOption{
logger: logger,
}
}
type LoggerOption struct {
logger Logging
}
func (o LoggerOption) ApplyGenericTo(opts *GenericProviderOptions) {
opts.Logger = o.logger
}
func (o LoggerOption) ApplyDockerTo(opts *DockerProviderOptions) {
opts.Logger = o.logger
}
func (o LoggerOption) ApplyToLocalCompose(opts *LocalDockerComposeOptions) {
opts.Logger = o.logger
}
type testLogger struct {
testing.TB
}
func (t testLogger) Printf(format string, v ...interface{}) {
t.Helper()
t.Logf(format, v...)
}