Continuously read lines added to the log file

As for my previous question , I found out that maven cannot really output the jboss console. So I thought I wanted to do this. Here is the deal:

While jboss is running, it writes console logs to the server.log file, so I try to get the data as it arrives, because every few seconds the file is changed / updated by jboss. I encountered some difficulties so I need help.

I really need:

  • read server.log file
  • when server.log is changed with a few lines added, output the change

Here is the code that I still received, there is a problem with it, it works indefinitely, and it starts every time from the very beginning of the file, I would like it to continue to print only new lines from server.log, I hope there is meaning:

import java.io.*;


class FileRead 
{
   public static void main(String args[])
  {
      try{
   for(;;){ //run indefinitely
    // Open the file 
    FileInputStream fstream = new FileInputStream("C:\\jboss-5.1.0.GA\\server\\default\\log\\server.log");
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    String strLine;
    //Read File Line By Line
    while ((strLine = br.readLine()) != null)   {
      // Print the content on the console
      System.out.println (strLine);
    }
    //Close the input stream
    in.close();
    }
  }
      catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
}

As suggested by Montecristo, I did this:

import java.io.*;

class FileRead {
    public static void main(String args[]) {
        try {
            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream(
                    "C:\\jboss-5.1.0.GA\\server\\default\\log\\server.log");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String line;
            // Read File Line By Line
            while ((line = br.readLine()) != null) {
                // Print the content on the console
                line = br.readLine();
                if (line == null) {
                    Thread.sleep(1000);
                } else {
                    System.out.println(line);
                }

            }
            // Close the input stream
            in.close();

        } catch (Exception e) {// Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
}

And it still doesn't work, it just prints the source file .. although the file is constantly changing, nothing happens .. nothing is printed except the original log file.

HERE DECISION: tnx Montecristo

import java.io.*;

class FileRead {
    public static void main(String args[]) {
        try {

            FileInputStream fstream = new FileInputStream(
                    "C:\\jboss-5.1.0.GA\\server\\default\\log\\server.log");

            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String line;

            while (true) {

                line = br.readLine();
                if (line == null) {
                    Thread.sleep(500);
                } else {
                    System.out.println(line);
                }

            }

        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}

See also:

http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html

+5
source share

All Articles