github.com/v2fly/tools@v0.100.0/internal/event/export/log_test.go (about) 1 // Copyright 2020 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package export_test 6 7 import ( 8 "context" 9 "errors" 10 "os" 11 "time" 12 13 "github.com/v2fly/tools/internal/event" 14 "github.com/v2fly/tools/internal/event/core" 15 "github.com/v2fly/tools/internal/event/export" 16 "github.com/v2fly/tools/internal/event/keys" 17 "github.com/v2fly/tools/internal/event/label" 18 ) 19 20 func ExampleLog() { 21 ctx := context.Background() 22 event.SetExporter(timeFixer(export.LogWriter(os.Stdout, false))) 23 anInt := keys.NewInt("myInt", "an integer") 24 aString := keys.NewString("myString", "a string") 25 event.Log(ctx, "my event", anInt.Of(6)) 26 event.Error(ctx, "error event", errors.New("an error"), aString.Of("some string value")) 27 // Output: 28 // 2020/03/05 14:27:48 my event 29 // myInt=6 30 // 2020/03/05 14:27:48 error event: an error 31 // myString="some string value" 32 } 33 34 func timeFixer(output event.Exporter) event.Exporter { 35 at, _ := time.Parse(time.RFC3339Nano, "2020-03-05T14:27:48Z") 36 return func(ctx context.Context, ev core.Event, lm label.Map) context.Context { 37 copy := core.CloneEvent(ev, at) 38 return output(ctx, copy, lm) 39 } 40 }