为什么日志"1 MAIN INFO"不工作,当我禁用所有日志从核心库。level=OFF。但是对于我的包,我启用了所有日志记录。为什么它只工作后,我有第二个记录器是由字符串实例化?
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
谢谢大家。谢谢Andreas的正确回答。
问题在这里:Logger. getlogger (Main.class.getName())在Logger加载配置之前加载类时执行LogManager.getLogManager(). readconfiguration (fis).
解决方案
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