From b868e985c2cb00bb8cf7b5153cfe113bec7c28f3 Mon Sep 17 00:00:00 2001 From: Erik Hatcher Date: Tue, 23 Apr 2002 18:33:52 +0000 Subject: [PATCH] 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 --- build.xml | 10 + .../ant/listener/CommonsLoggingListener.java | 210 ++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 src/main/org/apache/tools/ant/listener/CommonsLoggingListener.java diff --git a/build.xml b/build.xml index b1ca61558..3ea52bc88 100644 --- a/build.xml +++ b/build.xml @@ -219,6 +219,10 @@ + + + @@ -371,6 +375,9 @@ + + @@ -603,6 +611,7 @@ + @@ -641,6 +650,7 @@ + diff --git a/src/main/org/apache/tools/ant/listener/CommonsLoggingListener.java b/src/main/org/apache/tools/ant/listener/CommonsLoggingListener.java new file mode 100644 index 000000000..eb2a22b28 --- /dev/null +++ b/src/main/org/apache/tools/ant/listener/CommonsLoggingListener.java @@ -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 + * . + */ + +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; + } + } + } +}