RandomAccessFile ...

I am trying to detect new error message in error log file.

- in an error log file, as there is a new error message logged. my

application will need to mail this error message to admin.

- I am thinking of using RandomAccessFile to do this.

- as there is more error message logged, I advance the File pointer,

and read the error message.

Can you give out some pesudo-code --> detect new lines written to the

log file. I had never use RandomAccessFile class before and don't know

how to use it.

[565 byte] By [ulukb00] at [2007-9-26 1:33:18]
# 1

I am using this code but doesn't work:

==============================================

code to email error msg to admin as more error logged to error log

file.

==============================================

import java.io.*;

public class random {

public static void main (String[] args) {

String thisLine;

try{

RandomAccessFile r = new RandomAccessFile

("d:/apache/logs/access.log", "r");

// infinite loop to poll error log for error

while ( true ){

// if has more error msg, then email

if ( (thisLine = r.readLine()) != null )

System.out.println(thisLine);

//send an email of error to admin

}

} catch ( Exception e ){

}

}

}

Thanks.

ulukb00 at 2007-6-29 1:35:37 > top of Java-index,Archived Forums,Java Programming...
# 2
to detect new error messages, you could poll the log file for its last modification time.
parthasarkar at 2007-6-29 1:35:37 > top of Java-index,Archived Forums,Java Programming...
# 3
On win32 (win2K), last modification time will not change if apache writes to log instead of user open the file and edit it.Any other better way except keep waiting for more data in an infinite / blocked loop? ( the code I had given above )Thanks.
ulukb00 at 2007-6-29 1:35:37 > top of Java-index,Archived Forums,Java Programming...
# 4

i tried this code, it works on winnt. try it and tell me if it doesn't work on win2k. i see no reason why it shouldn't.

it prints "file changed" whenever i access a html page (which writes about this to the access.log file).

import java.io.File;

public class FileChangePolling {

public static void main( String[] args ) throws Exception {

File file = new File("d:\\apache 1.3.20\\apache\\logs\\access.log");

long lastModified = file.lastModified();

long currentCheckedTime = 0;

while( true ) {

System.out.print(".");

currentCheckedTime = file.lastModified();

if( currentCheckedTime > lastModified ) {

System.out.println("file changed");

lastModified = currentCheckedTime;

} // end of if( )

else {

try {

Thread.sleep( 2000 );

}

catch( InterruptedException ie ) {

System.out.println("thread sleep interrupted: " + ie);

}

}

}

}

parthasarkar at 2007-6-29 1:35:37 > top of Java-index,Archived Forums,Java Programming...