Normally, we put the logging. properties at the src/main/resources , and project compile or build will copy it to the root of the classpath. And we can use LogManager or System. setProperty to load the logging.
The Java Logging API has a default logging configuration file located at "lib/logging. properties" , inside the JRE directory. If you edit this file, you edit the default logging settings for the entire JRE, for every program executed.
Logging in simple words refers to the recording of an application activity. Logging is used to store exceptions, information, and warnings as messages that occur during the execution of a program. Logging helps a programmer in the debugging process of a program. Java provides logging facility in the java. util.
You can dynamically load java.util.logging
properties files from a relative path very easily. This is what I put inside a static {}
block in my Main
class. Put your logging.properties
file in the default package
and you can access it very easily with the following code.
final InputStream inputStream = Main.class.getResourceAsStream("/logging.properties");
try
{
LogManager.getLogManager().readConfiguration(inputStream);
}
catch (final IOException e)
{
Logger.getAnonymousLogger().severe("Could not load default logging.properties file");
Logger.getAnonymousLogger().severe(e.getMessage());
}
Java logging doesn't search your whole hard disk for a file; there are very simple rules how files are looked up. You want Java to see that the two files belong to each other but you didn't say so anywhere. Since Java sees no connection between the properties file and your class other than that they are in the same folder on your disk, it can't find the file.
-Djava.util.logging.config.file=log.properties
only works if the file log.properties
is in the current directory of the Java process (which can be pretty random). So you should use an absolute path here.
An alternate solution would be to move the file logging.properties
into $JAVA_HOME/lib/
(or edit the file which should be there). In that case, you don't need to set a System property.
util logging does not load from classpath, it needs an absolute path which is why other logging packages like log4j are far easier to configure and better for web apps where it's a pain to get abs paths.
this is not explained at all in the java.util.logging.LogManager doco.
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