Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure logging when running a JAR?

Tags:

java

logging

jar

I am new to Java logging API and need some help with this problem: While creating the application, my config file was stored in the project root folder, so I used -Djava.util.logging.config.file=logging.properties switch to run the program. But then I exported the executable JAR. How to configure the logging now? It doesn't work, when I specify the path to config file with the -D switch.

like image 773
planky Avatar asked Apr 29 '12 09:04

planky


People also ask

How do I check the logs of a JAR file?

jar doesn't use any logging framework. In that case, it is impossible to get logs from it. However, what you can do is use a debugger. All IDEs have debuggers.

How do you enable logs in Java?

To enable logging perform the following actions: Open Java Control Panel. Click Advanced tab. Select Enable Logging under the Debugging option.

Where do I put logging properties?

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.


2 Answers

I know it's little late to answer this question but I ran into this issue few days back with the runnable jar and I solved it like this:

java -Djava.util.logging.config.file=logging.properties -cp test.jar com.sample.test.Main

where test.jar is the name of your jar and com.sample.test.Main is the fully qualified name of your main class.

like image 152
pakg1047 Avatar answered Oct 02 '22 14:10

pakg1047


The javadoc says:

In addition, the LogManager uses two optional system properties that allow more control over reading the initial configuration:

"java.util.logging.config.class"
"java.util.logging.config.file" 

These two properties may be set via the Preferences API, or as command line property definitions to the "java" command, or as system property definitions passed to JNI_CreateJavaVM.

If the "java.util.logging.config.class" property is set, then the property value is treated as a class name. The given class will be loaded, an object will be instantiated, and that object's constructor is responsible for reading in the initial configuration. (That object may use other system properties to control its configuration.) The alternate configuration class can use readConfiguration(InputStream) to define properties in the LogManager.

So, either use the java.util.logging.config.file system property, and store the config file out of the jar file (which is probably a good idea if you want to be able to customize the logging properties in order to debug or analyze some weird behavior), or store the config file wherever you want (in the jar file, for example), and use the java.util.logging.config.class system property to load and instantiate a class that will read the file in the jar file (Using Class.getResourceAsStream()).

like image 37
JB Nizet Avatar answered Oct 02 '22 14:10

JB Nizet