Home > Articles

Global Concepts

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Listeners and Loggers

We need to briefly touch on two other concepts: listeners and loggers. A listener is a component that is alerted to certain events during the life of a build. A logger is built on top of a listener, and is the component that is responsible for logging information about the build.

Listeners are alerted to seven different events:

  • Build started

  • Build finished

  • Target started

  • Target finished

  • Task started

  • Task finished

  • Message logged

These events should trigger some action in the listener.

For the most part, you really need to know about listeners and what they respond to only if you're trying to write your own custom task or logger. You can read about building such elements in Chapter 6, "Extending Ant with Custom Tasks, Data Types, and Listeners."

Loggers, however, are far more interesting and useful. You're using a logger whenever you run Ant, whether or not you specify one. If you don't specify a logger, you're using the DefaultLogger. In addition to the DefaultLogger, Ant comes with four standard loggers and one standard listener. They are

  • DefaultLogger

  • NoBannerLogger

  • MailLogger

  • AnsiColorLogger

  • XmlLogger

  • Log4jListener

Here's a rundown of what each one does, how you use it, and why you'd use it.


The DefaultLogger is used if you don't tell Ant to use something different. It is singularly unimpressive, but it does its job and does it well. Although you can tell Ant specifically to use this logger, there is really no good reason to do so. For the record, Listing 3.70 shows how to force Ant to use the DefaultLogger.

Listing 3.70 Forcing Use of the DefaultLogger

ant -logger org.apache.tools.ant.DefaultLogger


The NoBannerLogger is very similar to the DefaultLogger, except it doesn't log anything from targets that don't log anything. When you run Ant, each target's name prints at the left-most column, followed by a colon, and then the name of each task contained inside it is printed in brackets. This occurs even if there is no useful output from any of the tasks.

If you want to suppress this output, use the NoBannerLogger. This logger still logs any useful information that is logged, but does not log anything for targets or tasks that don't make any "noise," so to speak. Invoke it as shown in Listing 3.71.

Listing 3.71 Invoking Ant with the NoBannerLogger

ant -logger org.apache.tools.ant.NoBannerLogger


The MailLogger logs whatever messages come its way, and then sends them in an e-mail. A group of properties must be set for MailLogger to do its job. These properties can be passed into Ant from the command line using the standard Java -D syntax or, more easily, in <property> statements in your init target. Or, easiest of all, by specifying a properties file. The properties are

  • MailLogger.mailhost—Defaults to "localhost"

  • MailLogger.from—You have to set this to a good "from" address

  • MailLogger.failure.notify—Whether to send an e-mail on build failure; defaults to true

  • MailLogger.success.notify—Whether to send an e-mail on build success; defaults to true

  • MailLogger.failure.to—A comma-separated list of e-mail addresses to send to on build failure

  • MailLogger.success.to—A comma-separated list of e-mail addresses to send to on build success

  • MailLogger.failure.subject—The subject of the e-mail on build failure; defaults to "Build Failure"

  • MailLogger.success.subject—The subject of the e-mail on build success; defaults to "Build Success"

  • MailLogger.properties.file—The name of a properties file that contains properties for the logger

Just like before, to use this logger, you pass Ant the -logger command-line option and the logger's class name as in Listing 3.72.

Listing 3.72 Using the MailLogger

ant -logger org.apache.tools.ant.listener.MailLogger


If you're running Ant inside an XTerm window that supports color or a Windows 9x command shell with ANSI.SYS loaded, the AnsiColorLogger can produce color output. Unfortunately, it won't work under Windows NT/2000 in the CMD shell. I also tested it using 4NT (a popular Cmd.exe replacement), but it didn't work there either.

You can specify certain ANSI color sequences for different types of messages based on severity. In other words, info messages are a different color than errors. You don't have to specify colors; you can just use the defaults. There are lots of codes that you can use to specify which colors you want. Consult the Ant documentation for the full list. Listing 3.73 shows the command line to use this logger.

Listing 3.73 Using the AnsiColorLogger

ant -logger org.apache.tools.ant.listener.AnsiColorLogger


With XML being such a hot topic, and everything seemingly needing to import, export, and support XML, Ant comes with a logger that logs everything to an XML file. This logger behaves differently depending on whether it is invoked as a listener or a logger. In listener mode, the property called XmlLogger.file (set in your init target, at the command line with -D, or in a properties file), determines where the XML output goes. If this property is not set, the output goes to a file called log.xml. In logger mode, all output goes to the console unless the -logger command-line option is used and specifies a file to write to.

You can set a property called ant.XmlLogger.stylesheet to point to the XSL stylesheet of your choice. If you don't specify a stylesheet, the XML file will point to one called log.xsl in the current directory. If you don't want stylesheet support built into your XML file, set this property to an empty string. Listing 3.74 shows command lines for starting Ant with the XmlLogger in both listener and logger modes.

Listing 3.74 Both Ways to Use XmlLogger

ant -listener org.apache.tools.ant.XmlLogger
ant -logger org.apache.tools.ant.XmlLogger -logfile output.xml


Finally, we come to the sole listener amongst the loggers, Log4jListener. You need to have Log4j installed and configured for this listener to work. What happens is that each listener event is delivered to Log4j with the class name of the sender of the event as the Log4j Category. All the start events are sent as INFO-level messages; finish events are sent as INFO if they succeed, and ERROR otherwise. Your Log4j settings will determine what gets sent to your log. You certainly have the most freedom and capability using this listener, but it would most likely be for very specialized uses. Listing 3.75 shows how to invoke Ant with the Log4jListener running.

Listing 3.75 Using the Log4jListener

ant -listener org.apache.tools.ant.Log4jListener
  • + Share This
  • 🔖 Save To Your Account