-1

Why logging "1 MAIN INFO" doesn't working when I disabled all logging from core libraries by .level=OFF. But for my package I am enabled all logging. Why it works only after I have second logger is instantiated by string?

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 

Output

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 
user6539479
  • 11
  • 1
  • 1
  • What out are your getting, and what did you expect. Show it (edit question, don't add in comment). – Andreas Jul 01 '16 at 19:38
  • Question has a link to screenshot with name "Output. And the code is very simple. What I want i wrote at first line "1 MAIN INFO" must be logged but not. – user6539479 Jul 01 '16 at 19:45
  • The logging.properties can only configure loggers that are demanded by name in the code. Currently you have 2 nodes which are the `root<-com.mycompany.Main` and `root<-com.mycompany.LibClass`. Declare and pin a `com.mycompany` logger and that will insert a common parent for both loggers. – jmehrens Jul 01 '16 at 20:28
  • Don't create the logger before you load the log configuration. Or use a better logging framework, e.g. [Log4j](http://logging.apache.org/log4j/2.x/) or [Logback](http://logback.qos.ch/). – Andreas Jul 01 '16 at 22:03

1 Answers1

0

Thank you all. Thank Andreas for correct answer.

The Problem was here: Logger.getLogger(Main.class.getName()) executes when class is loading before Logger loads configuration LogManager.getLogManager().readConfiguration(fis).

Here is solution

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 

Output

[Сб июл 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 
user6539479
  • 11
  • 1
  • 1