github.com/franciscocpg/up@v0.1.10/platform/lambda/logs/logs.go (about) 1 // Package logs implements AWS CloudWatchLogs tailing. 2 package logs 3 4 import ( 5 "time" 6 7 "github.com/apex/log" 8 "github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface" 9 ) 10 11 // Event is a single log event from a group. 12 type Event struct { 13 GroupName string 14 Message string 15 } 16 17 // Config is used to configure Logs and Log. 18 type Config struct { 19 Service cloudwatchlogsiface.CloudWatchLogsAPI 20 FilterPattern string 21 PollInterval time.Duration 22 StartTime time.Time 23 Follow bool 24 } 25 26 // Logs fetches or tails logs from CloudWatchLogs for any number of groups. 27 type Logs struct { 28 Config 29 GroupNames []string 30 err error 31 } 32 33 // Start consuming logs. 34 func (l *Logs) Start() <-chan *Event { 35 ch := make(chan *Event) 36 done := make(chan error) 37 38 for _, name := range l.GroupNames { 39 go l.consume(name, ch, done) 40 } 41 42 go func() { 43 l.wait(done) 44 close(ch) 45 }() 46 47 return ch 48 } 49 50 // Err returns the error, if any, during processing. 51 func (l *Logs) Err() error { 52 return l.err 53 } 54 55 // wait for each log group to complete. 56 func (l *Logs) wait(done <-chan error) { 57 for range l.GroupNames { 58 if err := <-done; err != nil { 59 l.err = err 60 return 61 } 62 } 63 } 64 65 // consume logs for group `name`. 66 func (l *Logs) consume(name string, ch chan *Event, done chan error) { 67 log := Log{ 68 Config: l.Config, 69 GroupName: name, 70 Log: log.WithField("group", name), 71 } 72 73 for event := range log.Start() { 74 ch <- event 75 } 76 77 done <- log.Err() 78 }