Golang github.com-ActiveState-tail.TailFile类(方法)实例源码

下面列出了Golang github.com-ActiveState-tail.TailFile 类(方法)源码代码实例,从而了解它的用法。

作者:way-2-g    项目:gostrea   
func ReadFile(filename string, stream chan<- string) {
	seekinfo := tail.SeekInfo{Whence: os.SEEK_END}
	t, _ := tail.TailFile(filename, tail.Config{Follow: true, Location: &seekinfo})
	for line := range t.Lines {
		stream <- line.Text + "\n"
	}
}

作者:dbenqu    项目:maglogparse   
func runAgent(aSender sender, fileToTail string) {
	// prepare filter
	compileFilters()

	if len(fileToTail) > 0 {
		// keep watching for the file
		for {
			if t, err := tail.TailFile(fileToTail, tail.Config{Follow: true, MustExist: true}); err != nil {
				fmt.Printf("Error in receive: %v\n", err)
			} else {
				//filterAndSend(zmqSender, t.Lines)
				filterAndBulkSend(aSender, t.Lines)
			}

			// the reopen option is not working in the TailFile config. Let's reopen after 1s in case of file lost
			timer := time.NewTimer(time.Second * 1)
			<-timer.C
		}
	} else { //read from stdin
		fmt.Printf("Reading from STDIN")
		lines := make(chan *tail.Line)
		go func(lines chan *tail.Line) {
			bio := bufio.NewReader(os.Stdin)
			for {
				line, _, err := bio.ReadLine()
				lines <- &(tail.Line{string(line), time.Now(), err})
			}
		}(lines)

		filterAndBulkSend(aSender, lines)
	}

}

作者:proni    项目:logsen   
func NewFile(fpath string) (*File, error) {
	seekInfo := &tail.SeekInfo{Offset: 0, Whence: 2}
	file := &File{}
	var err error
	file.Tail, err = tail.TailFile(fpath, tail.Config{Follow: true, ReOpen: true, Location: seekInfo})
	return file, err
}

作者:rajthilakmc    项目:gul   
func tailLog(cs chan []byte, filePath string, w io.Writer, closechan chan bool) {
	t, _ := tail.TailFile(filePath, tail.Config{Follow: true})
	for line := range t.Lines {
		fmt.Fprintf(w, line.Text)
	}
	closechan <- true
}

作者:jcftan    项目:logentries-g   
func main() {
	var token = flag.String("token", "nil", "log token")
	var logfile = flag.String("logfile", "/tmp/foo.txt", "log file to follow")
	var seekInfoOnStart = &tail.SeekInfo{Offset: 0, Whence: os.SEEK_END}

	flag.Parse()

	fmt.Println("using token: ", *token)

	if _, err := os.Stat(*logfile); os.IsNotExist(err) {
		fmt.Printf("no such file or directory: %s\n", *logfile)
		return
	}

	le, err := le_go.Connect(*token) // replace with token
	if err != nil {
		panic(err)
	}

	defer le.Close()
	t, err := tail.TailFile(*logfile, tail.Config{Follow: true, ReOpen: true, Location: seekInfoOnStart, Logger: tail.DiscardingLogger})
	if err == nil {
		for line := range t.Lines {
			le.Println(line.Text)
		}
	}
}

作者:dockwar    项目:dockeriz   
func tailFile(ctx context.Context, file string, dest *os.File) {
	defer wg.Done()
	t, err := tail.TailFile(file, tail.Config{
		Follow: true,
		ReOpen: true,
		//Poll:   true,
		Logger: tail.DiscardingLogger,
	})
	if err != nil {
		log.Fatalf("unable to tail %s: %s", "foo", err)
	}

	// main loop
	for {
		select {
		// if the channel is done, then exit the loop
		case <-ctx.Done():
			t.Stop()
			tail.Cleanup()
			return
		// get the next log line and echo it out
		case line := <-t.Lines:
			fmt.Fprintln(dest, line.Text)
		}
	}
}

