log4j:WARN No appenders could be found for logger(Know how log4j tries to configure itself )

http://jaitechwriteups.blogspot.com/2006/07/know-how-log4j-tries-to-configure.html

http://logging.apache.org/log4j/1.2/manual.html

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.


Anonymous said...

It needs to be followed with a =true otherwise you get errors that it doesn't know how to debug...

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".

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?

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.

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).

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.

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!

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

Andrew said...

thanks mate, you saved me some time with this...

Anonymous said...

hi Anonymous, BasicConfigurator.configure() helped me too. thanks.

hello_earth said...

BasicConfigurator.configure() did it for me too... I put it in the initialization phase of my application listener (using JSF)
Thanks!

Ammianus said...

Thank sir, this simple command saved me a ton of time at work.

satyadas said...

thanks.

 
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

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..

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.

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.....

Aswin said...

Dude that BasicConfiguration helped me a lot...Thanks very much!!!!!

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.

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");

nguyen said...

hi Anonymous, BasicConfigurator.configure() helped me too. thanks.

bananamosh said...

Thank you soo much this command spared my precious hours :)

Siva Kumar said...

Thanks a ton for sharing this information. It helped a lot.

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)

Diego said...

Mark T. W. Ebbert, You save me! ;) thanks a lot!

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...

nviladkar said...

I work in linux environment with eclipse.
I placed log4j.properties file in /src folder and it worked.

Thanks for the help.

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

Anonymous said...

BasicConfigurator.configure();

solved my problem. Thanks for your help.

Vidhya said...

Thanks Anonymous/otmek,
BasicConfigurator solved my problem.
Vidhya

Adis said...

BasicConfigurator.configure() helped me!

Thanks! :)

Anonymous said...

Thanks a lot, using

BasicConfigurator.configure();

worked fine (for console output)!!!

 

=================================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"));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值