Why Are the Level.Fine Logging Messages Not Showing

Why are the Level.FINE logging messages not showing?

Loggers only log the message, i.e. they create the log records (or logging requests). They do not publish the messages to the destinations, which is taken care of by the Handlers. Setting the level of a logger, only causes it to create log records matching that level or higher.

You might be using a ConsoleHandler (I couldn't infer where your output is System.err or a file, but I would assume that it is the former), which defaults to publishing log records of the level Level.INFO. You will have to configure this handler, to publish log records of level Level.FINER and higher, for the desired outcome.

I would recommend reading the Java Logging Overview guide, in order to understand the underlying design. The guide covers the difference between the concept of a Logger and a Handler.

Editing the handler level

1. Using the Configuration file

The java.util.logging properties file (by default, this is the logging.properties file in JRE_HOME/lib) can be modified to change the default level of the ConsoleHandler:

java.util.logging.ConsoleHandler.level = FINER

2. Creating handlers at runtime

This is not recommended, for it would result in overriding the global configuration. Using this throughout your code base will result in a possibly unmanageable logger configuration.

Handler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.FINER);
Logger.getAnonymousLogger().addHandler(consoleHandler);

Java logging does not seem to work for FINE / FINER / FINEST levels

You have changed the level on the handler, but not the logger. You'll need to do both.

You could do this in code:

LOGGER.setLevel(Level.FINE);

Or, you can read the settings from your properties file. To modify the default logging level, you can add:

.level=FINE

You can specify the logging levels for your own loggers by adding lines such as

com.mycompany.myapp.level=FINE

Why is INFO logged but not FINEST

I suppose your problem is with logs in the console.

The used default level for that Handler is Level.INFO.
http://docs.oracle.com/javase/6/docs/api/java/util/logging/ConsoleHandler.html

With FileHandler which the used default level is Level.ALL, you would have not had the problem.

Either you set the level for ConsoleHandler programatically, either you set the it in the configuration file (https://docs.oracle.com/cd/E19717-01/819-7753/gcblo/)

This post gives more details about the question :
Why are the Level.FINE logging messages not showing?

Logging not showing

I suspect it can be a library I included, which is messing with my logs. Is that possible at all?

Yes. The JUL to SLF4J Bridge can remove the console handler from the JUL root logger. Some libraries invoke LogManager.reset which can remove and close all handlers.

Can a library change the way my logs are shown?

Yes. Once the a log bridge is installed the JUL records are no longer formatted by the JUL formatters.

How can I investigate this, since I am a bit lost?

Modifying the logger tree requires permissions so you can install a SecurityManager with all permissions but then turn on debug tracing with -Djava.security.debug="access,stack" to determine the caller that is modifying the logger tree.

If that doesn't work you can use good ole' System.out to print the logger tree and the attached handlers before and after a library is loaded. Then start adding an removing libraries until you see the logger change.

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class DebugLogging {

private static final Logger log = Logger.getLogger("test");

public static void main(String[] a) {
log.log(Level.FINEST, "Finest");
log.log(Level.FINER, "FINER");
log.log(Level.FINE, "FINE");
log.log(Level.CONFIG, "CONFIG");
log.log(Level.INFO, "INFO");
log.log(Level.WARNING, "WARNING");
log.log(Level.SEVERE, "SEVERE");
log.finest("Finest Log");
log.finer("Finer Log");
log.fine("Fine Log");
log.config("Config Log");
log.info("Info Log");
log.warning("Warning Log");
log.severe("Severe Log");
printConfig(System.err);
}

private static void printConfig(PrintStream ps) {
String cname = System.getProperty("java.util.logging.config.class");
if (cname != null) {
try {
ClassLoader sys = ClassLoader.getSystemClassLoader();
Class<?> c = Class.forName(cname, false, sys);
ps.println(sys.getClass().getName() +" found log configuration class " + c.getName());
} catch (LinkageError | ClassNotFoundException | RuntimeException cnfe) {
ps.println("Unable to load " + cname);
cnfe.printStackTrace(ps);
}
} else {
ps.println("java.util.logging.config.class was null");
}

String file = System.getProperty("java.util.logging.config.file");
if (file != null) {
ps.println("java.util.logging.config.file=" + file);
try {
ps.println("CanonicalPath=" + new File(file).getCanonicalPath());
} catch (RuntimeException | IOException ioe) {
ps.println("Unable to resolve path for " + file);
ioe.printStackTrace(ps);
}

try {
Path p = Paths.get(file);
if (Files.isReadable(p)) {
ps.println(file + " is readable and has size " + Files.size(p));
} else {
if (Files.exists(p)) {
ps.println(file + " exists for " + System.getProperty("user.name") + " but is not readable.");
} else {
ps.println(file + " doesn't exist for " + System.getProperty("user.name"));
}
}
} catch (RuntimeException | IOException ioe) {
ps.println("Unable to read " + file);
ioe.printStackTrace(ps);
}
} else {
ps.println("java.util.logging.config.file was null");
}

LogManager lm = LogManager.getLogManager();
ps.append("LogManager=").println(lm.getClass().getName());
synchronized (lm) {
Enumeration<String> e = lm.getLoggerNames();
while (e.hasMoreElements()) {
Logger l = lm.getLogger(e.nextElement());
if (l != null) {
print(l, ps);
}
}
}
}

private static void print(Logger l, PrintStream ps) {
String scn = l.getClass().getSimpleName();
ps.append("scn=").append(scn).append(", n=").append(l.getName())
.append(", uph=").append(String.valueOf(l.getUseParentHandlers()))
.append(", l=").append(String.valueOf(l.getLevel()))
.append(", fl=").println(l.getFilter());
for (Handler h : l.getHandlers()) {
ps.append("\t").append(l.getName()).append("->")
.append(h.getClass().getName()).append(", h=")
.append(String.valueOf(h.getLevel())).append(", fl=")
.append(String.valueOf(h.getFilter())).println();
}
}
}

Why are the non-root level loggers not receiving logging?

In the end there was still a stray log4j 1.x JAR in the application artifact, after removing that everything worked fine. You would expect some errors or at least a clue in Log4J's debug logging but that wasn't the case. Then again, classpath problems (which I guess this is) can be tricky.



Related Topics



Leave a reply



Submit