Simplest possible setting Java logging level, and it still fails. But, System.out.println still works

huangapple go评论78阅读模式
英文:

Simplest possible setting Java logging level, and it still fails. But, System.out.println still works

问题

以下是翻译好的部分:

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"));
   }
}

请注意,这里只提供了代码部分的翻译,去掉了问题描述和其他内容。如果您需要进一步的帮助,请随时提问。

英文:

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&lt;Path&gt; {
   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(&quot;This does NOT print.\n&quot;));
      System.out.println(&quot;In ReadFileInformation Static Block.  This prints out!&quot;);
   }
}
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(&quot;This does NOT print.\n&quot;));
      System.out.println(&quot;In ReadMetadateFileInformation Static Block.  This prints out!&quot;);
   }
   {
      logger.log(Level.FINEST, String.format(&quot;This does NOT print.\n&quot;));
      System.out.println(&quot;In ReadMetadateFileInformation Anonymous Block.  This prints out!&quot;);
   }
   public ReadMetadateFileInformation() {
      super();
      logger.log(Level.FINE, String.format(&quot;This does NOT print.\n&quot;));
   }
}

答案1

得分: 1

根据 java.util.logging.ConsoleHandler 文档:

<handler-name>.level 指定处理程序的默认级别(默认为 Level.INFO)。

您正在调整记录器以将级别设置为 ALL,这将生成您想要查看的日志记录。然而,ConsoleHandler 将对它们进行过滤,因为 ConsoleHandler 的默认级别是 INFO。因此,您还需要调整 ConsoleHandler 的级别。您需要执行以下操作之一:

  1. 添加一个 ConsoleHandler 并将级别设置为 ALL。
  2. 修改根 ConsoleHandler,将级别设置为 ALL。

由于您正在通过代码修改记录器配置,执行 #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"));

您还需要确保不要多次将 ConsoleHandler 添加到记录器,因为每次处理程序添加都会导致重复的控制台输出。

英文:

Per the java.util.logging.ConsoleHandler docs:

> <handler-name>.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(&quot;This does NOT print.\n&quot;));

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.

huangapple
  • 本文由 发表于 2020年9月30日 09:01:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64129555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定