禁用 java 日志轮转

发布于 2024-08-06 15:55:53 字数 437 浏览 11 评论 0原文

我使用 java.util.logging.Logger 登录我的应用程序:

    FileHandler fh=new FileHandler(this.todayFileName, 0, 1, true);

    fh.setFormatter(new SimpleFormatter());

    Logger.getLogger(rootLogger.getName()).setLevel(Level.ALL);

    Logger.getLogger(rootLogger.getName()).addHandler(fh); 

但这确实有效,除了启用了日志轮换之外。

我得到文件:

run.log run.log.1 run.log.2

我想要的是只获取一个日志文件,并且不启用旋转。

我该怎么做?

I use java.util.logging.Logger to log in my app :

    FileHandler fh=new FileHandler(this.todayFileName, 0, 1, true);

    fh.setFormatter(new SimpleFormatter());

    Logger.getLogger(rootLogger.getName()).setLevel(Level.ALL);

    Logger.getLogger(rootLogger.getName()).addHandler(fh); 

but this does work well except that log rotation is enabled.

and i get files :

run.log run.log.1 run.log.2

what I want is to get only one log file, with no rotation enabled.

how do I do that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

老旧海报 2024-08-13 15:55:53

如果您同时多次运行同一个应用程序,java.util.logging 将创建许多日志文件,例如 run.log run.log.1 run.log.2

并且您确定您的应用程序正确关闭,因为我已经遇到这个问题了。由于我的应用程序没有正确关闭,当我启动应用程序时,创建了 myApplication.log.1

If you run the same application more than once at the same time, java.util.logging will create many log file like run.log run.log.1 run.log.2

And are you sure that your application close correctly, because I already got this problem. Since my application didn't close correctly, when I started my application, that created a myApplication.log.1

临走之时 2024-08-13 15:55:53

您必须使用使用文件名和布尔值的 FileHandle 构造函数,或者仅使用文件名的构造函数,这样您将获得一个没有旋转的单个日志文件。

问候,
路易斯.

You must use the FileHandle constructor tht uses a file name and a boolean or the one that uses the file name only, in this way you are going to get a single log file with no rotation.

Regards,
Luis.

戈亓 2024-08-13 15:55:53

除了已经给出的答案之外,我还会质疑您禁用对数轮换的愿望。有充分的理由让日志轮转(例如磁盘空间不足等),因此 make 是一个标准程序。我想说的是调查一下是什么让你想要关闭它并消除这种需求。例如,如果您希望能够轻松地挖掘日志信息,也许您应该考虑寻找一个可以为您完成此操作的软件包(例如 splunk 或其他)并且可以与旋转日志一起正常工作。还要考虑日志轮换是高度可配置的,所以也许您只想更改轮换配置。另请考虑,您可以从为此目的发送到其他地方的日志中获取所需的信息,而无需考虑轮换日志...

并且根据操作系统/日志轮换配置系统,您可能可以关闭轮换就在操作系统中,不用担心您的代码。

Apart from the answers already given I would question your desire to disable logrotation. There are very good reasons to have logs rotate (like running out of disc space and so on) that make is a standard procedure. I would say investigate what makes you want to turn it off and eliminate that need. E.g. if you want to be able to mine the logs information easily maybe you should consider looking at a package that does that for you (e.g. splunk or whatever) and can work fine with rotated logs. Also consider that log rotation is highly configurable so maybe you just want to change the rotation config. Also consider that you could get the information you need from the log sent somewhere else for that purpose and leave the rotating log alone...

And depending on the operating system/log rotation configuration system you will probably be able to just turn the rotation off right there in the operating system and not worry about your code.

春风十里 2024-08-13 15:55:53

如果必须使用它,请尝试使用 Integer.MAX_INT 而不是 0。0 实际上可能被视为 0 字节,因此每次初始化它时都会导致旋转。

顺便说一句,如果你想要“只有一个文件,不旋转”,那么使用只接受文件名的 1-arg 构造函数,它会做正确的事情。

If you must use this, then try Integer.MAX_INT instead of 0. The 0 might actually be treated as 0 bytes, and thus cause a rotation every time you initialise it.

BTW if you want 'only one file, no rotation' then use the 1-arg constructor that just takes the file name, and it will do the right thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文