Sunday, February 16, 2014

Java Log Tail with MongoDB


Recently I was working on a project to scan the log files and load some of the log messages to the database for further analysis. At first the idea was to upload the messages from the files at night assuming all the messages logged until 7 pm to be analyzed. Soon we found that this is not enough, what we need to do is upload the messages as they are logged, essentially we have to have a tail of logs in java. That is when I developed this solution, at the same time, I am learning Mongo so I developed a solution that would load the messages in the Mongodatabase.


Once we had this solution we started finding more use cases for it, e.g. write a test case for correct log messages.

The source code can be found at https://github.com/apadki/java-tail-mongo.git

This is how the code was built
  •  Set up listener to listen the file creation and change events
         A event listener for the files, to listen when a file is created and/or changed, for this used
         apache-commons-io.
          The FileAlterationObserverer observers the files in a particular folder:
          FileAlterationObserver observer = new FileAlterationObserver(folder);

          To the observe you pass a FileAlterationListner as follows, this the class where we write
         further code for tail.
             FileAlterationListener listener = new FileAlterationListenerImpl(
                fileNameQ, logMonitorConfig, activeFiles);
             observer.addListener(listener);

          The listener has callback method for FileCreate, FileChange, Delete etc.

          You also define the monitor, define the polling interval for the monitor, add the observer to the
           monitor and start it.

         FileAlterationMonitor monitor = new FileAlterationMonitor(2)
         monitor.addObserver(observer); try {
         monitor.start();
  • Processing new Line when it is logged 
          The FileAlterationListenerImpl does this job by launching two threads waiting on                  Blockingqueue.(FileNameQueue)
          When a file is changed, the file name is put on a blockingqueue, a thread FileNameConsumer waits on this queue.
          The FileNameConsumer keeps track of the location it read last for each file.
          It starts reading the file from the last location and puts every new line read on another blocking queue. (LogLineQueue)
          The thread LogLineConsumer reads a line and hands it over to processLine. ProcessLine is an interface, so one can plug in the logic for each line processing.
          The FileNameConsumer works as follows:
  • The LogTail when it starts along with the fileObservers, it also launches two threads.          FileNameConsumer fileNameConsumer = new FileNameConsumer(fileNameQ,     logLineQ, activeFiles, latch);                                                                                                    new Thread(fileNameConsumer, "File Name Consumer").start();    
          fileNameQ: The name of a file is put on this queue when a new file is created/changed.
          logLineQ : The queue on which FileNameConsumer puts a newly read line.
          activeFiles is a concurrentHashMap <String, Long> : the String is the fileName and the Long is the location from which the file will be read next time. When a file is added to the map first time the location is zero.
          latch : A countdown latch in case the threads are done.
      
         LogLineConsumer logLineConsumer = new LogLineConsumer(logLineQ,
               logMonitorConfig.getProcessLogLine(), latch);
        new Thread(logLineConsumer, "Log Line Consumer").start();
         
         The logLineConsumer is instantiated with the logLineQ from which it will take the line, it will pass the read line to the method processLine of the class logMonitorConfig.getProcessLogLine(). This class implements the interface processLine.

  • How the Project structured     
          Spring is used for configuration. 
          The configurations include :
                         logDir : The folder to be observed for the log files.
                         pollingInterval : The interval at which the file folder is polled for file changes.
                         mongodb URI and dbName
                         processLogLine: The class that will be used to process a line.
  •  How to Test the Project        
           Test with the MongoDB 
          1.  Change the MongoDB configuration in the spring-config.xml 
          2.  Start startLogWriter 
               This writes to a list of files (have to end in .log) that are configured in the spring-config.xml
               The message is constructed randomly, the fields separated by ";"
          3.  Start startLogTail
                

          Test without MongoDB
          1. Change the configuration for the processLine in the spring-config.xml
               There is a implementation of processLine that only prints the new line.
               Replace ProcessLogLineMongo with ProcessLogLineDisp in spring-config.xml