Java日志记录开放“太多”日志文件 [英] Java logging opening "too many" logfiles

查看:248
本文介绍了Java日志记录开放“太多”日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点奇怪,但我是日志包及其属性的新手。我通过谷歌搜索找到的所有问题都是如何让日志记录打开多个文件?但我今天的问题是如何让它停止同时执行多个文件。在这里我们去...

This is a bit weird, but I'm new to the logging package and its use of properties. All the questions I can find by googling are "how to make logging open multiple files?" but my question today is how to make it stop doing multiple files simultaneously. Here we go...

首先要做的事情:这个项目仅限于使用java.util.logging,不能我不能切换到log4j或任何其他第三个 - 派对套餐,是的,我知道他们应该更加棒极了。 : - )

First things first: this project is restricted to using java.util.logging, no I can't switch to log4j or any other third-party package, yes I know they're supposed to be more awesome. :-)

所以当这个小程序启动时,它运行这段代码:

So when this applet starts up, it runs this bit of code:

import java.util.logging.Logger;
import java.util.logging.LogManager;

// in startup routine:
LogManager.getLogManager().readConfiguration(
  this.getClass().getResourceAsStream("/logging.properties"));

从JAR中提取属性文件并应用它们,这有效。 readConfiguration()应该重置VM启动时的所有现有设置。项目的其余部分包含类似

to pull out the properties file out of a JAR and apply them, which works. The readConfiguration() is supposed to reset all existing settings from the VM startup. The rest of the project has lines like

private final static Logger LOGGER = Logger.getLogger(NameOfClass.class.getName());

我相信这是非常标准的。我们所有的类都在同一个包中(例如称之为TheProject),而时髦的日志名称/属性层次结构遵循相同的约定,因为这就是java.util.logging喜欢滚动的方式。

which I believe is pretty standard. All our classes are in the same package (call it TheProject for example), and the funky logging names/property hierarchy follows the same convention because that's just how java.util.logging likes to roll.

logging.properties文件作为Java 6 SE JRE附带的副本开始,然后进行了修改。现在它看起来像这样:

The logging.properties file started off as a copy of the one that ships with the Java 6 SE JRE and then got modified. Now it looks like this:

handlers=java.util.logging.FileHandler,java.util.logging.ConsoleHandler

# Default global logging level. 
.level=INFO

# Loggers 
# ------------------------------------------ 
# Loggers are usually attached to packages. 
# Here, the level for each package is specified. 
# The global level is used by default, so levels 
# specified here simply act as an override. 
java.level = INFO
javax.swing.level = INFO
sun.awt.level = INFO
theproject.level = ALL

# Handlers 
# -----------------------------------------
theproject.handlers=java.util.logging.FileHandler

# Override of global logging level 
java.util.logging.FileHandler.level=ALL

# Naming style for the output file: 
java.util.logging.FileHandler.pattern=/path/to/logfiles/TheProject%u.%g.log

所有工作,因为日志消息显示在Java控制台中,也出现在磁盘文件中。这是奇怪的部分:一旦applet运行,两个文件同时打开,包括TheProject0.0.log和TheProject1.0.log。当日志消息被触发时,它们同时显示在两个文件中。两个文件始终是彼此的精确副本,包括达到最大大小并且(都是!)旋转。

All that "works", in that the log messages show up in the Java console and also appear in the disk files. Here's the weird part: as soon as the applet runs, two files are opened at the same time, both TheProject0.0.log and TheProject1.0.log. When log messages are fired off, they show up in both files simultaneously. Both files are an exact copy of each other at all times, including when the max size is reached and are (both!) rotated.

只有一个JRE VM正在运行我一次检查过。

There is only one JRE VM running at a time, I checked.

在任何给定时间,两个文件都被打开,或两者都关闭,我检查过。它不像是一个比另一个更长或更短的时间打开。

At any given time, either both files are opened, or both are closed, I checked. It's not like one is open for longer or shorter time than the other.

两个文件名之间不同的%u标记记录为解决冲突的唯一编号 如果日志文件已被另一个进程打开,但我认为这不是这种情况,因为两个日志都获得相同的数据而没有其他任何打开文件。 (证据:当VM运行时,Windows不会让我删除任何一个文件,但是一旦VM最终退出,它就会被删除。)

The %u token that's different between the two filenames is documented as "a unique number to resolve conflicts" in case the logfile is already open by another process, but I think that's not the case here, since both logs are getting identical data and nothing else opens the file. (evidence: Windows won't let me delete either file while the VM is running, but it will once the VM finally exits.)

我做了一些愚蠢的事情属性文件,或者误解了如何正确加载属性,或者......?

Am I doing something stupid in the properties file, or misunderstanding how to load the properties correctly, or...?

推荐答案

@Joop将其钉住(在评论中) ,不能标记为答案)。事实证明,设置处理程序是添加过程,它不会简单地覆盖以前的设置。这对我来说似乎真的不直观,但那是你的java.util.logging ...留下所有其他属性,但删除处理程序赋值,是要走的路。

@Joop nailed it (in a comment, which can't be marked as the answer). It turns out that setting a handler is an additive process, it does not simply overwrite the previous settings. That seems really unintuitive to me, but that's java.util.logging for ya... Leaving all the other properties in place, but removing the handler assignment, was the way to go.

我也在使用@jschoen建议的reset()调用,因为这似乎是一个聪明的事情!

I'm also using the reset() call suggested by @jschoen because that just seems to be a smart thing to do!

非常感谢所有您。我不确定如何将这一切标记为已关闭...时间来摆弄网站。

Many thanks to all of you. I'm not sure how to mark this all as "closed"... time to fiddle with the site some.

这篇关于Java日志记录开放“太多”日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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