问题描述
我想拥有1个配置文件,其中包含有关特定类中的记录器应该如何记录/如何记录/记录的信息.
I'd like to have 1 configuration file that contains information about how/where/what logger in particular class should log.
示例:
Foo类
package myApp;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
class Foo {
static final Logger logger = Logger.getLogger(Foo.class.getName());
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("C:\\path\\to\\myApp.log.properties");
LogManager.getLogManager(). readConfiguration(fis);
logger.log(Level.INFO, "Msg message");
Bar obj = new Bar();
obj.doSth();
}
班级酒吧
package myApp;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Bar {
static final Logger logger = Logger.getLogger(Bar.class.getName());
public Bar() {
}
public void doSth() {
logger.log(Level.WARNING, "Msg message");
}
}
文件myApp.log.properties
File myApp.log.properties
handlers =
config =
myApp.Foo.handlers = java.util.logging.ConsoleHandler
myApp.Foo.useParentHandlers = false
myApp.Foo.level = INFO
myApp.Bar.handlers = java.util.logging.ConsoleHandler
myApp.Bar.useParentHandlers = false
myApp.Bar.level = INFO
输出:
Jul 23, 2014 1:27:12 PM myApp.Bar doSth
WARNING: Msg message
如您所见,缺少Foo的日志记录,我想这是因为logger是静态的,并且是在加载和设置LogManager的配置之前创建的.
As you can see, the log record for Foo is missing, i guess it's because logger is static and is created before configuration for LogManager is loaded and set.
能否请您提出为Foo的记录器打印日志记录的解决方案?还使用静态记录器,并且在执行程序时使用命令行参数-D configFile
不使用吗?
Could you please suggest solution where log record is printed for Foo's logger? Also with a static logger and without using commandline parameter -D configFile
when the program is executed?
推荐答案
在您的示例中,调用了LogingToFile.doSth
而不是Bar.doSth
.修改您的配置文件以包括:
In your example, LogingToFile.doSth
is called instead of Bar.doSth
. Modify your config file to include:
myApp.LogingToFile.handlers = java.util.logging.ConsoleHandler
myApp.LogingToFile.useParentHandlers = false
myApp.LogingToFile.level = INFO
或将new Bar().doSth();
添加到您的主要方法中.
Or add new Bar().doSth();
to your main method.
通读LogManager
源代码,似乎仅在设置配置后创建记录器时才加载Handlers
.这是错误 JDK-8033661readConfiguration不会干净地重新初始化日志记录系统.列出的解决方法是:
Reading through the LogManager
source code it appears that Handlers
are only loaded when the logger is created after the configuration is set. This is bug JDK-8033661readConfiguration does not cleanly reinitialize the logging system. The listed workarounds are:
鉴于您的限制,解决方法是在创建记录器之前先加载配置.
Given your constraints, the workaround will be to load the configuration before creating the loggers.
class Foo {
static {
try (FileInputStream fis = new FileInputStream("C:\\path\\to\\myApp.log.properties")) {
LogManager.getLogManager().readConfiguration(fis);
} catch (IOException ex) {
throw new ExceptionInInitializerError(ex);
}
}
static final Logger logger = Logger.getLogger(Foo.class.getName());
public static void main(String[] args) throws IOException {
logger.log(Level.INFO, "Msg message");
Bar obj = new Bar();
obj.doSth();
}
}
这篇关于通过全局配置文件配置记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!