# Listeners & Loggers

## Overview

Ant has two related features to allow the build process to be monitored: listeners and loggers.

### Listeners

A listener is alerted of the following events:

- build started
- build finished
- target started
- target finished
- task started
- task finished
- message logged

### Loggers

Loggers extend the capabilities of listeners and add the following features:

- Receives a handle to the standard output and error print streams and therefore can log information to the console or the -logfile specified file.
- Logging level (-quiet, -verbose, -debug) aware
- Emacs-mode aware

## Built-in Listeners/Loggers

Classname | Description | Type
`org.apache.tools.ant.DefaultLogger` | The logger used implicitly unless overridden with the `-logger` command-line switch. | BuildLogger
`org.apache.tools.ant.NoBannerLogger` | This logger omits output of empty target output. | BuildLogger
`org.apache.tools.ant.listener.MailLogger` | Extends DefaultLogger such that output is still generated the same, and when the build is finished an e-mail can be sent. | BuildLogger
`org.apache.tools.ant.listener.AnsiColorLogger` | Colorifies the build output. | BuildLogger
`org.apache.tools.ant.listener.Log4jListener` | Passes events to Log4j for highly customizable logging. | BuildListener
`org.apache.tools.ant.XmlLogger` | Writes the build information to an XML file. | BuildLogger

### DefaultLogger

Simply run Ant normally, or:

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

### NoBannerLogger

Removes output of empty target output.

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

### MailLogger

The MailLogger captures all output logged through DefaultLogger (standard Ant output) and will send success and failure messages to unique e-mail lists, with control for turning off success or failure messages individually.

Properties controlling the operation of MailLogger:

Property | Description | Required
--- | --- | ---
MailLogger.mailhost | Mail server to use | No, default "localhost"
MailLogger.port | SMTP Port for the Mail server | No, default "25"
MailLogger.user | user name for SMTP auth | Yes, if SMTP auth is required on your SMTP server the email message will be then sent using Mime and requires JavaMail
MailLogger.password | password for SMTP auth | Yes, if SMTP auth is required on your SMTP server the email message will be then sent using Mime and requires JavaMail
MailLogger.ssl | on or true if ssl is needed This feature requires JavaMail | no
MailLogger.from | Mail "from" address | Yes, if mail needs to be sent
MailLogger.replyto | Mail "replyto" address(es), comma-separated | No
MailLogger.failure.notify | Send build failure e-mails? | No, default "true"
MailLogger.success.notify | Send build success e-mails? | No, default "true"
MailLogger.failure.to | Address(es) to send failure messages to, comma-separated | Yes, if failure mail is to be sent
MailLogger.success.to | Address(es) to send success messages to, comma-separated | Yes, if success mail is to be sent
MailLogger.failure.subject | Subject of failed build | No, default "Build Failure"
MailLogger.success.subject | Subject of successful build | No, default "Build Success"
MailLogger.properties.file | Filename of properties file that will override other values. | No

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

### AnsiColorLogger

The AnsiColorLogger adds color to the standard Ant output by prefixing and suffixing ANSI color code escape sequences to it. It is just an extension of DefaultLogger and hence provides all features that DefaultLogger does.

AnsiColorLogger differentiates the output by assigning different colors depending upon the type of the message.

If used with the -logfile option, the output file will contain all the necessary escape codes to display the text in colorized mode when displayed in the console using applications like cat, more, etc.

This is designed to work on terminals that support ANSI color codes. It works on XTerm, ETerm, Win9x Console (with ANSI.SYS loaded.), etc.

NOTE: It doesn't work on WinNT even when a COMMAND.COM console loaded with ANSI.SYS is used.

If the user wishes to override the default colors with custom ones, a file containing zero or more of the custom color key-value pairs must be created. The recognized keys and their default values are shown below:

`

    AnsiColorLogger.ERROR_COLOR=2;31
    AnsiColorLogger.WARNING_COLOR=2;35
    AnsiColorLogger.INFO_COLOR=2;36
    AnsiColorLogger.VERBOSE_COLOR=2;32
    AnsiColorLogger.DEBUG_COLOR=2;34

`

Each key takes as value a color combination defined as Attribute;Foreground;Background. In the above example, background value has not been used.

This file must be specfied as the value of a system variable named ant.logger.defaults and passed as an argument using the -D option to the java command that invokes the Ant application. An easy way to achieve this is to add -Dant.logger.defaults= /path/to/your/file to the ANT_OPTS environment variable. Ant's launching script recognizes this flag and will pass it to the java command appropriately.

Format:

    AnsiColorLogger.*=Attribute;Foreground;Background

    Attribute is one of the following:
    0 -> Reset All Attributes (return to normal mode)
    1 -> Bright (Usually turns on BOLD)
    2 -> Dim
    3 -> Underline
    5 -> link
    7 -> Reverse
    8 -> Hidden

    Foreground is one of the following:
    30 -> Black
    31 -> Red
    32 -> Green
    33 -> Yellow
    34 -> Blue
    35 -> Magenta
    36 -> Cyan
    37 -> White

    Background is one of the following:
    40 -> Black
    41 -> Red
    42 -> Green
    43 -> Yellow
    44 -> Blue
    45 -> Magenta
    46 -> Cyan
    47 -> White

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

### Log4jListener

Passes build events to Log4j, using the full classname's of the generator of each build event as the category:

- build started / build finished - org.apache.tools.ant.Project
- target started / target finished - org.apache.tools.ant.Target
- task started / task finished - the fully qualified classname of the task
- message logged - the classname of one of the above, so if a task logs a message, its classname is the category used, and so on.

All start events are logged as INFO. Finish events are either logged as INFO or ERROR depending on whether the build failed during that stage. Message events are logged according to their Ant logging level, mapping directly to a corresponding Log4j level.

> `ant -listener org.apache.tools.ant.listener.Log4jListener`

### XmlLogger

Writes all build information out to an XML file named log.xml, or the value of the `XmlLogger.file` property if present, when used as a listener. When used as a logger, it writes all output to either the console or to the value of `-logfile`. Whether used as a listener or logger, the output is not generated until the build is complete, as it buffers the information in order to provide timing information for task, targets, and the project.

By default the XML file creates a reference to an XSLT file "log.xsl" in the current directory; look in ANT_HOME/etc for one of these. You can set the property `ant.XmlLogger.stylesheet.uri` to provide a uri to a style sheet. this can be a relative or absolute file path, or an http URL. If you set the property to the empty string, "", no XSLT transform is declared at all.

> `ant -listener org.apache.tools.ant.XmlLogger`
> `ant -logger org.apache.tools.ant.XmlLogger -verbose -logfile build_log.xml`

## Writing your own

See the Build Events section for developers.

Notes:

- A listener or logger should not write to standard output or error - Ant captures these internally and may cause an infinite loop.

Copyright © 2000-2003 Apache Software Foundation. All rights Reserved.