Log4j with Ant:
Now lets use log4j with ant to log messages from source code to log files, before that i suggest you to download the log4j binary from logging.apache.org/log4j/ it usually comes in .tar and .zip formats choose .zip format unzip, extract it's contents to some drive. Open your eclipse IDE create new project, select java project, now create a file and specify it's extension as .log for example(logging.log) and save it in src folder. create log4.properties file save it in src folder along with logging.log(empty file) file.import all the jars from the lib directory of ant extracted path and log4j.jar from ant extracted path .
Lets code now
logging.log
its an empty file
log4j.properties
log=src
log4j.rootLogger=DEBUG,INFO,rollingFile
log4j.appender.rollingFile.=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=${log}/logging.log
log4j.appender.rollingFile.MaxFileSize=2MB
log4j.appender.rolingFile.MaxBackupIndex=2
log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.conversionPattern=%d%m%n
package com.ant.log4j.tests;
public class Ant_Log_Test_Class
{
private static Logger logger=Logger.getLogger(Ant_Log_Test_Class.class);
public static void main(String...s)
{
BasicConfigurator.configure();
logger.debug("test debug message !");
logger.info("test info message !");
}
};
build.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project name="ant_log_project" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="bin" value="bin"/>
<property name="build.dir" value="${bin.dir}/build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jars"/>
<property name="main-class" value="com.ant.log.tests.Ant_Log_Test_Class"/>
<path id="supportjarsid">
<pathelement location="bin" />
<pathelement location="fully qualified name of the log4j.jar with .jar extension"/>
</path>
<target name="clean">
<delete dir="${bin.dir}/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="supportjarsid" includeantruntime="false" />
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}" />
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}""/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java classname="${main-class}" fork="true">
<classpath >
<path refid="supportjarsid"/>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
</project>
........right click on this build.xml file select run-as from run as select 2.ant-build an external tool will pop out select the choice of target and run, check the logging.log file you will find the messages !bingo
Now lets use log4j with ant to log messages from source code to log files, before that i suggest you to download the log4j binary from logging.apache.org/log4j/ it usually comes in .tar and .zip formats choose .zip format unzip, extract it's contents to some drive. Open your eclipse IDE create new project, select java project, now create a file and specify it's extension as .log for example(logging.log) and save it in src folder. create log4.properties file save it in src folder along with logging.log(empty file) file.import all the jars from the lib directory of ant extracted path and log4j.jar from ant extracted path .
Lets code now
logging.log
its an empty file
log4j.properties
log=src
log4j.rootLogger=DEBUG,INFO,rollingFile
log4j.appender.rollingFile.=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=${log}/logging.log
log4j.appender.rollingFile.MaxFileSize=2MB
log4j.appender.rolingFile.MaxBackupIndex=2
log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.conversionPattern=%d%m%n
package com.ant.log4j.tests;
public class Ant_Log_Test_Class
{
private static Logger logger=Logger.getLogger(Ant_Log_Test_Class.class);
public static void main(String...s)
{
BasicConfigurator.configure();
logger.debug("test debug message !");
logger.info("test info message !");
}
};
build.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project name="ant_log_project" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="bin" value="bin"/>
<property name="build.dir" value="${bin.dir}/build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jars"/>
<property name="main-class" value="com.ant.log.tests.Ant_Log_Test_Class"/>
<path id="supportjarsid">
<pathelement location="bin" />
<pathelement location="fully qualified name of the log4j.jar with .jar extension"/>
</path>
<target name="clean">
<delete dir="${bin.dir}/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="supportjarsid" includeantruntime="false" />
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}" />
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}""/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java classname="${main-class}" fork="true">
<classpath >
<path refid="supportjarsid"/>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
</project>
........right click on this build.xml file select run-as from run as select 2.ant-build an external tool will pop out select the choice of target and run, check the logging.log file you will find the messages !bingo
No comments:
Post a Comment