Apache Commons IO Tailer example
Asked Answered
C

2

18

I am working on a monitoring program that reads the /var/log/auth.log file. I am using Apache Commons IO Tailer class to read the file in real time. To get started, I wanted to test the real-time reading part on a simple file, and manually enter some code in the console line. Here is my code:

public class Main {
    public static void main(String[] args) {
        TailerListener listener = new MyListener();
        Tailer tailer = Tailer.create(new File("log.txt"), listener, 500);
        while(true) {

        }
    }
}

public class MyListener extends TailerListenerAdapter {
    @Override
    public void handle(String line) {
        System.out.println(line);
    }
}

And from the terminal : sudo echo "Hello" >> log.txt The problem is when I try to write manually something in the file, it does not print it in the console. I tried to find a concrete example of usage of Tailer class, but no luck. What am I doing wrong here?

Citystate answered 10/4, 2014 at 11:49 Comment(3)
while(true) {} is never a good idea. Are you launching the MyListener in a separate thread? You should at least add a Thread.sleep(...) to your loop.Margetmargette
I'm not sure why you got a down-vote. Seems a clear question with code that demonstrates the problem.Sieve
could you please rate it? I tried to make it clear, expose the problem, and show the sample of my code. I respected stackoverflow's conditionsCitystate
S
20

Based on my testing, Tailer will only print a line when you've added a newline to the file. So try sudo echo "Hello\n" >> log.txt

Also note that if you call create, you start a thread but have no handle on it. Hence why you had to have a while/true loop.

You could try this instead:

public static void main(String[] args) {
    TailerListener listener = new MyListener();
    Tailer tailer = new Tailer(new File("log.txt"), listener, 500);        
    tailer.run();
}
Sieve answered 10/4, 2014 at 12:12 Comment(3)
OK, so I've done what you said, and it is working. One thing though, i prints the output twice. for instance, if I try to write in the terminal echo "Hello" >> log.txt, it will print "Hello" twice in the console. Why is that?Citystate
@Citystate I would guess that's because you've still got Tailer tailer = Tailer.create rather than Tailer tailer = new Tailer? That mistake would cause you to have two threads running, each printing to stdout.Sieve
Thank you very much, that solved my problem. I thought that using create is the only way to create a new instance. I had the feeling that the constructor is not public, so this is why I used tha Factory method using create. Now it works perfectly. Now I can pass to the next step which is parsing. Thanks a lot! Cheers!Citystate
D
1

Your code should work. For me, this does works as expected.

package de.lhorn.stackoverflowplayground;

import java.io.File;
import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListenerAdapter;

public class App {

    private static final int SLEEP = 500;

    public static void main(String[] args) throws Exception {
        App app = new App();
        app.run();
    }

    private void run() throws InterruptedException {
        MyListener listener = new MyListener();
        Tailer tailer = Tailer.create(new File("/tmp/log.txt"), listener, SLEEP);
        while (true) {
            Thread.sleep(SLEEP);
        }
    }

    public class MyListener extends TailerListenerAdapter {

        @Override
        public void handle(String line) {
            System.out.println(line);
        }

    }
}
Didi answered 10/4, 2014 at 12:27 Comment(1)
I managed to make it work, my problem now is that it prints every line twice, how cand I solve this?Citystate

© 2022 - 2024 — McMap. All rights reserved.