1
0
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:
Ian Cottrell 2020-04-04 23:16:06 -04:00
parent ee2abff5cf
commit ff0df58207
2 changed files with 42 additions and 0 deletions

View File

@ -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)
}
}

View 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)
}
}