Skip to content

Commit 5377a98

Browse files
committed
Move testing -> testr, deprecate, alias old names
This avoids the pkg name overlap with standard "testing".
1 parent 28755ae commit 5377a98

File tree

4 files changed

+182
-90
lines changed

4 files changed

+182
-90
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ with higher verbosity means more (and less important) logs will be generated.
105105
There are implementations for the following logging libraries:
106106

107107
- **a function** (can bridge to non-structured libraries): [funcr](https://github.com/go-logr/logr/tree/master/funcr)
108+
- **a testing.T** (for use in Go tests, with JSON-like output): [testr](https://github.com/go-logr/logr/tree/master/testr)
108109
- **github.com/google/glog**: [glogr](https://github.com/go-logr/glogr)
109110
- **k8s.io/klog** (for Kubernetes): [klogr](https://git.k8s.io/klog/klogr)
111+
- **a testing.T** (with klog-like text output): [ktesting](https://git.k8s.io/klog/ktesting)
110112
- **go.uber.org/zap**: [zapr](https://github.com/go-logr/zapr)
111113
- **log** (the Go standard library logger): [stdr](https://github.com/go-logr/stdr)
112114
- **github.com/sirupsen/logrus**: [logrusr](https://github.com/bombsimon/logrusr)

testing/test.go

Lines changed: 12 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -14,102 +14,24 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
// Package testing provides support for using logr in tests.
18+
// Deprecated. See github.com/go-logr/logr/testr instead.
1719
package testing
1820

19-
import (
20-
"testing"
21-
22-
"github.com/go-logr/logr"
23-
"github.com/go-logr/logr/funcr"
24-
)
21+
import "github.com/go-logr/logr/testr"
2522

2623
// NewTestLogger returns a logr.Logger that prints through a testing.T object.
27-
// Info logs are only enabled at V(0).
28-
func NewTestLogger(t *testing.T) logr.Logger {
29-
l := &testlogger{
30-
Formatter: funcr.NewFormatter(funcr.Options{}),
31-
t: t,
32-
}
33-
return logr.New(l)
34-
}
24+
// Deprecated. See github.com/go-logr/logr/testr.New instead.
25+
var NewTestLogger = testr.New
3526

3627
// Options carries parameters which influence the way logs are generated.
37-
type Options struct {
38-
// LogTimestamp tells the logger to add a "ts" key to log
39-
// lines. This has some overhead, so some users might not want
40-
// it.
41-
LogTimestamp bool
42-
43-
// Verbosity tells the logger which V logs to be write.
44-
// Higher values enable more logs.
45-
Verbosity int
46-
}
28+
// Deprecated. See github.com/go-logr/logr/testr.Options instead.
29+
type Options = testr.Options
4730

4831
// NewTestLoggerWithOptions returns a logr.Logger that prints through a testing.T object.
49-
// In contrast to the simpler NewTestLogger, output formatting can be configured.
50-
func NewTestLoggerWithOptions(t *testing.T, opts Options) logr.Logger {
51-
l := &testlogger{
52-
Formatter: funcr.NewFormatter(funcr.Options{
53-
LogTimestamp: opts.LogTimestamp,
54-
Verbosity: opts.Verbosity,
55-
}),
56-
t: t,
57-
}
58-
return logr.New(l)
59-
}
60-
61-
// Underlier exposes access to the underlying testing.T instance. Since
62-
// callers only have a logr.Logger, they have to know which
63-
// implementation is in use, so this interface is less of an
64-
// abstraction and more of a way to test type conversion.
65-
type Underlier interface {
66-
GetUnderlying() *testing.T
67-
}
68-
69-
type testlogger struct {
70-
funcr.Formatter
71-
t *testing.T
72-
}
73-
74-
func (l testlogger) WithName(name string) logr.LogSink {
75-
l.Formatter.AddName(name)
76-
return &l
77-
}
78-
79-
func (l testlogger) WithValues(kvList ...interface{}) logr.LogSink {
80-
l.Formatter.AddValues(kvList)
81-
return &l
82-
}
83-
84-
func (l testlogger) GetCallStackHelper() func() {
85-
return l.t.Helper
86-
}
87-
88-
func (l testlogger) Info(level int, msg string, kvList ...interface{}) {
89-
prefix, args := l.FormatInfo(level, msg, kvList)
90-
l.t.Helper()
91-
if prefix != "" {
92-
l.t.Logf("%s: %s", prefix, args)
93-
} else {
94-
l.t.Log(args)
95-
}
96-
}
97-
98-
func (l testlogger) Error(err error, msg string, kvList ...interface{}) {
99-
prefix, args := l.FormatError(err, msg, kvList)
100-
l.t.Helper()
101-
if prefix != "" {
102-
l.t.Logf("%s: %s", prefix, args)
103-
} else {
104-
l.t.Log(args)
105-
}
106-
}
107-
108-
func (l testlogger) GetUnderlying() *testing.T {
109-
return l.t
110-
}
32+
// Deprecated. See github.com/go-logr/logr/testr.NewWithOptions instead.
33+
var NewTestLoggerWithOptions = testr.NewWithOptions
11134

112-
// Assert conformance to the interfaces.
113-
var _ logr.LogSink = &testlogger{}
114-
var _ logr.CallStackHelperLogSink = &testlogger{}
115-
var _ Underlier = &testlogger{}
35+
// Underlier exposes access to the underlying testing.T instance.
36+
// Deprecated. See github.com/go-logr/logr/testr.Underlier instead.
37+
type Underlier = testr.Underlier

testr/testr.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
Copyright 2019 The logr Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package testr provides support for using logr in tests.
18+
package testr
19+
20+
import (
21+
"testing"
22+
23+
"github.com/go-logr/logr"
24+
"github.com/go-logr/logr/funcr"
25+
)
26+
27+
// New returns a logr.Logger that prints through a testing.T object.
28+
// Info logs are only enabled at V(0).
29+
func New(t *testing.T) logr.Logger {
30+
l := &testlogger{
31+
Formatter: funcr.NewFormatter(funcr.Options{}),
32+
t: t,
33+
}
34+
return logr.New(l)
35+
}
36+
37+
// Options carries parameters which influence the way logs are generated.
38+
type Options struct {
39+
// LogTimestamp tells the logger to add a "ts" key to log
40+
// lines. This has some overhead, so some users might not want
41+
// it.
42+
LogTimestamp bool
43+
44+
// Verbosity tells the logger which V logs to be write.
45+
// Higher values enable more logs.
46+
Verbosity int
47+
}
48+
49+
// NewWithOptions returns a logr.Logger that prints through a testing.T object.
50+
// In contrast to the simpler New, output formatting can be configured.
51+
func NewWithOptions(t *testing.T, opts Options) logr.Logger {
52+
l := &testlogger{
53+
Formatter: funcr.NewFormatter(funcr.Options{
54+
LogTimestamp: opts.LogTimestamp,
55+
Verbosity: opts.Verbosity,
56+
}),
57+
t: t,
58+
}
59+
return logr.New(l)
60+
}
61+
62+
// Underlier exposes access to the underlying testing.T instance. Since
63+
// callers only have a logr.Logger, they have to know which
64+
// implementation is in use, so this interface is less of an
65+
// abstraction and more of a way to test type conversion.
66+
type Underlier interface {
67+
GetUnderlying() *testing.T
68+
}
69+
70+
type testlogger struct {
71+
funcr.Formatter
72+
t *testing.T
73+
}
74+
75+
func (l testlogger) WithName(name string) logr.LogSink {
76+
l.Formatter.AddName(name)
77+
return &l
78+
}
79+
80+
func (l testlogger) WithValues(kvList ...interface{}) logr.LogSink {
81+
l.Formatter.AddValues(kvList)
82+
return &l
83+
}
84+
85+
func (l testlogger) GetCallStackHelper() func() {
86+
return l.t.Helper
87+
}
88+
89+
func (l testlogger) Info(level int, msg string, kvList ...interface{}) {
90+
prefix, args := l.FormatInfo(level, msg, kvList)
91+
l.t.Helper()
92+
if prefix != "" {
93+
l.t.Logf("%s: %s", prefix, args)
94+
} else {
95+
l.t.Log(args)
96+
}
97+
}
98+
99+
func (l testlogger) Error(err error, msg string, kvList ...interface{}) {
100+
prefix, args := l.FormatError(err, msg, kvList)
101+
l.t.Helper()
102+
if prefix != "" {
103+
l.t.Logf("%s: %s", prefix, args)
104+
} else {
105+
l.t.Log(args)
106+
}
107+
}
108+
109+
func (l testlogger) GetUnderlying() *testing.T {
110+
return l.t
111+
}
112+
113+
// Assert conformance to the interfaces.
114+
var _ logr.LogSink = &testlogger{}
115+
var _ logr.CallStackHelperLogSink = &testlogger{}
116+
var _ Underlier = &testlogger{}

testr/testr_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2021 The logr Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package testr
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
"github.com/go-logr/logr"
24+
)
25+
26+
func TestLogger(t *testing.T) {
27+
log := New(t)
28+
log.Info("info")
29+
log.V(0).Info("V(0).info")
30+
log.V(1).Info("v(1).info")
31+
log.Error(fmt.Errorf("error"), "error")
32+
log.WithName("testing").Info("with prefix")
33+
Helper(log, "hello world")
34+
35+
log = NewWithOptions(t, Options{
36+
LogTimestamp: true,
37+
Verbosity: 1,
38+
})
39+
log.V(1).Info("v(1).info with options")
40+
}
41+
42+
func Helper(log logr.Logger, msg string) {
43+
helper, log := log.WithCallStackHelper()
44+
helper()
45+
helper2(log, msg)
46+
}
47+
48+
func helper2(log logr.Logger, msg string) {
49+
helper, log := log.WithCallStackHelper()
50+
helper()
51+
log.Info(msg)
52+
}

0 commit comments

Comments
 (0)