尝试同时读取时,Java滚动文件创建失败 [英] Java Rolling File Creation Fails when attempting to read simultaneously
问题描述
我正在使用java.util日志记录类来创建滚动文件追加器.我想创建一个日志读取器,在将数据写入它们时从这些日志中读取数据.
I am using java.util logging classes to create a rolling file appender. I want to create a log reader that reads from these logs as data is written to them.
滚动日志追加程序代码本身可以正常工作.但是,一旦启动阅读器线程,就不会创建新文件,即,如果滚动日志附加程序设置为使用5个文件,它将创建1og.0,log.1,log.2等,但是如果阅读器线程启动,那么它将仅创建create log.0没有创建其他文件.我在Java日志记录和log4j中都注意到了这一点.
The rolling log appender code works fine on its own. But once I start the reader thread new files are not created i.e. if rolling log appender is set to use 5 files it will create 1og.0, log.1, log.2 and so on but if the reader thread starts then it will only create log.0 no other files are created. I notice this in both java logging and log4j.
我正在使用nio读取日志读取器.所以我的疑问是,是否在同一文件上创建另一个FileChannel会产生一些问题?还是我错过了一些导致问题的基本nio内容.
I am using nio to read in the log reader. So my doubt is, is creating another FileChannel on the same file creating some problem? or have I missed some basic nio stuff here that is causing problems.
这是代码.
public class Test {
private static final long initialTime = System.currentTimeMillis();
private static Logger logger = Logger.getLogger(Test.class.getName());
public static void main(String[] args) {
logger.setLevel(Level.INFO);
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(
new File(System.getProperty("user.dir")+File.separator+"input.dat"))));
FileHandler handler = new FileHandler("log/test.log", 1024, 5, true);
handler.setFormatter(new SimpleFormatter());
logger.addHandler(handler);
logger.setUseParentHandlers(false);
//If I comment the next two lines the rolling appender works as expected both in java logging
//and when using log4j. however once I start the log reader only one file is created.
Thread logReader = new Thread(new LogReader("log/test.log.0"));
logReader.start();
while(reader.ready())
{
String strToLog = reader.readLine();
logger.info(strToLog);
//Only want to run this for 10 secs.
if(System.currentTimeMillis() - initialTime > 10000)
System.exit(0);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
}
}
}
class LogReader implements Runnable {
private final int BUFFER_SIZE = 1024;
private RandomAccessFile file;
private FileChannel chan;
private long offset;
private ByteBuffer buf;
private Charset charset = Charset.forName("UTF-8");
private String filename = "output.log";
public LogReader(String logfile) throws IOException {
System.out.println("Starting log reader from " + logfile);
file = new RandomAccessFile(logfile, "r");
offset = file.length();
chan = file.getChannel();
chan.position(offset);
buf = ByteBuffer.allocateDirect(BUFFER_SIZE);
System.out.println("Started log reader from " + logfile);
}
public void run() {
//Even if I have nothing here..the problem exists..
}
}
推荐答案
我认为RandomAccessFile
可能存在一些并发问题.
I think there might be some concurrency issues with RandomAccessFile
.
我建议您使用其他类来读取文件.例如. java.io.FileReader
I suggest you use a different class for reading the file. E.g. java.io.FileReader
这篇关于尝试同时读取时,Java滚动文件创建失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!