作者:BrendanThompso    项目:remote_syslog   
// Tails a single file
func tailOne(file string, excludePatterns []*regexp.Regexp, logger *syslog.Logger, wr *WorkerRegistry, severity syslog.Priority, facility syslog.Priority, poll bool) {
	defer wr.Remove(file)
	wr.Add(file)
	tailConfig := tail.Config{ReOpen: true, Follow: true, MustExist: true, Poll: poll, Location: &tail.SeekInfo{0, os.SEEK_END}}

	t, err := tail.TailFile(file, tailConfig)

	if err != nil {
		log.Errorf("%s", err)
		return
	}

	for line := range t.Lines {
		if !matchExps(line.Text, excludePatterns) {
			logger.Packets <- syslog.Packet{
				Severity: severity,
				Facility: facility,
				Time:     time.Now(),
				Hostname: logger.ClientHostname,
				Tag:      path.Base(file),
				Message:  line.Text,
			}
			log.Tracef("Forwarding: %s", line.Text)
		} else {
			log.Tracef("Not Forwarding: %s", line.Text)
		}

	}

	log.Errorf("Tail worker executed abnormally")
}

作者:mj    项目:logstalke   
func main() {
	flag.Parse()
	config := loadConfig()

	// Determine the parsing function for this log file
	parserFn, err := parsingFunctionForType(config.Parser)
	if err != nil {
		log.Fatal(err)
	}

	// Connect to BigQuery & create tables
	tabledataService := connectToBigquery(&config)

	// Start tailing the log file & parsing the entries
	seek := tail.SeekInfo{Offset: 0, Whence: 2}
	t, _ := tail.TailFile(config.LogFilename, tail.Config{
		Location: &seek,
		Follow:   true,
		Logger:   tail.DiscardingLogger,
	})

	for line := range t.Lines {
		parsed, err := parserFn(config.Host, strings.Replace(line.Text, "\\", "", -1))
		if err == nil {
			go stream(&config, tabledataService, parsed)
		}
	}
}

作者:hgfische    项目:nginx2statha   
// MAIN
func main() {
	parseCommandLine()

	t, err := tail.TailFile(accessLog, tail.Config{Follow: true, ReOpen: true, MustExist: false})
	if err != nil {
		panic(err)
	}

	hits := make(chan *loghit.LogHit)
	defer close(hits)
	errors := make(chan error)
	defer close(errors)

	for i := 0; i < *parserRoutines; i++ {
		go parseLines(t.Lines, hits, errors)
	}

	for i := 0; i < *posterRoutines; i++ {
		go postStats(*statPrefix, ezKey, hits)
	}

	logWriter, err := syslog.New(syslog.LOG_ERR, "nginx2stathat")
	if err != nil {
		panic(err)
	}

	for err := range errors {
		logWriter.Err(err.Error())
	}
}

作者:CloudSid    项目:dei   
func tailFile(path string) {
	mkfifo(path)
	t, _ := tail.TailFile(path, tail.Config{Follow: true})

	for line := range t.Lines {
		log.Info(line.Text)
	}
}

作者:DrTal    项目:hearthstone-helpe   
func main() {
	hsLogFile := flag.String("log", "no-log-file-specified", "The file path to the Hearthstone log file.")
	hsUsername := flag.String("username", "no-username-specified", "Your battlenet ID (without the #1234).")

	flag.Parse()

	createManaUpdateParser(*hsUsername)
	log, _ := tail.TailFile(*hsLogFile, tail.Config{Follow: true})

	gs := GameState{}
	gs.resetGameState()
	solutionChan := make(chan *DecisionTreeNode)
	seenUsername := false
	var deepestSolution, shortestSolution *DecisionTreeNode
	var abortChan *chan time.Time
	for {
		select {
		case line := <-log.Lines:
			if !seenUsername && strings.Contains(line.Text, *hsUsername) {
				seenUsername = true
			}
			if turnStart, somethingHappened := ParseHearthstoneLogLine(line.Text, &gs); turnStart || somethingHappened {
				if !seenUsername {
					fmt.Println("WARN: Waiting to see --username before looking for solutions.")
					continue
				}
				//fmt.Println("It is the start of turn for:", gs.LastManaAdjustPlayer)
				if abortChan != nil {
					*abortChan <- time.Now()
					abortChan = nil
					deepestSolution = nil
					shortestSolution = nil
				}
				newAbortChan := make(chan time.Time, 1)
				abortChan = &newAbortChan
				go WalkDecisionTree(gs.DeepCopy(), solutionChan, newAbortChan)
			}
		case solution := <-solutionChan:
			if deepestSolution == nil {
				deepestSolution = solution
				shortestSolution = solution
				fmt.Println("INFO: Solution found")
				prettyPrintDecisionTreeNode(solution)
			}
			if len(deepestSolution.Moves) < len(solution.Moves) {
				deepestSolution = solution
				fmt.Println("INFO: Another solution with more BM:")
				prettyPrintDecisionTreeNode(solution)
			}
			if len(shortestSolution.Moves) > len(solution.Moves) {
				shortestSolution = solution
				fmt.Println("INFO: Another solution with fewer steps:")
				prettyPrintDecisionTreeNode(solution)
			}
		}
	}
}

