连续解析日志文件的问题

时间:2011-12-28 12:53:09

标签: java

我正在尝试连续读取日志文件正在写入日志文件,这两个进程同时发生。我使用滚动文件appender生成日志但问题是,当文件有时会更改数据时不读取文件接近结束时写的。这是我的代码,它连续生成日志文件。

import java.util.Timer;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class Slf4jSample {

    static Logger logger = LoggerFactory.getLogger(Slf4jSample.class);

    public static void main(final String[] args) {
        int delay = 0; // delay for 5 sec. 

        int period = 1000;  // repeat every sec.
        Timer timer = new Timer();

        timer.scheduleAtFixedRate(new TimerTask() {
            int i = 0;

            public void run() {
                // Task here ...
                for (i = 0; i < 50; i++) {
                    logger.error("testing" + i);
                    System.out.println(i);
                }
            }
        }, delay, period);
    }
}

这是我的代码正在连续阅读。

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.mobility.config.ParseConfig;
import com.mobility.mail.SendMail;
import com.mobility.vo.ConfigurationVO;
import com.mobility.vo.LogVO;
import javax.mail.*;


            TimerTask fileWatcherTask = new LogWatcher(fileName) {

                long addFileLen = fileName.length();
                FileChannel channel, oldChannel;
                FileLock lock, oldLock;

                @Override
                protected void onChange(File file) {

                    RandomAccessFile access = null;
                    RandomAccessFile oldAccess = null;
                    int level = 0;
                    try {
                        access = new RandomAccessFile(file, "rw");
                        oldAccess = new RandomAccessFile(prevFileName, "rw");
                        channel = access.getChannel();
                        lock = channel.lock();
                        oldChannel = oldAccess.getChannel();
                        oldLock = oldChannel.lock();
//                      System.out.println("addFileLen>>" + addFileLen);

                        if (file.length() <= addFileLen) {
                            System.out.println("in if>>");
                            // reading old file

                            if (prevFileName.exists()) {
                                parsingLog(oldAccess, addFileLen,level);
                                addFileLen = file.length();
                            }
                            // reading old file ends

//                          Seek to 0th position when the file changes.
                            parsingLog(access, 0,level);
                        } else {
//                          access.seek(addFileLen);
                            parsingLog(access, addFileLen,level);
                            System.out.println("else>>>");
                            addFileLen = file.length();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {
                        lock.release();
                        oldLock.release();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    } // Close the file

                    try {
                        channel.close();
                        oldChannel.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };

            Timer timer = new Timer();
            // repeat the check every second
            long scheduleTimer = Long.parseLong(properties
                    .getProperty("scheduler.timer"));
            timer.schedule(fileWatcherTask, new Date(), scheduleTimer);
        }
    }

    public void parsingLog(RandomAccessFile access, long fileLength, int level) {

        List<String> logList = new ArrayList<String>();
        int flag = 0;
        String line = null;
        try {
            access.seek(fileLength);
            while ((line = access.readLine()) != null) {

                 System.out.println("after reading line >"+line);

                }

    }catch(Exception e){
}
}

PS:当内容即将结束时,它不会读取内容。

1 个答案:

答案 0 :(得分:1)

我会打开文件,直到它完全被读取,并且只有在你完成阅读后关闭它。这可能需要您的记录器每次都创建一个新文件,但您不可能错过这种方式。