I got an issue which is really baffling me. I wanted to try out some things with the JUL logging (JDK logging). I started out with the following simple program:
package biz.ple;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class Application {
public static void main(String[] args)
{
Logger logA = LogManager.getLogManager().getLogger("LoggerA");
Logger logB = LogManager.getLogManager().getLogger("LoggerB");
if (logA == null) {
System.out.println("LoggerA is null!");
}
if (logB == null) {
System.out.println("LoggerB is null!");
}
String normalMsg = "Message without cookie prefix.";
String cookieMsg = "[Cookie] Message with a cookie prefix.";
logA.info(normalMsg);
logA.info(cookieMsg);
logB.info(normalMsg);
logB.info(cookieMsg);
}
}
Amazingly, the loggers logA and logB are always null. I read in the documentation that a logger to which the application holds no strong reference my be GCed at any time, but it seems to me that the variables logA and logB are indeed strong references, aren't they?
I really don't understand this and would appreciate any help.
My understanding is that this will only get existing loggers, not create a new one of that name. To create a new logger you need to use the static Logger.getLogger("myLogger");
command
public static void main(String[] args){
Logger log=Logger.getLogger("myLogger");
Logger logA = LogManager.getLogManager().getLogger("myLogger"); //exists
Logger logB = LogManager.getLogManager().getLogger("nonExistantLogger"); //is null
}
Find or create a logger for a named subsystem. If a logger has already been created with the given name it is returned. Otherwise a new logger is created.
If a new logger is created its log level will be configured based on the LogManager configuration and it will configured to also send logging output to its parent's Handlers. It will be registered in the LogManager global namespace.
Note: The LogManager may only retain a weak reference to the newly created Logger. It is important to understand that a previously created Logger with the given name may be garbage collected at any time if there is no strong reference to the Logger. In particular, this means that two back-to-back calls like getLogger("MyLogger").log(...) may use different Logger objects named "MyLogger" if there is no strong reference to the Logger named "MyLogger" elsewhere in the program.
Method to find a named logger.
Note that since untrusted code may create loggers with arbitrary names this method should not be relied on to find Loggers for security sensitive logging. It is also important to note that the Logger associated with the String name may be garbage collected at any time if there is no strong reference to the Logger. The caller of this method must check the return value for null in order to properly handle the case where the Logger has been garbage collected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With