作者:kf8    项目:raingauge-loade   
func fileToTail(fileName string) *tail.Tail {
	tail, err := tail.TailFile(fileName, tail.Config{
		Follow: true,
		ReOpen: true,
	})
	if err != nil {
		log.Fatal(err)
	}
	return tail
}

作者:yutop    项目:subak   
func liveStatus(c web.C, w http.ResponseWriter, r *http.Request) {
	log.Printf("Running Task Id => %s\n", c.URLParams["id"])
	id, err := strconv.ParseInt(c.URLParams["id"], 10, 32)
	if err != nil {
		http.Error(w, "Invalid id", http.StatusInternalServerError)
		return
	}

	runningTask := gSubakoCtx.RunningTasks.Get(int(id))
	if runningTask == nil {
		http.Error(w, "task is nil", http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	w.Header().Set("X-Content-Type-Options", "nosniff") // important

	w.WriteHeader(http.StatusOK)

	flusher, ok := w.(http.Flusher)
	if !ok {
		// ERROR
		http.Error(w, "Failed to cast to http.Flusher", http.StatusInternalServerError)
	}

	t, err := tail.TailFile(runningTask.LogFilePath, tail.Config{Follow: true})
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// finish when timeout
	go func() {
		time.Sleep(time.Duration(60) * time.Second) // timeout: 60sec
		t.Stop()
	}()
	// finish when task finished
	go func() {
		for {
			if !runningTask.IsActive() {
				t.Stop()
				break
			}
			time.Sleep(time.Duration(1) * time.Second)
		}
	}()
	// show logs
	for line := range t.Lines {
		fmt.Fprintln(w, line.Text)
		flusher.Flush() // Trigger "chunked" encoding and send a chunk...
	}

	fmt.Fprintf(w, "Current Status => %s\n", runningTask.Status)
	log.Printf("Task %d has been finished!!!!", runningTask.Id)
}

作者:mschuet    项目:go-log2gel   
// readLogsFromFile reads log lines from file and send them to `queue`
// notify `shutdown` when file is completely read
func readLogsFromFile(fname string, queue chan<- Logline, shutdown chan<- string, savestate <-chan bool) {
	var statefile string
	var offset int64
	var inode uint64
	var doFollowFile bool = !*options.nofollow

	if *options.verbose {
		log.Printf("readLogsFromFile: dofollow=%v", doFollowFile)
	}

	if doFollowFile {
		statefile = fname + ".state"
		inode = readFileInode(fname)
		offset = readStateFile(fname, statefile, inode)
	}

	// setup
	config := tail.Config{
		Follow:    doFollowFile,
		ReOpen:    doFollowFile,
		MustExist: true,
		Logger:    tail.DiscardingLogger,
		Location: &tail.SeekInfo{
			Offset: offset,
			Whence: 0,
		},
	}
	t, err := tail.TailFile(fname, config)
	if err != nil {
		shutdown <- fmt.Sprintf("cannot tail file %s: %v", fname, err)
	} else if *options.verbose {
		log.Printf("opened log file %s", fname)
	}

	// now just sleep and wait for input and control channel
	for {
		select {
		case line := <-t.Lines:
			if line != nil {
				queue <- Logline(line.Text)
			} else {
				shutdown <- "Logfile closed"
				return
			}
		case <-savestate:
			offset, _ := t.Tell()
			if doFollowFile {
				writeStateFile(statefile, inode, offset)
			}
			if *options.verbose {
				log.Printf("reading %s, now at offset %d", fname, offset)
			}
		}
	}
}

作者:danielchatfiel    项目:orchestr   
func TailServiceLog(service *services.Service, wg *sync.WaitGroup) {
	defer wg.Done()
	spacingLength := services.MaxServiceNameLength + 2 - len(service.Name)
	t, err := tail.TailFile(service.LogFilePath, tail.Config{Follow: true})
	if err != nil {
		log.Error(err.Error())
	}
	for line := range t.Lines {
		logReceiver <- fmt.Sprintf("@{%s}%[email protected]{|}%s|  %s\n", service.Color, service.Name, strings.Repeat(" ", spacingLength), line.Text)
	}
}

作者:segue    项目:logrepor   
func main() {
	t, err := tail.TailFile("/tmp/test/log.log", tail.Config{Follow: true})
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	for line := range t.Lines {
		processLine(line)
	}
}

作者:rosetear    项目:logsen   
// 创建File对象
func NewFile(fpath string) (*File, error) {
	file := &File{}
	var err error
	// 判断读取方式
	if Conf.ReadWholeLog && Conf.ReadOnce {
		// 读整个文件并且读一次
		Conf.Logger.Printf("read whole file once %+v", fpath)
		file.Tail, err = tail.TailFile(fpath, tail.Config{})
	} else if Conf.ReadWholeLog {
		// 读整个文件和增量内容
		Conf.Logger.Printf("read whole file and continue %+v", fpath)
		file.Tail, err = tail.TailFile(fpath, tail.Config{Follow: true, ReOpen: true})
	} else {
		// 从文件末尾开始读取
		// offset 文件指针的位置
		// whence 相对位置标识: 0代表相对文件开始的位置,1代表相对当前位置,2代表相对文件结尾的位置
		seekInfo := &tail.SeekInfo{Offset: 0, Whence: 2}
		file.Tail, err = tail.TailFile(fpath, tail.Config{Follow: true, ReOpen: true, Location: seekInfo})
	}
	return file, err
}

作者:delatec    项目:htai   
func (t *Tailer) AddFile(filename string) error {
	if _, ok := t.files[filename]; ok {
		return nil
	}

	config := tail.Config{Location: &tail.SeekInfo{Whence: os.SEEK_END}, Follow: true}
	tf, err := tail.TailFile(filename, config)
	if err != nil {
		return err
	}
	t.files[filename] = tf
	return nil
}

作者:fsta    项目:exim_prometheus_exporte   
func tailFile(logfile string, readall bool) (*tail.Tail, error) {
	whence := os.SEEK_END
	if readall {
		whence = os.SEEK_SET
	}
	return tail.TailFile(logfile, tail.Config{
		MustExist: true,                      // Fail early if the file does not exist
		ReOpen:    true,                      // Reopen recreated files (tail -F)
		Follow:    true,                      // Continue looking for new lines (tail -f)
		Logger:    tail.DiscardingLogger,     // Disable logging
		Location:  &tail.SeekInfo{0, whence}, // Start at the beginning or end of the file?
	})
}

作者:artiri    项目:fal   
func Filewatcher(comp Component, conf Configuration, outgoing *fifo.Queue) {
	// tail a given file, add appended lines into a Message queue
	t, _ := tail.TailFile(comp.File, tail.Config{Follow: true})
	for line := range t.Lines {
		outgoing.Add(&Message{
			Project:   conf.Project,
			Env:       conf.Env,
			Component: comp.Name,
			Text:      line.Text,
			Timestamp: time.Now().Unix(),
		})
	}
}


问题


面经


文章

微信
公众号

扫码关注公众号