GithubHelp home page GithubHelp logo

Comments (3)

KaczDev avatar KaczDev commented on September 5, 2024

Hey, just had the same issue but I think I managed to resolve it.

Have you tried setting the Location when creating the Tail with config?
This is my conf and it doesn't read the file from the start. I only want to listen for the new logs.

t, err := tail.TailFile(catalinaPath,
	tail.Config{
		Follow:        true,
		CompleteLines: true,
		Location:      &tail.SeekInfo{Offset: 0, Whence: io.SeekEnd},
	})

from tail.

ouwe-knutselaar avatar ouwe-knutselaar commented on September 5, 2024

I also found this workaround:

 for line := range tailfile.Lines {
		pos, _ := tailfile.Tell()
		if pos != line.SeekInfo.Offset {
			continue
		}
		sendNewLogLine(line.Text)
	}

It works a little, however the last line in a truncated file is always send this eample.

from tail.

ouwe-knutselaar avatar ouwe-knutselaar commented on September 5, 2024

What also Could be done is to add an extra field in the config struct:
ToEndAfterTruncate bool // Go to the end of a file after a truncation to prevent double file read

And then add this piece of code to the function waitForChanges()

	case <-tail.changes.Truncated:
		// Always reopen truncated files (Follow is true)
		tail.Logger.Printf("Re-opening truncated file %s ...", tail.Filename)
		if err := tail.reopen(); err != nil {
			return err
		}
		tail.Logger.Printf("Successfully reopened truncated %s", tail.Filename)
		tail.openReader()
		
		// New snippet
		if tail.ToEndAfterTruncate {
			tail.Logger.Printf("Jump to the end of the file after being truncated %s", tail.Filename)
			if err := tail.seekEnd(); err != nil {
				return err
			}
		}
		// End

		return nil

Test it locally and it works really nice.

from tail.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.