Skip to content

Commit 0849a56

Browse files
authored
Merge pull request #301 from dgrisonnet/traces-verbosity
Gate traces behind log level 2
2 parents 4693a02 + be5eaf9 commit 0849a56

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

trace/trace.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (t *Trace) Log() {
192192
t.endTime = &endTime
193193
t.lock.Unlock()
194194
// an explicit logging request should dump all the steps out at the higher level
195-
if t.parentTrace == nil { // We don't start logging until Log or LogIfLong is called on the root trace
195+
if t.parentTrace == nil && klogV(2) { // We don't start logging until Log or LogIfLong is called on the root trace
196196
t.logTrace()
197197
}
198198
}

trace/trace_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
func init() {
3232
klog.InitFlags(flag.CommandLine)
3333
flag.CommandLine.Lookup("logtostderr").Value.Set("false")
34+
flag.CommandLine.Lookup("v").Value.Set("2")
3435
}
3536

3637
func TestStep(t *testing.T) {
@@ -140,6 +141,7 @@ func TestLog(t *testing.T) {
140141
fields []Field
141142
expectedMessages []string
142143
sampleTrace *Trace
144+
verbosity klog.Level
143145
}{
144146
{
145147
name: "Check the log dump with 3 msg",
@@ -177,13 +179,35 @@ func TestLog(t *testing.T) {
177179
},
178180
sampleTrace: fieldsTraceFixture(),
179181
},
182+
{
183+
name: "Check that logs are not dumped if verbosity < 2",
184+
verbosity: 1,
185+
expectedMessages: []string{},
186+
sampleTrace: fieldsTraceFixture(),
187+
},
180188
}
181189

182190
for _, test := range tests {
183191
t.Run(test.name, func(t *testing.T) {
184192
var buf bytes.Buffer
185193
klog.SetOutput(&buf)
194+
195+
if test.verbosity > 0 {
196+
orig := klogV
197+
klogV = func(l klog.Level) bool {
198+
return l <= test.verbosity
199+
}
200+
defer func() {
201+
klogV = orig
202+
}()
203+
}
204+
186205
test.sampleTrace.Log()
206+
207+
if len(test.expectedMessages) == 0 && buf.Len() != 0 {
208+
t.Errorf("\nNo message expected in trace log: \n%v\n", buf.String())
209+
}
210+
187211
for _, msg := range test.expectedMessages {
188212
if !strings.Contains(buf.String(), msg) {
189213
t.Errorf("\nMsg %q not found in log: \n%v\n", msg, buf.String())

0 commit comments

Comments
 (0)