mirror of
https://github.com/golang/go
synced 2024-11-18 11:04:42 -07:00
internal/telemetry: add an example of using the logging behaviour
And also fix that the output was not actually correct! Change-Id: If81e22e586b1c2a71c69843b49d5dd8d5d2dfde6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/227298 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
This commit is contained in:
parent
ee2abff5cf
commit
ff0df58207
@ -70,6 +70,11 @@ func (ev Event) Format(f fmt.State, r rune) {
|
||||
}
|
||||
for it := ev.Tags(); it.Valid(); it.Advance() {
|
||||
tag := it.Tag()
|
||||
// msg and err were both already printed above, so we skip them to avoid
|
||||
// double printing
|
||||
if tag.Key == Msg || tag.Key == Err {
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(f, "\n\t%v", tag)
|
||||
}
|
||||
}
|
||||
|
37
internal/telemetry/export/log_test.go
Normal file
37
internal/telemetry/export/log_test.go
Normal file
@ -0,0 +1,37 @@
|
||||
// Copyright 2020 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package export_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"golang.org/x/tools/internal/telemetry/event"
|
||||
"golang.org/x/tools/internal/telemetry/export"
|
||||
)
|
||||
|
||||
func ExampleLog() {
|
||||
ctx := context.Background()
|
||||
event.SetExporter(timeFixer(export.LogWriter(os.Stdout, false)))
|
||||
anInt := event.NewIntKey("myInt", "an integer")
|
||||
aString := event.NewStringKey("myString", "a string")
|
||||
event.Print(ctx, "my event", anInt.Of(6))
|
||||
event.Error(ctx, "error event", errors.New("an error"), aString.Of("some string value"))
|
||||
// Output:
|
||||
// 2020/03/05 14:27:48 my event
|
||||
// myInt=6
|
||||
// 2020/03/05 14:27:48 error event: an error
|
||||
// myString="some string value"
|
||||
}
|
||||
|
||||
func timeFixer(output event.Exporter) event.Exporter {
|
||||
at, _ := time.Parse(time.RFC3339Nano, "2020-03-05T14:27:48Z")
|
||||
return func(ctx context.Context, ev event.Event, tagMap event.TagMap) context.Context {
|
||||
ev.At = at
|
||||
return output(ctx, ev, tagMap)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user