Browse Source

Commons Logging listener

Gump alert: to get this built we need to have commons-logging.jar available, but without the bulid should work fine.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272559 13f79535-47bb-0310-9956-ffa450edef68
master
Erik Hatcher 23 years ago
parent
commit
b868e985c2
2 changed files with 220 additions and 0 deletions
  1. +10
    -0
      build.xml
  2. +210
    -0
      src/main/org/apache/tools/ant/listener/CommonsLoggingListener.java

+ 10
- 0
build.xml View File

@@ -219,6 +219,10 @@
<exclude name="${ant.package}/listener/Log4jListener.java"
unless="log4j.present" />
</patternset>
<patternset id="needs.commons.logging">
<exclude name="${ant.package}/listener/CommongsLoggingListener.java"
unless="commons.logging.present" />
</patternset>
<patternset id="needs.bsf">
<exclude name="${optional.package}/Script.java"
unless="bsf.present" />
@@ -371,6 +375,9 @@
<available property="log4j.present"
classname="org.apache.log4j.Category"
classpathref="classpath"/>
<available property="commons.logging.present"
classname="org.apache.commons.logging.LogFactory"
classpathref="classpath"/>
<!-- this is just a way to check for a TraX implementation -->
<available property="trax.impl.present"
resource="META-INF/services/javax.xml.transform.TransformerFactory"
@@ -525,6 +532,7 @@
<patternset refid="needs.jakarta.regexp" />
<patternset refid="needs.jakarta.oro" />
<patternset refid="needs.jakarta.log4j" />
<patternset refid="needs.commons.logging" />
<patternset refid="needs.sun.uue" />
<patternset refid="needs.javamail" />
<patternset refid="needs.icontract" />
@@ -603,6 +611,7 @@
<exclude name="${util.package}/optional/**" />
<exclude name="${util.package}/regexp/**" />
<exclude name="${ant.package}/listener/Log4jListener.class" />
<exclude name="${ant.package}/listener/CommonsLoggingListener.class" />
<exclude name="${ant.package}/taskdefs/email/UUMailer.class" />
<exclude name="${ant.package}/taskdefs/email/MimeMailer.class" />
<exclude name="${ant.package}/taskdefs/Get.class" />
@@ -641,6 +650,7 @@
<include name="${util.package}/optional/**" />
<include name="${util.package}/regexp/**" />
<include name="${ant.package}/listener/Log4jListener.class" />
<include name="${ant.package}/listener/CommonsLoggingListener.class" />
<include name="${ant.package}/taskdefs/email/UUMailer.class" />
<include name="${ant.package}/taskdefs/email/MimeMailer.class" />
<include name="${ant.package}/taskdefs/Get.class" />


+ 210
- 0
src/main/org/apache/tools/ant/listener/CommonsLoggingListener.java View File

@@ -0,0 +1,210 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Ant", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/

package org.apache.tools.ant.listener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogConfigurationException;
import org.apache.commons.logging.LogFactory;
import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;

/**
* Jakarta Commons Logging listener.
* Note: do not use the SimpleLog as your logger implementation as it
* causes an infinite loop since it writes to System.err, which Ant traps
* and reroutes to the logger/listener layer.
*
* @author Erik Hatcher
* @since Ant 1.5
*/
public class CommonsLoggingListener implements BuildListener {

/** Indicates if the listener was initialized. */
private boolean initialized = false;

private LogFactory logFactory;

/**
* Construct the listener and make sure that a LogFactory
* can be obtained.
*/
public CommonsLoggingListener() {
try {
logFactory = LogFactory.getFactory();
} catch (LogConfigurationException e) {
e.printStackTrace(System.err);
return;
}

initialized = true;
}

/**
* @see BuildListener#buildStarted
*/
public void buildStarted(BuildEvent event) {
if (initialized) {
Log log = logFactory.getInstance(Project.class);
log.info("Build started.");
}
}

/**
* @see BuildListener#buildFinished
*/
public void buildFinished(BuildEvent event) {
if (initialized) {
Log log = logFactory.getInstance(Project.class);
if (event.getException() == null) {
log.info("Build finished.");
} else {
log.error("Build finished with error.", event.getException());
}
}
}

/**
* @see BuildListener#targetStarted
*/
public void targetStarted(BuildEvent event) {
if (initialized) {
Log log = logFactory.getInstance(Target.class);
log.info("Target \"" + event.getTarget().getName() + "\" started.");
}
}

/**
* @see BuildListener#targetFinished
*/
public void targetFinished(BuildEvent event) {
if (initialized) {
String targetName = event.getTarget().getName();
Log log = logFactory.getInstance(Target.class);
if (event.getException() == null) {
log.info("Target \"" + targetName + "\" finished.");
} else {
log.error("Target \"" + targetName
+ "\" finished with error.", event.getException());
}
}
}

/**
* @see BuildListener#taskStarted
*/
public void taskStarted(BuildEvent event) {
if (initialized) {
Task task = event.getTask();
Log log = logFactory.getInstance(task.getClass().getName());
log.info("Task \"" + task.getTaskName() + "\" started.");
}
}

/**
* @see BuildListener#taskFinished
*/
public void taskFinished(BuildEvent event) {
if (initialized) {
Task task = event.getTask();
Log log = logFactory.getInstance(task.getClass().getName());
if (event.getException() == null) {
log.info("Task \"" + task.getTaskName() + "\" finished.");
} else {
log.error("Task \"" + task.getTaskName()
+ "\" finished with error.", event.getException());
}
}
}

/**
* @see BuildListener#messageLogged
*/
public void messageLogged(BuildEvent event) {
if (initialized) {
Object categoryObject = event.getTask();
if (categoryObject == null) {
categoryObject = event.getTarget();
if (categoryObject == null) {
categoryObject = event.getProject();
}
}

Log log = logFactory.getInstance(categoryObject.getClass().getName());
switch (event.getPriority()) {
case Project.MSG_ERR:
log.error(event.getMessage());
break;
case Project.MSG_WARN:
log.warn(event.getMessage());
break;
case Project.MSG_INFO:
log.info(event.getMessage());
break;
case Project.MSG_VERBOSE:
log.debug(event.getMessage());
break;
case Project.MSG_DEBUG:
log.debug(event.getMessage());
break;
default:
log.error(event.getMessage());
break;
}
}
}
}

Loading…
Cancel
Save