I'm trying out log4j in a simple test app. I create a new Java project in eclipse and add the log4j JAR (v1.2.16) to my build path. I then create a simple class that prints Hello World. Then I use the log4j Logger
class to log a info message. When I run the app, I see the log message, using what I assume is the default appender and layout. Great. What I'm having trouble with is adding my own configuration. This is what I've done:
Created a log4j.properties file with a custom appender and log level and placed it into the src folder (which upon compilation gets copied to the bin folder). Run the app - no change.
I try adding PropertyConfigurator.configure("log4j.properties")
. Run the app - no change. No errors, but no change.
What do I have to do to get log4j to load my configuration file?
We should put log4j2. properties anywhere in the application's classpath. Log4j2 will scan all classpath locations to find out this file and then load it. We have put the file in resources folder.
By default, Log4J 2 looks for a properties file with the name log4j2. properties in the classpath. In a Spring Boot application, the log4j2. properties file will typically be in the resources folder.
Navigate into the "META-INF" sub-directory and open the file "MANIFEST. MF" in a text editor. Find the line starting with "Implementation-Version", this is the Log4j version.
Look in the manual under the heading Default Initialization Procedure, where you'll find the following:
The exact default initialization algorithm is defined as follows:
- Setting the
log4j.defaultInitOverride
system property to any other value than "false
" will cause log4j to skip the default initialization procedure (this procedure).- Set the resource string variable to the value of the
log4j.configuration
system property. The preferred way to specify the default initialization file is through thelog4j.configuration
system property. In case the system property log4j.configuration is not defined, then set the string variable resource to its default value "log4j.properties
".- Attempt to convert the resource variable to a URL.
- If the resource variable cannot be converted to a URL, for example due to a MalformedURLException, then search for the resource from the classpath by calling org.apache.log4j.helpers.Loader.getResource(resource, Logger.class) which returns a URL. Note that the string "
log4j.properties
" constitutes a malformed URL. See Loader.getResource(java.lang.String) for the list of searched locations.- If no URL could not be found, abort default initialization. Otherwise, configure log4j from the URL. The PropertyConfigurator will be used to parse the URL to configure log4j unless the URL ends with the "
.xml
" extension, in which case the DOMConfigurator will be used. You can optionaly specify a custom configurator. The value of thelog4j.configuratorClass
system property is taken as the fully qualified class name of your custom configurator. The custom configurator you specify must implement theConfigurator
interface.
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