Reputation: 7483
I recently installed Java 1.14 JDK on my Linux machine and started to learn Java, and I'm now stuck trying to figure out why this code is not writing to the default log file.
package foo;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
public static void main(String[] args) {
Logger logger = Logger.getLogger("test");
logger.log(Level.INFO, "testinggggg info");
logger.log(Level.SEVERE, "testinggggg severe");
logger.log(Level.WARNING, "testinggggg warning");
logger.log(Level.FINE, "testinggggg fine");
}
}
This code prints to the console
INFO: testinggggg info
Apr 01, 2020 2:45:57 AM foo.Main main
SEVERE: testinggggg severe
Apr 01, 2020 2:45:57 AM foo.Main main
WARNING: testinggggg warning
This is the contents of the my /usr/java/jdk-14/conf/logging.properties
file
# I have omitted all comments
handlers= java.util.logging.ConsoleHandler
.level= INFO
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.maxLocks = 100
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
When I check most of the system properties I see NULL
, for example executing these lines in my application printing NULL
System.out.println(System.getProperty("java.util.logging.FileHandler.pattern"));
System.out.println(System.getProperty("java.util.logging.FileHandler.limit"));
System.out.println(Main.class.getClassLoader().getResource("logging.properties"));
I don't have any file in my $HOME
directory with the name java0.log
or java1.log
. Why is my application not writing to these files ?
Upvotes: 1
Views: 738
Reputation: 7483
Finaaaaaaaaaaaaaaaaaly
I changed this
handlers= java.util.logging.ConsoleHandler
to this
handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
In my /usr/java/jdk-14/conf/logging.properties
file, and now I have java0.log
under my Linux $HOME
directory with these logs
BTW, changing the logging.properties
and saving it, made the changes take effect immediately without restarting my computer, or java or something.
Upvotes: 2