Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 连续解析日志文件时出现问题_Java - Fatal编程技术网

Java 连续解析日志文件时出现问题

Java 连续解析日志文件时出现问题,java,Java,我正在尝试连续读取日志文件,其中日志正在写入,这两个过程同时发生。我正在使用滚动文件追加器生成日志,但问题是,当文件即将更改时,有时在文件即将结束时写入的数据不会被读取。下面是我连续生成日志文件的代码 import java.util.Timer; import java.util.TimerTask; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public final class Slf4jSample {

我正在尝试连续读取日志文件,其中日志正在写入,这两个过程同时发生。我正在使用滚动文件追加器生成日志,但问题是,当文件即将更改时,有时在文件即将结束时写入的数据不会被读取。下面是我连续生成日志文件的代码

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){
}
}
import java.util.Timer;
导入java.util.TimerTask;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
公共最终类Slf4jSample{
静态记录器Logger=LoggerFactory.getLogger(Slf4jSample.class);
公共静态void main(最终字符串[]args){
int delay=0;//延迟5秒。
int period=1000;//每秒重复一次。
定时器=新定时器();
timer.scheduleAtFixedRate(新TimerTask(){
int i=0;
公开募捐{
//任务在这里。。。
对于(i=0;i<50;i++){
记录器错误(“测试”+i);
系统输出打印LN(i);
}
}
},延误,期限);
}
}
这是我一直在读的代码

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){
}
}
导入java.io.File;
导入java.io.IOException;
导入java.io.RandomAccessFile;
导入java.nio.channels.FileChannel;
导入java.nio.channels.FileLock;
导入java.util.ArrayList;
导入java.util.Date;
导入java.util.List;
导入java.util.Properties;
导入java.util.Timer;
导入java.util.TimerTask;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
导入javax.servlet.ServletContextEvent;
导入javax.servlet.ServletContextListener;
导入com.mobility.config.ParseConfig;
导入com.mobility.mail.SendMail;
导入com.mobility.vo.ConfigurationVO;
导入com.mobility.vo.LogVO;
导入javax.mail.*;
TimerTask fileWatcherTask=新日志观察程序(文件名){
long addFileLen=fileName.length();
FileChannel、oldChannel;
文件锁锁,oldLock;
@凌驾
受保护的void onChange(文件){
RandomAccessFile access=null;
RandomAccessFile oldAccess=null;
智力水平=0;
试一试{
access=新的随机访问文件(文件,“rw”);
oldAccess=新的随机访问文件(prevFileName,“rw”);
channel=access.getChannel();
lock=channel.lock();
oldChannel=oldAccess.getChannel();
oldLock=oldChannel.lock();
//System.out.println(“addFileLen>>”+addFileLen);

if(file.length()我会一直打开文件,直到它被完全读取,并且只有在您完成读取后才将其关闭。这可能需要您的记录器每次创建一个新文件,但您不太可能以这种方式遗漏任何内容。

如果您知道程序的行为不符合要求,我不会忽略IOException esp。您能告诉我在代码中的何处进行编辑吗d如何在文件更改后仍保持打开状态d如果不锁定文件,则可以在读取文件时对其进行更改。