github.com/blend/go-sdk@v1.20220411.3/examples/logger/drain/main.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package main 9 10 import ( 11 "context" 12 "fmt" 13 "math/rand" 14 "os" 15 "time" 16 17 "github.com/blend/go-sdk/logger" 18 ) 19 20 func main() { 21 log := logger.MustNew(logger.OptAll()) 22 23 log.Listen(logger.Info, "randomly_slow", func(ctx context.Context, e logger.Event) { 24 if rand.Float64() < 0.2 { 25 fmt.Println("randomly slow start") 26 time.Sleep(2000 * time.Millisecond) 27 fmt.Println("randomly slow stop") 28 } 29 }) 30 31 infoSignal := time.Tick(100 * time.Millisecond) 32 33 done := time.After(10 * time.Second) 34 35 for { 36 select { 37 case <-infoSignal: 38 log.Infof("this is an info event") 39 case <-done: 40 fmt.Println("draining") 41 func() { 42 ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second) 43 defer cancel() 44 log.DrainContext(ctx) 45 }() 46 fmt.Println("exiting") 47 os.Exit(0) 48 } 49 } 50 }