0

I want to write a tool with Java to monitor a log file which is on the production server, and the log file is increasing from time to time.

The tool will get each new appended line from the log file, parse it and do something.

Is it possible to reading data from a live log file? If yes, how to do that?

Freewind
  • 193,756
  • 157
  • 432
  • 708
  • 2
    The following answer should help you. http://stackoverflow.com/questions/557844/java-io-implementation-of-unix-linux-tail-f – Neeraj Nov 01 '13 at 07:45
  • Any application that need to read its own log files has a much bigger problem. – user207421 Nov 01 '13 at 07:57

1 Answers1

0

There's various number of java file poller (file monitoring) libs out there (jPoller for example).

If you're using java 7, take a look at the WatchService API (part of NIO.2), which will notify you when the file you monitor changes.

If you are OK with using 3rd party libs, check out Apache's commons-vfs2 - DefaultFileMonitor

Example usage:
FileSystemManager fsManager = VFS.getManager();

FileObject listendir = fsManager.resolveFile("/home/username/monitored/");

DefaultFileMonitor fm = new DefaultFileMonitor(new CustomFileListener());
fm.setRecursive(true);
fm.addFile(listendir);

fm.start();

Community
  • 1
  • 1
hovanessyan
  • 30,580
  • 6
  • 55
  • 83