19

Are there any example log4j configuration files (XML).

I have a java main application.

I want log4j to output to console AND write to file.

Any examples of this would be greatly appreciated.

I'm using netbeans if that matters.

Blankman
  • 259,732
  • 324
  • 769
  • 1,199

3 Answers3

30

Just have more than one appender in your log4j.xml, like this:

<?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="consoleAppender" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
      </layout>
   </appender>

   <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
      <param name="append" value="false"/>
      <param name="file" value="out/learning.log"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
      </layout>
   </appender>

   <root>
      <level value="INFO"/>
      <appender-ref ref="consoleAppender"/>
      <appender-ref ref="fileAppender"/>
   </root>

</log4j:configuration>
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 1
    silly question, but would I place this in the same folder as my hibernate.cfg folder? My hibernate.cfg.xml is in my /src folder. I am using netbeans, and confused about classpath sorry! – Blankman Apr 11 '10 at 23:53
  • I would not put anything other than .java files in the /src directory. Create a new directory called /config and put your Hibernate and log4j configuration in it. Then add the /config directory to your classpath. – duffymo Apr 11 '10 at 23:58
  • 1
    I guess I just don't understand how to add things to my classpath, i'm using netbeans. – Blankman Apr 12 '10 at 00:06
  • 2
    I don't use NetBeans, but I'd hit the Help button on the menu and figure it out as fast as I could if I were you. You simply can't write Java without knowing how CLASSPATH works. – duffymo Apr 12 '10 at 00:45
6

Here is one sample example of log4j.xml used in Hibernate project.Just need to place this file in classpath and you will get log messages on console as well as in file also.If you want any specific appender you can change in tag.

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">  
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"  
    debug="false">  
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">  
 <layout class="org.apache.log4j.PatternLayout">  
  <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />  
 </layout>  
</appender>  
    <appender name="ASYNC" class="org.apache.log4j.AsyncAppender">  
        <appender-ref ref="CONSOLE" />  
        <appender-ref ref="FILE" />  
</appender>  
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">  
    <param name="File" value="C:/hibernatelog.log"/>  
    <param name="MaxBackupIndex" value="100" />  
 <layout class="org.apache.log4j.PatternLayout">  
  <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />  
</layout>  
</appender>  
    <category name="org.hibernate">  
        <priority value="DEBUG" />  
    </category>  
    <category name="java.sql">  
        <priority value="debug" />  
    </category>  
    <root>  
        <priority value="INFO" />  
        <appender-ref ref="ASYNC" />  
    </root>  
</log4j:configuration>  

I found this one more descriptive then above one.Hope it help.

Ravi Kant
  • 4,785
  • 2
  • 24
  • 23
0

The typical thing to do is to put a log4j.properties file in your classpath. The log4j doc will tell you all you need to know about having two appenders for the console and a file, it's in their examples. In other words, don't bother with the less common XML format and stick with the very common properties format.

bmargulies
  • 97,814
  • 39
  • 186
  • 310