0

I am new to the Log4j. By following some online tutorial I am integrating Log4j to my project. I am using Exlipse. It is Dynamic web project and uses Hibernate. I added Log4j jarfile to my project , next I created Log4j.xml file and placed under /src folder. I am getting following error

log4j:ERROR Could not open [log4j.xml].
java.io.FileNotFoundException: log4j.xml (The system cannot find the file specified)

here is my xml file.

 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
 <log4j:configuration debug="true"  xmlns:log4j='http://jakarta.apache.org/log4j/'>

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="[%d{dd-MM-yyyy HH:mm:ss}] %-5p %c{1}:%L - %m%n" />
    </layout>
</appender>

<appender name="file" class="org.apache.log4j.RollingFileAppender">
    <param name="append" value="false" />
    <param name="maxFileSize" value="10MB" />
    <param name="maxBackupIndex" value="10" />
    <param name="file" value="${catalina.home}/logs/ITS_Server.log" />
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="[%d{dd-MM-yyyy HH:mm:ss}] %-5p %c{1}:%L - %m%n" />
    </layout>
</appender>

<category name="org.hibernate">
    <priority value="DEBUG" />
</category>

<category name="java.sql">
    <priority value="debug" />
</category>

<logger name="org.hibernate">
    <level value="info"/> 
</logger>

<root>
    <level value="DEBUG" />
    <appender-ref ref="console" />
    <appender-ref ref="file" />

   </root>
 </log4j:configuration>

In java file I am calling logger as

static Logger log = Logger.getLogger(HibernateUtil.class.getName());


static {
    try {
        DOMConfigurator.configure("log4j.xml");
 }
}

Where I am going wrong , can anyone help me in this pease.

Raghu
  • 1,324
  • 7
  • 24
  • 47
  • Can you make sure that the log4j xml file exists in the directory like 'src/main/resources/log4j.xml' ? – Saqib Rezwan Apr 20 '15 at 10:08
  • @SaqibRezwan Can you please look at my folder structure Now – Raghu Apr 20 '15 at 10:22
  • I am not sure. All of my spring projects structure were like 'Java Resources'->'src/main/resources'->log4j.properties from Eclipse. However, it might look different for many reasons (in eclipse). Can you check the file path in the project folder (in HDD) as 'src/main/resources/log4j.xml'? – Saqib Rezwan Apr 20 '15 at 10:31
  • @SaqibRezwan My logging System is working fine now How can I log all hibernate INFO related logs into a file. Look at my updated xml file. – Raghu Apr 21 '15 at 07:17
  • What was the problem and how you solved? The new problem/question should be in a new thread. However, use rootLogger in "Appender-ref ref" tag and make the "Level value" as "INFO". – Saqib Rezwan Apr 21 '15 at 07:30
  • @SaqibRezwan I dont know what happened , I just created new project , I just put that xml in my actual project. Here I have `` tag and `` and I have set to `info` can u be more specific with your answer. – Raghu Apr 21 '15 at 07:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75774/discussion-between-raghu-and-saqib-rezwan). – Raghu Apr 21 '15 at 07:37

2 Answers2

0

What your code is trying to do is to manually load the log4j configuration and it cannot find the file from the deployed resource.

The correct way is to make sure the log4j.xml is in your CLASSPATH. In Eclipse the source folders will be in your CLASSPATH. So, you can just place it there.

To keep files organized, you can create a source directory under src (package) named resources and place the log4j.xml there. Then you can avoid calling the DOMConfigurator.configure and log4j will automatically load the log4j.xml. Also, make sure you have that resources folder as part of the source fodler in the build path. You can do that by right clicking on the resources folder -> Build Path -> Use as a Source Folder

Edit:

Change

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

To

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">

If it doesn't help test with this example

Edit 2:

For Hibernate, you have the top most (org.hibernate) which will output lots of messages. The level there is INFO.

You can change the level to ERROR to only log serious issues. Changing org.hibernate to org.hibernate.SQL will limit the kind of messages to SQL DML statements being executed.

Read more here https://stackoverflow.com/a/436687/4388699 and https://stackoverflow.com/a/1978415/4388699

Community
  • 1
  • 1
AbuHayA
  • 30
  • 5
  • What you mean exactly? Do I need create one folder by name resources. – Raghu Apr 20 '15 at 10:05
  • Copy log4j.xml to the src folder of your Eclipse project. Refresh the project in the IDE. Then, comment out the `.configure` call – AbuHayA Apr 20 '15 at 10:09
  • Yes I did as you said but no use. Logs are not logging into file. – Raghu Apr 20 '15 at 10:14
  • Yes I made it to `true` but not working. I am doubt abt my folder structure. Is it correct? – Raghu Apr 20 '15 at 11:40
  • Put the log4j.xml in the root of src to solve the issue. If you want the resources to also be in the CLASSPATH, Right click on the project -> properties -> Java Build Path -> in the Source tab add the resources folder there – AbuHayA Apr 20 '15 at 12:32
  • Double check that you are deploying correctly. Do a clean redploy or do an exploded one to see the contents. Also add -Dlog4j.debug to see what is happening – AbuHayA Apr 20 '15 at 12:37
  • Can u brefly explain steps to deploy .? – Raghu Apr 20 '15 at 12:39
  • I am not getting BuildPath option for resorcres folder under properties. – Raghu Apr 20 '15 at 12:40
  • look at my updated xml file in question. Now logging works fine but how to hide Hibernate related logs. – Raghu Apr 21 '15 at 07:20
  • I added an update. You should really end this question and start new ones when this initial issue is resolved. If this answer was helpful, then please accept it so that we can move on to another question. Its getting messy here :) – AbuHayA Apr 21 '15 at 08:00
0

Put log4j.xml file in the project folder as the following structure 'src/main/resources/log4j.xml'.

From Eclipse it should look like'Java Resources'->'src/main/resources'->'log4j.xml'

Saqib Rezwan
  • 1,382
  • 14
  • 20
  • thanks for your reply, I got solved this question , can you please look at my another question , http://stackoverflow.com/questions/29697087/how-to-disable-hibernate-logs-in-eclipse/29697204?noredirect=1#comment47663957_29697204 – Raghu Apr 21 '15 at 09:42