为什么要记录" 1 MAIN INFO"当我通过.level = OFF禁用核心库的所有日志记录时,它不起作用。但是对于我的包,我启用了所有日志记录。为什么它只有在我有第二个记录器之后才能用字符串实例化?
LibClass libClass = new LibClass();
package com.mycompany;
public class Main {
private final static Logger logger = Logger.getLogger(Main.class.getName());
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("log.prop")
LogManager.getLogManager().readConfiguration(fis);
logger.info("1 MAIN INFO");
LibClass libClass = new LibClass();
libClass.doWork();
logger.info("3 MAIN INFO");
}
}
package com.mycompany;
public class LibClass {
private final static Logger logger = Logger.getLogger(LibClass.class.getName());
public void doWork() {
System.out.println("doWork");
logger.info("2 doWork INFO");
}}
handlers= java.util.logging.ConsoleHandler
.level = OFF
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
com.mycompany.level = ALL
输出
doWork
[Пт июл 01 22:37:29 EEST 2016] INFO: com.mycompany.LibClass doWork - 2 doWork INFO
[Пт июл 01 22:37:29 EEST 2016] INFO: com.mycompany.Main main - 3 MAIN INFO
答案 0 :(得分:0)
谢谢大家。感谢Andreas的正确答案。
问题在于: Logger.getLogger(Main.class.getName())在Logger加载配置之前加载类时执行 LogManager.getLogManager()。readConfiguration(fis)< / em>的
以下是解决方案
package com.mycompany;
public class Main {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("log.prop");
LogManager.getLogManager().readConfiguration(fis);
MainClass mainClass = new MainClass();
}
}
package com.mycompany;
public class MainClass {
private final static Logger logger = Logger.getLogger(MainClass.class.getName());
public MainClass() throws IOException {
logger.info("1 MAIN INFO");
LibClass libClass = new LibClass();
libClass.doWork();
URL url = new URL("http://google.ru");//I don't want see
url.openStream(); // fine, finest logs from this code
logger.info("3 MAIN INFO");
}
}
package com.mycompany;
public class LibClass {
private final static Logger logger = Logger.getLogger(LibClass.class.getName());
public void doWork() {
System.out.println("doWork");
logger.info("2 doWork INFO");
}
}
handlers= java.util.logging.ConsoleHandler
.level = OFF
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
com.mycompany.level = ALL
输出
[Сб июл 02 04:32:11 EEST 2016] INFO: com.mycompany.MainClass <init> - 1 MAIN INFO
doWork
[Сб июл 02 04:32:11 EEST 2016] INFO: com.mycompany.LibClass doWork - 2 doWork INFO
[Сб июл 02 04:32:12 EEST 2016] INFO: com.mycompany.MainClass <init> - 3 MAIN INFO