org.apache.qpid.util
Class LogMonitor

java.lang.Object
  extended by org.apache.qpid.util.LogMonitor

public class LogMonitor
extends Object

Utility to simplify the monitoring of Log4j file output Monitoring of a given log file can be done alternatively the Monitor will add a new log4j FileAppender to the root Logger to gather all the available logging for monitoring


Constructor Summary
LogMonitor()
          Create a new LogMonitor that creates a new Log4j Appender and monitors all log4j output via the current configuration.
LogMonitor(File file)
          Create a new LogMonitor on the specified file if the file does not exist or the value is null then a new Log4j appender will be added and monitoring set up on that appender.
 
Method Summary
 void close()
          Stop monitoring this file.
 List<String> findMatches(String pattern)
          Checks the log for instances of the search string.
 File getMonitoredFile()
          Return a File reference to the monitored file
 String readFile()
          Read the log file in to memory as a String
 void reset()
          Clears the log file and writes: 'Log Monitor Reset' at the start of the file
 List<String> waitAndFindMatches(String message, long wait)
          Checks the log file for a given message to appear and returns all instances of that appearance.
 boolean waitForMessage(String message, long alertLogWaitPeriod)
           
 boolean waitForMessage(String message, long wait, boolean printFileOnFailure)
          Checks the log file for a given message to appear.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LogMonitor

public LogMonitor()
           throws IOException
Create a new LogMonitor that creates a new Log4j Appender and monitors all log4j output via the current configuration.

Throws:
IOException - if there is a problem creating the temporary file.

LogMonitor

public LogMonitor(File file)
           throws IOException
Create a new LogMonitor on the specified file if the file does not exist or the value is null then a new Log4j appender will be added and monitoring set up on that appender. NOTE: for the appender to receive any value the RootLogger will need to have the level correctly configured.ng

Parameters:
file - the file to monitor
Throws:
IOException - if there is a problem creating a temporary file
Method Detail

waitAndFindMatches

public List<String> waitAndFindMatches(String message,
                                       long wait)
                                throws FileNotFoundException,
                                       IOException
Checks the log file for a given message to appear and returns all instances of that appearance.

Parameters:
message - the message to wait for in the log
wait - the time in ms to wait for the message to occur
Returns:
true if the message was found
Throws:
FileNotFoundException - if the Log file can nolonger be found
IOException - thrown when reading the log file

findMatches

public List<String> findMatches(String pattern)
                         throws IOException
Checks the log for instances of the search string. The pattern parameter can take any valid argument used in String.contains() String.contains(CharSequences)

Parameters:
pattern - the search string
Returns:
a list of matching lines from the log
Throws:
IOException - if there is a problem with the file

waitForMessage

public boolean waitForMessage(String message,
                              long wait,
                              boolean printFileOnFailure)
                       throws FileNotFoundException,
                              IOException
Checks the log file for a given message to appear.

Parameters:
message - the message to wait for in the log
wait - the time in ms to wait for the message to occur
printFileOnFailure - should we print the contents that have been read if we fail ot find the message.
Returns:
true if the message was found
Throws:
FileNotFoundException - if the Log file can nolonger be found
IOException - thrown when reading the log file

waitForMessage

public boolean waitForMessage(String message,
                              long alertLogWaitPeriod)
                       throws FileNotFoundException,
                              IOException
Throws:
FileNotFoundException
IOException

readFile

public String readFile()
                throws FileNotFoundException,
                       IOException
Read the log file in to memory as a String

Returns:
the current contents of the log file
Throws:
FileNotFoundException - if the Log file can nolonger be found
IOException - thrown when reading the log file

getMonitoredFile

public File getMonitoredFile()
Return a File reference to the monitored file

Returns:
the file being monitored

reset

public void reset()
           throws FileNotFoundException,
                  IOException
Clears the log file and writes: 'Log Monitor Reset' at the start of the file

Throws:
FileNotFoundException - if the Log file can nolonger be found
IOException - thrown if there is a problem with the log file

close

public void close()
Stop monitoring this file. This is required to be called incase we added a new logger. If we don't call close then the new logger will continue to get log entries after our desired test has finished.



Licensed to the Apache Software Foundation