Can't configure logging for executable jar
After dealing a few days against this and reading many resources on the Internet I came up with this solution:
I have to change the logging properties of the LogManager in the program. I can use the logging.properties
file packaged in the .jar
like this:
LogManager.getLogManager().readConfiguration(ReportGenerator.class.getClassLoader().getResourceAsStream("logging.properties"));
And then I can do the same as before to get the logger and log:
Logger logger = Logger.getLogger(ReportGenerator.class.getName());
logger.log(Level.INFO, "LOG THIS");
But I found very useful that you can specify another logging.properties
file on runtime so my final code is this:
String logFile = System.getProperty("java.util.logging.config.file");
if(logFile == null){
LogManager.getLogManager().readConfiguration(ReportGenerator.class.getClassLoader().getResourceAsStream("logging.properties"));
}
Logger logger = Logger.getLogger(ReportGenerator.class.getName());
logger.log(Level.INFO, "LOG THIS");
That way, if I execute the jar as this:
java -jar MyReportsJar.jar
It uses the internal logging.properties
file.
But if I execute:
java -Djava.util.logging.config.file=<external-logging.properties> -jar MyReportsJar.jar
it uses the external logging.properties
file.
Related videos on Youtube
Christian Vielma
I'm a person who like to do the right thing every day. Aristotle one day said "We are what we repeatedly do.Excellence, then, is not an act, but a habit.". I truly believe in this. I'm mainly what's considered a back-end developer. I've worked on some projects, with more emphasis lately on distributed applications and enterprise systems. Lately I've been working also on systems orchestration, streaming and microservices development. I also like to talk about computer science topics on my Youtube channel "A Dev's Story"
Updated on September 14, 2022Comments
-
Christian Vielma over 1 year
This is similar to this other question, although I already put the
logging.properties
in the executable jar and doesn't work.I have a class (ReportGenerator) that has the following:
Logger logger = Logger.getLogger(ReportGenerator.class.getName()); logger.log(Level.INFO, "LOG THIS");
I'm using Netbeans so I put the
logging.properties
file in the pathsrc/main/resources
. It has this (among other things):# default file output is in user's home directory. java.util.logging.FileHandler.pattern = /my/folder/reports.log java.util.logging.FileHandler.limit = 50000 java.util.logging.FileHandler.count = 10 java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter # Limit the message that are printed on the console to INFO and above. java.util.logging.ConsoleHandler.level = OFF java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter ############################################################ # Facility specific properties. # Provides extra control for each logger. ############################################################ # For example, set the com.xyz.foo logger to only log SEVERE # messages: com.mypackage.ReportGenerator.level = ALL
The jar is generated using Maven, when decompressed I can see that the
logging.properties
is in the main folder of the jar. Along with the foldercom
where my class is.-com -mypackage -ReportGenerator logging.properties ...other things
When I run from console:
java - jar MyReportsJar.jar
It shows me the logs through the console. I want to log it to the file I set in the
logging.properties.
What am I doing wrong? How do I do it without setting the JVM
java.util.logging.config.file
param?