JavaLogger随机写入第二个文件 [英] JavaLogger randomly writes to a second file

查看:92
本文介绍了JavaLogger随机写入第二个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个罐子,大约每分钟都会被另一个脚本调用一次.在此jar中,我创建了一个JavaLogger,用于记录Jar运行时发生的情况. JavaLogger写入的日志文件称为myLog.0.我有以下代码允许它转到.1/.2/.3/.4.

I have a jar that is called about every minute from another script. In this jar I have created a JavaLogger that logs what happens while the Jar runs. The log file that JavaLogger writes to is called myLog.0. I have the following code to allow it to go to .1/.2/.3/.4.

try {
  FileHandler fileHandler = new FileHandler(filePath, 5242880, 5, true);
  fileHandler.setFormatter(new java.util.logging.Formatter() {
    @Override
    public String format(LogRecord logRecord) {
      return "[" + logRecord.getLevel() + " " + createDateTimeLog() + "]  " + logRecord.getMessage() + "\r\n";
    }
  });
  logger.addHandler(fileHandler);
} catch (IOException e) {}

因此,我希望日志会增长.但是,日志偶尔会打印到myLog.0.1.我猜这是因为文件被锁定了.但是,这永远不会发生在我罐子的中途.整个jar运行期间,它都记录为.0.1.该文件仍可以从我以前的运行中锁定吗?

So I expect the logs to grow. However, every once and a while the log will print to myLog.0.1. I would guess that this is because the file is locked. However, this never happens mid-run of my jar. It logs to .0.1 the entire time the jar runs. Could the file still be locked from my previous run?

如果是这样,我什至试图在Jar退出之前关闭处理程序.罐子只有一个出口点,我在其前面放了以下代码:

If so I have even tried to close the handler before the Jar exits. There is only one exit point from the jar and I put the following code right before it:

MyLogger.logger.getHandlers()[0].close();

我已经通过调试器运行了它,并且只有一个处理程序(我添加的FileHandler).

I have run this through the debugger and there is only ever one handler (the FileHandler that I add).

正如我所说,这只是随机发生的.广口瓶的前3个运行次数可以为.0,然后第四个运行为.0.1.那么接下来的10个可能又是正确的.很难说.但是它的确经常发生(我会说它大约每隔1/8就会写入.0.1.

As I said, this only happens randomly. The first 3 runs of the jar could be to .0 and then the fourth to .0.1. Then the next 10 could be correct again. It's hard to say. However it does happen fairly often (I would say it writes to .0.1 about every 1/8 of the time).

任何想法/建议都会很棒.提前谢谢.

Any ideas / suggestions would be great. Thanks ahead of time.

推荐答案

该文件是否仍可以从我之前的运行中锁定?

Could the file still be locked from my previous run?

可能是两个JVM同时运行您的jar.添加代码以获取 RuntimeMXBean ,然后添加单个日志语句以记录

Could be that two JVMs are running your jar at the same time. Add code to grab the RuntimeMXBean and then add a single log statement to record the runtime name and the start time. The runtime name usually maps to a process id and a host name.

FileHandler尽其所能防止两个同时运行的JVM写入同一日志文件.如果允许这种行为,则几乎无法读取和理解日志文件.

The FileHandler does everything it can to prevent two concurrently running JVMs from writing to the same log file. If this behavior was allowed the log file would be almost impossible to read and understand.

如果您真的想将所有内容写入一个日志文件,则必须执行以下操作之一:

If you really want to write everything to one log file then you have to do one of the following:

  1. 通过更改启动方式来防止并发JVM进程启动.
  2. 让您的代码检测其他JVM是否正在运行您的代码并在创建FileHandler之前退出.
  3. 让每个JVM写入一个不同的日志文件,并创建代码以安全地将这些文件合并为一个.
  4. 创建一个代理处理程序,该代理处理程序为每个日志记录创建并关闭FileHandler.代理处理程序将使用预定义的文件名(不同于日志文件)和FileLock来序列化来自不同JVM的对日志文件的访问.
  5. 使用专用进程写入日志文件,并让所有JVM将日志消息发送到该进程.

这篇关于JavaLogger随机写入第二个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