Tuesday, July 04, 2006

Know how log4j tries to configure itself

Large number of applications use log4j 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.


36 comments:

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

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

    ReplyDelete
  3. 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?

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

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

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

    ReplyDelete

  7. 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.properties and it configured log4j properly.

    Hope this helps!

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

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

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

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

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

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

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

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

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

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

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

    ReplyDelete
  19. 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");

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

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

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

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

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

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

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

    Thanks for the help.

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

    ReplyDelete
  28. BasicConfigurator.configure();

    solved my problem. Thanks for your help.

    ReplyDelete
  29. Thanks Anonymous/otmek,
    BasicConfigurator solved my problem.
    Vidhya

    ReplyDelete
  30. BasicConfigurator.configure() helped me!

    Thanks! :)

    ReplyDelete
  31. Thanks a lot, using

    BasicConfigurator.configure();

    worked fine (for console output)!!!

    ReplyDelete
  32. You made my day, bro! Live long and prosper!

    ReplyDelete
  33. BasicConfigurator.configure() did it for me too...
    Thanks alot

    ReplyDelete
  34. @Deepa .. thanks
    Its worked .. when i put the file in /bin/*.class

    ReplyDelete
  35. @Eclipse Users ... put the file (log4j.properties) in ur workspace/project/bin/ .. where your class files are present.. no need to put in /src/ or /project/.

    ReplyDelete