如何在Java中创建或配置FileHandler,以便在不同执行中记录我的日志逻辑?

huangapple 未分类评论44阅读模式
标题翻译

How to create or configure Filehandler in java to logic my logs in different executions?

问题

你可能会认为这是一个愚蠢的问题,但我确实没有找到针对它的规范。

我有一个小程序,需要在执行过程中记录成功和失败,并且我需要将日志与默认日志分开保存,以便每次都可以轻松读取。最初我放置了以下代码:

  1. Logger.getLogger("Success").addHandler(new FileHandler("var/success.log", true));

我的意图是拥有一个不断增长的日志,并且每次我执行该程序时,日志都会增长。但当我查看日志时,实际上每次执行该行时都会有很多<?xml version="1.0">头,即使没有保存日志。我很确定这不是它应该工作的方式,我不再拥有一个唯一的大日志,而是在一个单一文件中有很多这样的日志,切换到另一个格式化程序也不像是一个解决方案(我仍然需要XML)。

我目前正在寻找的一个解决方案是在每次执行时都打开一个新文件。
构造函数中的模式似乎对此没有帮助。%u 只测试是否已经打开或无法写入,但无论是否启用附加,现有文件都不会停止此处理程序。至于%g,它只在文件大小达到一定值时进行旋转,但我无法解决之前提到的日志问题。

是否有一种方法通过修改LoggerLogManager、构造函数或Filehandler来实现这一点?使用 Files(或 Path 或 File 或其他任何方式)检查每个组合是否打开日志记录器之前,自己找出特定的解决方案似乎并不高效,而且我真的不想为此而担心。

另外,是否有一种方法可以让 FileHander 实际读取文件输入并继续记录,而不是创建一个带有新标头的新文件?

尽管第二个问题更适合我的需求,但似乎更难实现(但我怎么知道呢?),所以我可以先解决第一个问题。

谢谢!

英文翻译

You may think it a stupid question, but surely I found no specification for it.

I have a mini-program that needs to log success and failures upon execution and I need the log to be kept apart from default ones so I can easily read it every time. Originally I put:

  1. Logger.getLogger(&quot;Success&quot;).addHandler(new FileHandler(&quot;var/success.log&quot;, true));

My intention was to have a growing log and that every time I executed the program, the log would be growing. When I look at the log, it actually just has a lot of <?xml version="1.0" > headers, once for each time I executed that line, even if no log was saved. I am pretty sure this isn't how it should work, I don't have 1 unique big log anymore but a lot of them in a single file, and switching to another formatter doesn't feel like a solution at all (i still need XML).

One solution I am currently searching for is to open a fresh file at every execution.
The Pattern in the constructor doesn't seem to help with this. %u only test for already open or impossible to write, but with or without append enabled, an existing file won't stop this handler. And for %g, a rotating, it just rotates on size but the same I don't identify the previous log problem still happens.

is there a way to get there by modifying the Logger, LogManager, the constructor or the Filehandler? A my-own and specific solution using Files (or Path or File or whatever) to check for every combination before opening the logger doesn't seem efficient and its something I really don't really want to worry about.

Also, is there a way to get the FileHander to actually read the file input and CONTINUE with the log where it was left, instead of creating a new one with new headers?

This seconds question, although it fits my needs better, seems harder to get (but what would I know?), so I can work with the first one.

Thanks!

答案1

得分: 0

> 最初我放置了:
>
> Logger.getLogger("Success").addHandler(new FileHandler("var/success.log", true));

在您的JUL编码中,您可能希望改进的一些事项包括:

  1. 保留日志记录器的静态最终硬引用
  2. 对于文件处理器,使用绝对路径或解析为绝对路径的令牌
  3. 使用静态代码块,这样您就不会为日志记录器创建并添加多个文件处理器。

> 我的意图是拥有一个增长的日志,并且每次我执行程序时,日志都会增长。当我查看日志时,实际上每次我执行该行时都会有很多标题,即使没有保存日志。我很确定这不是它应该工作的方式,我不再拥有一个唯一的大日志,而是在单个文件中有很多日志,切换到另一个格式化程序似乎根本不是一个解决方案(我仍然需要XML)。
>
> [snip]
>
> 另外,有没有办法让FileHandler实际上读取文件输入并在之前的日志位置继续记录,而不是创建一个带有新标题的新文件?

这就是它的工作方式。造成问题和解决方案在这个SO问题中有记录。这里有一个CSV的代码示例,但对于XML也适用。扩展XMLFormatter并重写getHead和getTail方法。

> 我目前正在寻找的一个解决方案是在每次执行时打开一个新文件。构造函数中的模式似乎对此没有帮助。%u只测试已打开或无法写入,但是无论是否启用追加,现有文件都不会阻止此处理程序。对于%g,旋转只会根据大小旋转,但我仍然无法解决之前的日志问题。

您正在从代码中创建FileHandler,因此可以编写逻辑来创建唯一的文件名。

  1. new FileHandler("var/success" + System.nanoTime() + ".log", true);
英文翻译

> Originally I put:
>
> Logger.getLogger(&quot;Success&quot;).addHandler(new FileHandler(&quot;var/success.log&quot;, true));

Some things you'll want to improve in your JUL coding are:

  1. Hold on to the logger in a static final hard reference.
  2. Use an absolute path or a token that resolves to an absolute path for the filehandler.
  3. Use a static block so you don't create and add multiple file handlers to the logger.

> My intention was to have a growing log and that every time I executed the program, the log would be growing. When I look at the log, it actually just has a lot of headers, once for each time I executed that line, even if no log was saved. I am pretty sure this isn't how it should work, I don't have 1 unique big log anymore but a lot of them in a single file, and switching to another formatter doesn't feel like a solution at all (i still need XML).
>
> [snip]
>
> Also, is there a way to get the FileHander to actually read the file input and CONTINUE with the log where it was left, instead of creating a new one with new headers?

That is how it works. The cause and solution are documented in this SO question. Here is a code example for CSV but would work the same for XML. Extend the XMLFormatter and override getHead and getTail.

> One solution I am currently searching for is to open a fresh file at every execution. The Pattern in the constructor doesn't seem to help with this. %u only test for already open or impossible to write, but with or without append enabled, an existing file won't stop this handler. And for %g, a rotating, it just rotates on size but the same I don't identify the previous log problem still happens.

You are creating the FileHandler from code so you can simply code up logic to create a unique file name yourself.

  1. new FileHandler(&quot;var/success&quot; + System.nanoTime() + &quot;.log&quot;, true);

huangapple
  • 本文由 发表于 2020年3月16日 19:11:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/60704877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定