Ejemplo n.º 1
0
Archivo: log.go Proyecto: dgruber/ugego
// CreateChannel opens a logfile and creates a channel of log
// Entry structs. It keeps the file open and scans it for new
// log file entries forever.
func CreateChannel(file string) (chan Entry, error) {
	lineCh := make(chan Entry, 1024)

	if t, err := tail.TailFile(file, tail.Config{Follow: true}); err == nil {
		// start tailing the logfile in background
		go func() {
			// writing to a closed channel panics - hence
			// we need to recover here since closing is our
			// "official" way to stop tailing
			defer func() {
				t.Stop()
				recover()
			}()
			for line := range t.Lines {
				if entry, errParse := ParseLine(line.Text); errParse == nil {
					// blocking when buffer is full
					lineCh <- entry
				}
			}
		}()
	} else {
		return nil, err
	}
	return lineCh, nil
}
Ejemplo n.º 2
0
func tailFile(filename string, config tail.Config, done chan bool) {
	defer func() { done <- true }()
	t, err := tail.TailFile(filename, config)
	if err != nil {
		fmt.Println(err)
		return
	}
	for line := range t.Lines {
		fmt.Println(line.Text)
	}
	err = t.Wait()
	if err != nil {
		fmt.Println(err)
	}
}