0
votes

I am having problems getting logging to print out in some of my constructors. With others, logging is working just as I would expect.

I have attempted to eliminate every possible source of confusion by going with defaults, and by explicitly setting the log levels within the class itself. I've tried instantiating the logger as both a static and instance variable.

But, even setting the logging level to "Level.ALL" doesn't seem to be working in some of the classes.

For instance loggers, I have been setting the logging level within an instance-initializer / anonymous block. For the static instance, I am using the static block to set the logging level.

Any ideas why the log messages aren't printing out in the classes below?

Note: Code has been edited to show the fix, based on comment by @jmehrens

public abstract class ReadFileInformation extends SimpleFileVisitor<Path> {
   static public ConsoleHandler globalConsoleHandler = new ConsoleHandler(); // fix, based on comment by @jmehrens
   static private final Logger logger = Logger.getLogger(ReadFileInformation.class.getName());
   static { // fix: See comment by @jmehrens
      globalConsoleHandler.setLevel(Level.ALL);
      logger.addHandler(globalConsoleHandler);
   }
   {
      logger.setLevel(Level.ALL);
      logger.log(Level.FINEST, String.format("This does NOT print.\n"));
      System.out.println("In ReadFileInformation Static Block.  This prints out!");
   }
}
public class ReadMetadateFileInformation extends ReadFileInformation {
   static private final Logger logger = Logger.getLogger(ReadMetadateFileInformation.class.getName());
   static {
      logger.setLevel(Level.ALL);
      logger.log(Level.FINEST, String.format("This does NOT print.\n"));
      System.out.println("In ReadMetadateFileInformation Static Block.  This prints out!");
   }
   {
      logger.log(Level.FINEST, String.format("This does NOT print.\n"));
      System.out.println("In ReadMetadateFileInformation Anonymous Block.  This prints out!");
   }
   public ReadMetadateFileInformation() {
      super();
      logger.log(Level.FINE, String.format("This does NOT print.\n"));
   }
}
1
System.out.printing has nothing to do with Logger. I am also guessing that this has nothing to do with subclassing either - basically (the code as posting) does not Log I suggest that you make a very very easy class with just a main as get that printing.Scary Wombat
The only point of adding the System.out.print was to prove to myself that the code had actually reached at that point in the code, and then was ignoring the logging statement. Adding the ConsoleHandler, as suggested by @jmehrens, did trick!A. Rick

1 Answers

1
votes

Per the java.util.logging.ConsoleHandler docs:

.level specifies the default level for the Handler (defaults to Level.INFO).

You are adjusting the logger to set the level to ALL which will produce the log records you want to see. However, the ConsoleHandler will filter them because the default level of the ConsoleHandler is INFO. Therefore, you have to adjust the level of the ConsoleHandler too. You need to do one of the following:

  1. Add a ConsoleHandler and set the level to ALL.
  2. Modify the root ConsoleHandler to set the level to ALL.

Since you are modifying the logger configuration via code it might be easier to do #1.

ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(Level.ALL);
logger.addHandler(ch);
logger.setLevel(Level.ALL);
logger.log(Level.FINEST, String.format("This does NOT print.\n"));

You also have to ensure you don't add the ConsoleHandler multiple times to the logger as each handler add will result in duplicated console output.