0

I've seen java util logging configured in a number of ways. Usually the fully qualified class name is used for the logger name. In the corresponding logging.properties file, I've seen log level configured at either the package level or the class level in a couple different ways. For example, to set the loglevel to FINE for com.example.MyClass:

  • com.example.level = FINE
  • com.example.* = FINE
  • com.example.MyClass = FINE
  • com.example.MyClass.level = FINE

Do all four of these variants work (assuming they are not overridden later in the file)? Are any of the options "more correct" than the others? Does java.util.logging just assume .level if its not there?

I tried finding the definitive guidance on this config file but stopped at https://docs.oracle.com/javase/8/docs/technotes/guides/logging/overview.html#a1.8 which seems quite underspecified.

lmsurprenant
  • 1,723
  • 2
  • 14
  • 28

1 Answers1

0

Do all four of these variants work (assuming they are not overridden later in the file)?

Most of the online tutorials I've seen use the explicit .level suffix...is that preferred (and why)?

Under the standard LogManager, com.example.* = FINE and com.example.MyClass = FINE would not change the level. The key has to end with .level in order to change the level.

Per the LogManager documentation:

All properties whose names end with ".level" are assumed to define log levels for Loggers. Thus "foo.level" defines a log level for the logger called "foo" and (recursively) for any of its children in the naming hierarchy. Log Levels are applied in the order they are defined in the properties file. Thus level settings for child nodes in the tree should come after settings for their parents. The property name ".level" can be used to set the level for the root of the tree.

If you are using a subclass of LogManager then you need to consult that documentation to verify the syntax.

Does java.util.logging just assume .level if its not there?

According to the documentation it does not. If you declare something without the .level it would just be considered a LogManager entry.

Are any of the options "more correct" than the others?

The LogManager properties file can't create loggers. This means that your log file must match how the code is creating the loggers. For instance, if your file is using com.example.level = FINE and your code is using com.example.MyClass1 and com.example.MyClass2 as the logger name you will never see MyClass1 or MyClass2 switch to FINE because the code never created package parent logger. Root is the parent of all named loggers so that is the ideal way to change multiple loggers at once. If you need to do anything really complicated then you use the config option supported by the LogManager.

jmehrens
  • 10,580
  • 1
  • 38
  • 47
  • “…if your file is using com.example.level = FINE and your code is using com.example.MyClass1 and com.example.MyClass2 as the logger name you will never see MyClass1 or MyClass2 switch to FINE because the code never created package parent logger.” I’m pretty sure that is not true. Creating a logger implicitly creates its parent loggers. From the [documentation](https://docs.oracle.com/en/java/javase/16/docs/api/java.logging/java/util/logging/Logger.html): “Each Logger keeps track of a "parent" Logger, which is its nearest existing ancestor in the Logger namespace.” – VGR Jun 21 '21 at 14:57
  • @VGR I explored this in detail in https://stackoverflow.com/questions/50172433/tomcat-logging-confusion/50177764#50177764 including Java test cases to prove the behavior. – jmehrens Jun 21 '21 at 15:51
  • Yes, I was definitely confusing two different things in my head. The `com.example.*` package syntax I was thinking of here is actually specific to a particular application server named OpenLiberty. I got confused because it handles java.util.logging.Logger messages, but now I understand that this is totally different from LogManager config via logging.properties. The issue discussed here in the comments is actually much more interesting that my actual question, but I'll refrain from modifying the original question here. – lmsurprenant Jun 21 '21 at 17:32