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 path src/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 folder com
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?
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.
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