1 18 19 package org.apache.tools.ant.listener; 20 21 import org.apache.log4j.Logger; 22 import org.apache.log4j.helpers.NullEnumeration; 23 import org.apache.tools.ant.BuildEvent; 24 import org.apache.tools.ant.BuildListener; 25 import org.apache.tools.ant.Project; 26 import org.apache.tools.ant.Target; 27 import org.apache.tools.ant.Task; 28 29 30 34 public class Log4jListener implements BuildListener { 35 36 37 private boolean initialized = false; 38 39 42 public static final String LOG_ANT = "org.apache.tools.ant"; 43 44 47 public Log4jListener() { 48 initialized = false; 49 Logger log = Logger.getLogger(LOG_ANT); 50 Logger rootLog = Logger.getRootLogger(); 51 if (!(rootLog.getAllAppenders() instanceof NullEnumeration)) { 52 initialized = true; 53 } else { 54 log.error("No log4j.properties in build area"); 55 } 56 } 57 58 61 62 public void buildStarted(BuildEvent event) { 63 if (initialized) { 64 Logger log = Logger.getLogger(Project.class.getName()); 65 log.info("Build started."); 66 } 67 } 68 69 72 73 public void buildFinished(BuildEvent event) { 74 if (initialized) { 75 Logger log = Logger.getLogger(Project.class.getName()); 76 if (event.getException() == null) { 77 log.info("Build finished."); 78 } else { 79 log.error("Build finished with error.", event.getException()); 80 } 81 } 82 } 83 84 87 88 public void targetStarted(BuildEvent event) { 89 if (initialized) { 90 Logger log = Logger.getLogger(Target.class.getName()); 91 log.info("Target \"" + event.getTarget().getName() + "\" started."); 92 } 93 } 94 95 98 99 public void targetFinished(BuildEvent event) { 100 if (initialized) { 101 String targetName = event.getTarget().getName(); 102 Logger cat = Logger.getLogger(Target.class.getName()); 103 if (event.getException() == null) { 104 cat.info("Target \"" + targetName + "\" finished."); 105 } else { 106 cat.error("Target \"" + targetName 107 + "\" finished with error.", event.getException()); 108 } 109 } 110 } 111 112 115 116 public void taskStarted(BuildEvent event) { 117 if (initialized) { 118 Task task = event.getTask(); 119 Logger log = Logger.getLogger(task.getClass().getName()); 120 log.info("Task \"" + task.getTaskName() + "\" started."); 121 } 122 } 123 124 127 128 public void taskFinished(BuildEvent event) { 129 if (initialized) { 130 Task task = event.getTask(); 131 Logger log = Logger.getLogger(task.getClass().getName()); 132 if (event.getException() == null) { 133 log.info("Task \"" + task.getTaskName() + "\" finished."); 134 } else { 135 log.error("Task \"" + task.getTaskName() 136 + "\" finished with error.", event.getException()); 137 } 138 } 139 } 140 141 144 145 public void messageLogged(BuildEvent event) { 146 if (initialized) { 147 Object categoryObject = event.getTask(); 148 if (categoryObject == null) { 149 categoryObject = event.getTarget(); 150 if (categoryObject == null) { 151 categoryObject = event.getProject(); 152 } 153 } 154 155 Logger log 156 = Logger.getLogger(categoryObject.getClass().getName()); 157 switch (event.getPriority()) { 158 case Project.MSG_ERR: 159 log.error(event.getMessage()); 160 break; 161 case Project.MSG_WARN: 162 log.warn(event.getMessage()); 163 break; 164 case Project.MSG_INFO: 165 log.info(event.getMessage()); 166 break; 167 case Project.MSG_VERBOSE: 168 log.debug(event.getMessage()); 169 break; 170 case Project.MSG_DEBUG: 171 log.debug(event.getMessage()); 172 break; 173 default: 174 log.error(event.getMessage()); 175 break; 176 } 177 } 178 } 179 } 180 | Popular Tags |