File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Open Source Projects and the fly likes log4j.properties or log4j.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "log4j.properties or log4j.xml" Watch "log4j.properties or log4j.xml" New topic
Author

log4j.properties or log4j.xml

pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
which is good for a small application where i just want to log exceptions? log4j.properties or log4j.xml

tia.


:d
Vinodkumar Beli
Greenhorn

Joined: Nov 26, 2008
Posts: 4
keep this log where you want , but specify the path in Loader.getResource("c:/log4j.xml") correctly


1.<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/";>
2. <appender name="APPLOGFILE" class="org.apache.log4j.RollingFileAppender">
3. <param name="File" value="workshop_test.log" />
4. <param name="Append" value="true" />
5. <param name="MaxFileSize" value="3000KB" />
6. <layout class="org.apache.log4j.PatternLayout">
7. <param name="ConversionPattern" value="%d{DATE} %-5p %-15c{1}: %m%n"/>
</layout>

<root>
8. <priority value="debug"></priority>
9. <appender-ref ref="File"/>
</root>
</appender>

line 2 , here we are using RollingFileAppender . it will role the file if maximum file eceeds the size =3000kb
as mentioned in the line 5
line 3 to create file name called workshop_test.log where all logs are available in your root java_project
line 6 and 7 used for how the logs format to be logged on file .
important line 8. tells that from which level the log to printed .
levels like 1. trace 2.debug 3.info 4.warn 5. error 6. fatal. In production environment change the debug level to
error level to avoid unnessary log getting printed in file.

Load the log4j.xml use this code

import org.apache.log4j.helpers.Loader;
import org.apache.log4j.xml.DOMConfigurator;

/ use the same class loader as your class
URL url = LogClass.class.getResource("/my.properties");
PropertyConfigurator.configure(url);

// load custom XML configuration
URL url = Loader.getResource("c:/log4j.xml"); // if you keep it in c drive
DOMConfigurator.configure(url);


Download the jar log4j-1.2.13.jar from docjar.com site and build the jars to classpath using eclipse tool

Test your log4j set using this calss

import org.apache.log4j.Logger;

public class LogClass {
private static org.apache.log4j.Logger log = Logger
.getLogger(LogClass.class);

public static void main(String[] args) {

log.trace("Trace");
log.debug("Debug");
log.info("Info");
log.warn("Warn");
log.error("Error");
log.fatal("Fatal");

}
}
javascript:emoticon('');


Vinnodkumar

happy to help!!!
Vinodkumar Beli
Greenhorn

Joined: Nov 26, 2008
Posts: 4
But if you use property file you cant able to set Filter in appender definition .

i.e<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="info"/>
</filter>

LevelRangeFilter and LevelMatchFilters used to restrict of level to be logged to log4j file....
 
Don't get me started about those stupid light bulbs.
 
subject: log4j.properties or log4j.xml