Tuesday, July 04, 2006
Know how log4j tries to configure itself
Large number of applications uselog4j for logging. Sometimes you may encounter cases where you sense that log4j is using some other configurations, other than the one that you expected it to use. You can debug the same by switching on the debug flag on log4j. Here's how you can do it:
Add -Dlog4j.debug to the command line. log4j will output info to std. out. telling you how it tries to configure itself.
32 comments:
Anonymous said...
It needs to be followed with a =true otherwise you get errors that it doesn't know how to debug...
Thursday, August 24, 2006 12:21:00 AM
jaikiran said...
No, you need not specify =true. Just providing -Dlog4j.debug while launching your java program works. Here's an example:
java -Dlog4j.debug org.myapp.SampleProg
You will see the log4j output something similar to:
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@e80a59.
log4j: Using URL [file:/E:/Samples/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is: org.apache.crimson.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= "false".
Thursday, August 24, 2006 6:25:00 PM
Anonymous said...
I added -Dlog4j.debug, but the output is not as descriptive as what you posted. All I get is the following:
log4j: Trying to find [log4j.properties] using context classloader org.eclipse.core.runtime.internal.adaptor.ContextFinder@ede48.
log4j: Trying to find [log4j.properties] using org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader@57f993 class loader.
log4j: Trying to find [log4j.properties] using ClassLoader.getSystemResource().
log4j: Could not find resource: [log4j.properties].
I also tried adding the directory of interest to my classpath, and simply putting the file in a major directory, but log4j just can't see it.
Any ideas?
Friday, February 22, 2008 6:24:00 AM
Anonymous said...
I am the same person as the above comment...
I added the PropertyConfigurator.configure($PATH_TO_FILE) command, and it worked. But I would prefer use the previous method where it gets the file from the classpath. If you have any tips on how to get that to work, I would appreciate it.
I am using eclipse, and I checked that the file is in the classpath there.
Thanks in advance.
Friday, February 22, 2008 6:53:00 AM
Jaikiran said...
I added -Dlog4j.debug, but the output is not as descriptive as what you posted. All I get is the following:
...
log4j: Could not find resource: [log4j.properties].
The last line shows that the log4.properties is not found in the classpath. You mention that you are using Eclipse. I guess you have not placed the log4j.properties (or log4j.xml) at the right place in the project. If you have project created in D:\MySampleApp folder, then place the log4j.properties (or log4j.xml) at the root of the project (i.e. directly in D:\MySampleApp folder).
Friday, February 22, 2008 2:40:00 PM
Mark T. W. Ebbert said...
If you have project created in D:\MySampleApp folder, then place the log4j.properties (or log4j.xml) at the root of the project (i.e. directly in D:\MySampleApp folder).
Thanks for the tip. I tried placing it in various locations within the project (at the root, in the root of the build.eclipse directory, etc.) but to no avail. I wish that the debug output would tell me where it is looking, as it did in your output. I have searched but found no way of making it more verbose.
Friday, February 22, 2008 10:51:00 PM
Anonymous said...
Mark T. W. Ebbert said...
If you have project created in D:\MySampleApp folder, then place the log4j.properties (or log4j.xml) at the root of the project (i.e. directly in D:\MySampleApp folder).
Thanks for the tip. I tried placing it in various locations within the project (at the root, in the root of the build.eclipse directory, etc.) but to no avail. I wish that the debug output would tell me where it is looking, as it did in your output. I have searched but found no way of making it more verbose.
Mark, if you use Eclipse go to project properties, then Java Build Path. There you'll find currently defined classpath for your project.
For example my project defines 2 source folders on build path:
{project name}/src/main/java and {project name}/src/main/resources. I added the log4j.properties file under {project name}/src/main/resources/config/log4j.properties. Whan I ran the project I had to pass the parameter as -Dlog4j.configuration=config/log4j.propertiesand it configured log4j properly.
Hope this helps!
Thursday, April 10, 2008 10:16:00 PM
Anonymous said...
I had a problem with log4j too, my error was:
log4j:WARN No appenders could be found for logger ...
log4j:WARN Please initialize the log4j system properly.
To solve this problem I used :
BasicConfigurator.configure(); line on start main method.
import org.apache.log4j.BasicConfigurator;
It solved my problem. Maybe it help you too.
otmek
Wednesday, June 25, 2008 6:08:00 PM
Andrew said...
thanks mate, you saved me some time with this...
Tuesday, August 05, 2008 12:22:00 AM
Anonymous said...
hi Anonymous, BasicConfigurator.configure() helped me too. thanks.
Thursday, December 11, 2008 10:45:00 PM
hello_earth said...
BasicConfigurator.configure() did it for me too... I put it in the initialization phase of my application listener (using JSF)
Thanks!
Monday, January 12, 2009 10:23:00 PM
Ammianus said...
Thank sir, this simple command saved me a ton of time at work.
Saturday, January 31, 2009 3:38:00 AM
satyadas said...
thanks.
Tuesday, March 17, 2009 2:04:00 PM
Roberto Melo Cavalcante said...
Guys,
If you use BasicConfigurator it will work, however, you will have only console appender with log level hard set and an useless log4j.config file.
To solve it, just put log4j.config file inside src folder.
I know it may not seem to be a very clean solution, but then, you'll have time figure out how you can set your classpath to allow log4j.properties to be read from project root folder.
Best regards
Friday, September 04, 2009 8:20:00 PM
Deepa said...
You are right,
Its for console default logger.
but if we need to use log4j.properties file's custom logging then its no use.
As Roberto mentioned, put a log4j.properties file in src older but still having same waring,
log4j:WARN No appenders could be found for logger (src.com.test.Calculator).
log4j:WARN Please initialize the log4j system properly.
please help on this..
Monday, September 07, 2009 5:45:00 PM
Jaikiran said...
Deepa,
You mention that you placed the log4j.properties in the src folder. However, if you are still seeing that WARN message then i guess log4j did not find the log4j.properties.
As explained in the article, try using the -Dlog4j.debug option to see whether log4j is able to find the configuration file.
Monday, September 07, 2009 5:48:00 PM
Deepa said...
hi Jaikiran,
I understood the problem why log4j did not find the log4j.properties even though placing it in src folder..,
Not just for compilation it needs for running application as well (the thing which was missing :( ), so i put one copy of this properties file where the classes exit of my src.
src
--com
-- test
-- Hello.java
log4j.properties
classes
--com
-- test
-- Hello.class
log4j.properties
it works .. Thanks :)
Deepa.....
Wednesday, September 09, 2009 11:31:00 AM
Aswin said...
Dude that BasicConfiguration helped me a lot...Thanks very much!!!!!
Saturday, October 03, 2009 2:10:00 AM
Jack said...
Hey thanks for sharing about looking at the java build path to know where to put the log4j.properties.
Finally solve my problem when dealing with a package.
Tuesday, January 05, 2010 3:26:00 PM
Anonymous said...
I guess, it will solve this problem if you fully qualify the class...
for example,
private static Logger log = Logger.getLogger("com.fmr.fims.deconv.XrefCnvThread.class");
instead of just private static Logger log = Logger.getLogger("XrefCnvThread.class");
Friday, July 16, 2010 12:23:00 AM
nguyen said...
hi Anonymous, BasicConfigurator.configure() helped me too. thanks.
Wednesday, August 04, 2010 9:04:00 AM
bananamosh said...
Thank you soo much this command spared my precious hours :)
Thursday, August 12, 2010 5:50:00 PM
Siva Kumar said...
Thanks a ton for sharing this information. It helped a lot.
Thursday, January 06, 2011 11:45:00 AM
Anonymous said...
I just had a nightmare time debugging a problem similar to what you guys describe.
For me, the solution was to use the full "file:/" URL form. For example:
-Dlog4j.configuration=file:/jamesGosling/log4j.xml
This is briefly discussed (but totally inadequately emphasized) in the log4j manual:
http://logging.apache.org/log4j/1.2/manual.html
(See "Example 4" under the "Default Initialization under Tomcat" section)
Friday, April 01, 2011 1:06:00 PM
Diego said...
Mark T. W. Ebbert, You save me! ;) thanks a lot!
Saturday, April 23, 2011 10:08:00 PM
Anonymous said...
"The last line shows that the log4.properties is not found in the classpath. You mention that you are using Eclipse. I guess you have not placed the log4j.properties (or log4j.xml) at the right place in the project. If you have project created in D:\MySampleApp folder, then place the log4j.properties (or log4j.xml) at the root of the project (i.e. directly in D:\MySampleApp"
It works...
Tuesday, May 03, 2011 7:44:00 AM
nviladkar said...
I work in linux environment with eclipse.
I placed log4j.properties file in /src folder and it worked.
Thanks for the help.
Monday, August 22, 2011 1:13:00 PM
Anonymous said...
WOW thank you Anonymous from Friday, April 01, 2011 1:06:00 PM
I had put the properties file in the classpath, and I had tried specifying the properties file via the command line parameter -Dlog4j.configuration, but neither seemed to be working. Finally, I tried specifying the file path as a URI rather than a local file path, and it finally worked.
Try this:
-Dlog4j.configuration=file:C:\MyPath\log4j.properties
instead of this:
-Dlog4j.configuration=C:\MyPath\log4j.properties
Wednesday, October 12, 2011 11:15:00 PM
Anonymous said...
BasicConfigurator.configure();
solved my problem. Thanks for your help.
Wednesday, January 18, 2012 11:24:00 PM
Vidhya said...
Thanks Anonymous/otmek,
BasicConfigurator solved my problem.
Vidhya
Friday, March 09, 2012 10:44:00 PM
Adis said...
BasicConfigurator.configure() helped me!
Thanks! :)
Saturday, March 17, 2012 2:29:00 AM
Anonymous said...
Thanks a lot, using
BasicConfigurator.configure();
worked fine (for console output)!!!
Wednesday, April 11, 2012 3:36:00 PM
=================================for me
private static final Logger logger = Logger.getLogger(EODPublishMain.class.getName());
log4j: Trying to find [/export/opt/xxx/4.3_A1/config/log4j.properties] using context classloadersun.misc.Launcher$AppClassLoader@7d772e.
log4j: Trying to find [/export/opt/xxx/4.3_A1/config/log4j.properties] usingsun.misc.Launcher$AppClassLoader@7d772e class loader.
log4j: Trying to find [/export/opt/xxx/4.3_A1/config/log4j.properties] using ClassLoader.getSystemResource().
log4j: Could not find resource: [/export/opt/xxx/4.3_A1/config/log4j.properties].
when I used:
if ( System.getProperty("log4j.configuration") != null ) {
System.out.println("configure logj.configuration");
PropertyConfigurator.configure(System.getProperty("log4j.configuration"));
}
else
BasicConfigurator.configure();
it will work when calling PropertyConfigurator.configure(System.getProperty("log4j.configuration"));