java log4j记录_Java日志记录开放“太多”日志文件

bd96500e110b49cbb3cd949968f18be7.png

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...

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"));

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());

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.

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

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.

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.

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 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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值