Section 12.4. Creating Custom Loggers

12.4. Creating Custom Loggers

You can handle build events with custom loggers as well if you extend the Ant DefaultLogger class. Example 12-17 shows a logger that will log each task as it's executed. Like listeners, loggers must not access System.out and System.err directly, so to display messages to the standard out device, this code uses the Ant log( ) method (which defaults to standard out).

Example 12-17. A new logger (ch12/logger/
import; import; import; public class ProjectLogger extends DefaultLogger {     public void taskStarted(BuildEvent event)     {         String text = "Running task " + event.getTask( ).getTaskName( )              + StringUtils.LINE_SEP;          printMessage(text, out, event.getPriority( ));         log(text);     } }

For this example, use a simple custom task:

public class Project {     public void execute( )      {         System.out.println("No worries.");     } }

Here's the build file:

<?xml version="1.0"?> <project basedir="." default="main">     <property name="src" value="src"/>     <property name="output" value="output"/>     <target name="main" depends="jar">         <taskdef name="project" classname="Project" classpath="Project.jar"/>         <project/>     </target>     <target name="compile">         <mkdir dir="${output}"/>         <javac srcdir="${src}" destdir="${output}"/>     </target>     <target name="jar" depends="compile">         <jar destfile="Project.jar" basedir="${output}"/>     </target> </project>

When you execute this build file with the custom logger, each task will be displayed as it's executed. To attach the logger to the build, use this command line:

%ant -logger ProjectLogger

When the build runs, you'll see each task logged like this:

%ant -logger ProjectLogger Buildfile: build.xml Running task property Running task property compile: Running task mkdir     [mkdir] Created dir: C:\ant\ch12\logger\output Running task javac     [javac] Compiling 1 source file to C:\ant\ch12\logger\output jar: Running task jar       [jar] Building jar: C:\ant\ch12\logger\Project.jar main: Running task taskdef Running task project   [project] No worries. BUILD SUCCESSFUL Total time: 3 seconds

    Ant. The Definitive Guide
    Ant: The Definitive Guide, 2nd Edition
    ISBN: 0596006098
    EAN: 2147483647
    Year: 2003
    Pages: 115
    Authors: Steve Holzner

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: