You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

listeners.html 14 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <html>
  16. <head>
  17. <meta http-equiv="Content-Language" content="en-us">
  18. <link rel="stylesheet" type="text/css" href="stylesheets/style.css">
  19. <title>Listeners &amp; Loggers</title>
  20. </head>
  21. <body>
  22. <h1>Listeners &amp; Loggers</h1>
  23. <h2><a name="Overview">Overview</a></h2>
  24. <p>Ant has two related features to allow the build process to be monitored:
  25. listeners and loggers.</p>
  26. <h3><a name="Listeners">Listeners</a></h3>
  27. <p>A listener is alerted of the following events:</p>
  28. <ul>
  29. <li>build started</li>
  30. <li>build finished</li>
  31. <li>target started</li>
  32. <li>target finished</li>
  33. <li>task started</li>
  34. <li>task finished</li>
  35. <li>message logged</li>
  36. </ul>
  37. <h3><a name="Loggers">Loggers</a></h3>
  38. <p>Loggers extend the capabilities of listeners and add the following features:</p>
  39. <ul>
  40. <li>Receives a handle to the standard output and error print streams and
  41. therefore can log information to the console or the -logfile specified file.</li>
  42. <li>Logging level (-quiet, -verbose, -debug) aware</li>
  43. <li>Emacs-mode aware</li>
  44. </ul>
  45. <h2><a name="builtin">Built-in Listeners/Loggers</a></h2>
  46. <table border="1" cellspacing="1" width="100%" id="AutoNumber1">
  47. <tr>
  48. <td width="33%">Classname</td>
  49. <td width="33%">Description</td>
  50. <td width="34%">Type</td>
  51. </tr>
  52. <tr>
  53. <td width="33%"><code><a href="#DefaultLogger">org.apache.tools.ant.DefaultLogger</a></code></td>
  54. <td width="33%">The logger used implicitly unless overridden with the
  55. <code>-logger</code> command-line switch.</td>
  56. <td width="34%">BuildLogger</td>
  57. </tr>
  58. <tr>
  59. <td width="33%"><code><a href="#NoBannerLogger">
  60. org.apache.tools.ant.NoBannerLogger</a></code></td>
  61. <td width="33%">This logger omits output of empty target output.</td>
  62. <td width="34%">BuildLogger</td>
  63. </tr>
  64. <tr>
  65. <td width="33%"><code><a href="#MailLogger">
  66. org.apache.tools.ant.listener.MailLogger</a></code></td>
  67. <td width="33%">Extends DefaultLogger such that output is still generated
  68. the same, and when the build is finished an e-mail can be sent.</td>
  69. <td width="34%">BuildLogger</td>
  70. </tr>
  71. <tr>
  72. <td width="33%"><code><a href="#AnsiColorLogger">
  73. org.apache.tools.ant.listener.AnsiColorLogger</a></code></td>
  74. <td width="33%">Colorifies the build output.</td>
  75. <td width="34%">BuildLogger</td>
  76. </tr>
  77. <tr>
  78. <td width="33%"><code><a href="#Log4jListener">
  79. org.apache.tools.ant.listener.Log4jListener</a></code></td>
  80. <td width="33%">Passes events to Log4j for highly customizable logging.</td>
  81. <td width="34%">BuildListener</td>
  82. </tr>
  83. <tr>
  84. <td width="33%"><code><a href="#XmlLogger">org.apache.tools.ant.XmlLogger</a></code></td>
  85. <td width="33%">Writes the build information to an XML file.</td>
  86. <td width="34%">BuildLogger</td>
  87. </tr>
  88. <tr>
  89. <td width="33%"><code><a href="#TimestampedLogger">org.apache.tools.ant.TimestampedLogger</a></code></td>
  90. <td width="33%">Prints the time that a build finished</td>
  91. <td width="34%">BuildLogger</td>
  92. </tr>
  93. </table>
  94. <h3><a name="DefaultLogger">DefaultLogger</a></h3>
  95. <p>Simply run Ant normally, or:</p>
  96. <blockquote>
  97. <p><code>ant -logger org.apache.tools.ant.DefaultLogger</code></p>
  98. </blockquote>
  99. <h3><a name="NoBannerLogger">NoBannerLogger</a></h3>
  100. <p>Removes output of empty target output.</p>
  101. <blockquote>
  102. <p><code>ant -logger org.apache.tools.ant.NoBannerLogger</code></p>
  103. </blockquote>
  104. <h3><a name="MailLogger">MailLogger</a></h3>
  105. <p>The MailLogger captures all output logged through DefaultLogger (standard Ant
  106. output) and will send success and failure messages to unique e-mail lists, with
  107. control for turning off success or failure messages individually.</p>
  108. <p>Properties controlling the operation of MailLogger:</p>
  109. <table border="1" cellspacing="1" width="100%" id="AutoNumber2">
  110. <tr>
  111. <th width="337">Property</th>
  112. <th width="63%">Description</th>
  113. <th width="63%">Required</th>
  114. </tr>
  115. <tr>
  116. <td width="337">MailLogger.mailhost </td>
  117. <td width="63%">Mail server to use</td>
  118. <td width="63%">No, default &quot;localhost&quot;</td>
  119. </tr>
  120. <tr>
  121. <td width="337">MailLogger.port </td>
  122. <td width="63%">SMTP Port for the Mail server</td>
  123. <td width="63%">No, default &quot;25&quot;</td>
  124. </tr>
  125. <tr>
  126. <td width="337">MailLogger.user</td>
  127. <td width="63%">user name for SMTP auth</td>
  128. <td width="63%">Yes, if SMTP auth is required on your SMTP server<br>
  129. the email message will be then sent using Mime and requires JavaMail</td>
  130. </tr>
  131. <tr>
  132. <td width="337">MailLogger.password</td>
  133. <td width="63%">password for SMTP auth</td>
  134. <td width="63%">Yes, if SMTP auth is required on your SMTP server<br>
  135. the email message will be then sent using Mime and requires JavaMail</td>
  136. </tr>
  137. <tr>
  138. <td width="337">MailLogger.ssl</td>
  139. <td width="63%">on or true if ssl is needed<br>
  140. This feature requires JavaMail</td>
  141. <td width="63%">
  142. no</td>
  143. </tr>
  144. <tr>
  145. <td width="337">MailLogger.from</td>
  146. <td width="63%">Mail &quot;from&quot; address</td>
  147. <td width="63%">Yes, if mail needs to be sent</td>
  148. </tr>
  149. <tr>
  150. <td width="337">MailLogger.replyto</td>
  151. <td width="63%">Mail &quot;replyto&quot; address(es), comma-separated</td>
  152. <td width="63%">No</td>
  153. </tr>
  154. <tr>
  155. <td width="337">MailLogger.failure.notify </td>
  156. <td width="63%">Send build failure e-mails?</td>
  157. <td width="63%">No, default &quot;true&quot;</td>
  158. </tr>
  159. <tr>
  160. <td width="337">MailLogger.success.notify </td>
  161. <td width="63%">Send build success e-mails?</td>
  162. <td width="63%">No, default &quot;true&quot;</td>
  163. </tr>
  164. <tr>
  165. <td width="337">MailLogger.failure.to </td>
  166. <td width="63%">Address(es) to send failure messages to, comma-separated</td>
  167. <td width="63%">Yes, if failure mail is to be sent</td>
  168. </tr>
  169. <tr>
  170. <td width="337">MailLogger.success.to </td>
  171. <td width="63%">Address(es) to send success messages to, comma-separated</td>
  172. <td width="63%">Yes, if success mail is to be sent</td>
  173. </tr>
  174. <tr>
  175. <td width="337">MailLogger.failure.subject </td>
  176. <td width="63%">Subject of failed build</td>
  177. <td width="63%">No, default &quot;Build Failure&quot;</td>
  178. </tr>
  179. <tr>
  180. <td width="337">MailLogger.success.subject </td>
  181. <td width="63%">Subject of successful build</td>
  182. <td width="63%">No, default &quot;Build Success&quot;</td>
  183. </tr>
  184. <tr>
  185. <td width="337">MailLogger.properties.file </td>
  186. <td width="63%">Filename of properties file that will override other values.</td>
  187. <td width="63%">No</td>
  188. </tr>
  189. </table>
  190. <blockquote>
  191. <p><code>ant -logger org.apache.tools.ant.listener.MailLogger</code></p>
  192. </blockquote>
  193. <h3><a name="AnsiColorLogger">AnsiColorLogger</a></h3>
  194. <p>The AnsiColorLogger adds color to the standard Ant output
  195. by prefixing and suffixing ANSI color code escape sequences to
  196. it. It is just an extension of <a href="#DefaultLogger">DefaultLogger</a>
  197. and hence provides all features that DefaultLogger does.</p>
  198. <p>AnsiColorLogger differentiates the output by assigning
  199. different colors depending upon the type of the message.</p>
  200. <p>If used with the -logfile option, the output file
  201. will contain all the necessary escape codes to
  202. display the text in colorized mode when displayed
  203. in the console using applications like cat, more, etc.</p>
  204. <p>This is designed to work on terminals that support ANSI
  205. color codes. It works on XTerm, ETerm, Win9x Console
  206. (with ANSI.SYS loaded.), etc.</p>
  207. <p><Strong>NOTE:</Strong>
  208. It doesn't work on WinNT even when a COMMAND.COM console loaded with
  209. ANSI.SYS is used.</p>
  210. <p>If the user wishes to override the default colors
  211. with custom ones, a file containing zero or more of the
  212. custom color key-value pairs must be created. The recognized keys
  213. and their default values are shown below:</p><code><pre>
  214. AnsiColorLogger.ERROR_COLOR=2;31
  215. AnsiColorLogger.WARNING_COLOR=2;35
  216. AnsiColorLogger.INFO_COLOR=2;36
  217. AnsiColorLogger.VERBOSE_COLOR=2;32
  218. AnsiColorLogger.DEBUG_COLOR=2;34</pre></code>
  219. <p>Each key takes as value a color combination defined as
  220. <b>Attribute;Foreground;Background</b>. In the above example, background
  221. value has not been used.</p>
  222. <p>This file must be specfied as the value of a system variable
  223. named ant.logger.defaults and passed as an argument using the -D
  224. option to the <b>java</b> command that invokes the Ant application.
  225. An easy way to achieve this is to add -Dant.logger.defaults=
  226. <i>/path/to/your/file</i> to the ANT_OPTS environment variable.
  227. Ant's launching script recognizes this flag and will pass it to
  228. the java command appropriately.</p>
  229. <p>Format:</p><pre>
  230. AnsiColorLogger.*=Attribute;Foreground;Background
  231. Attribute is one of the following:
  232. 0 -&gt; Reset All Attributes (return to normal mode)
  233. 1 -&gt; Bright (Usually turns on BOLD)
  234. 2 -&gt; Dim
  235. 3 -&gt; Underline
  236. 5 -&gt; link
  237. 7 -&gt; Reverse
  238. 8 -&gt; Hidden
  239. Foreground is one of the following:
  240. 30 -&gt; Black
  241. 31 -&gt; Red
  242. 32 -&gt; Green
  243. 33 -&gt; Yellow
  244. 34 -&gt; Blue
  245. 35 -&gt; Magenta
  246. 36 -&gt; Cyan
  247. 37 -&gt; White
  248. Background is one of the following:
  249. 40 -&gt; Black
  250. 41 -&gt; Red
  251. 42 -&gt; Green
  252. 43 -&gt; Yellow
  253. 44 -&gt; Blue
  254. 45 -&gt; Magenta
  255. 46 -&gt; Cyan
  256. 47 -&gt; White</pre>
  257. <blockquote>
  258. <p><code>ant -logger org.apache.tools.ant.listener.AnsiColorLogger</code></p>
  259. </blockquote>
  260. <h3><a name="Log4jListener">Log4jListener</a></h3>
  261. <p>Passes build events to Log4j, using the full classname's of the generator of
  262. each build event as the category:</p>
  263. <ul>
  264. <li>build started / build finished - org.apache.tools.ant.Project</li>
  265. <li>target started / target finished - org.apache.tools.ant.Target</li>
  266. <li>task started / task finished - the fully qualified classname of the task</li>
  267. <li>message logged - the classname of one of the above, so if a task logs a
  268. message, its classname is the category used, and so on.</li>
  269. </ul>
  270. <p>All start events are logged as INFO.&nbsp; Finish events are either logged as
  271. INFO or ERROR depending on whether the build failed during that stage. Message
  272. events are logged according to their Ant logging level, mapping directly to a
  273. corresponding Log4j level.</p>
  274. <blockquote>
  275. <p><code>ant -listener org.apache.tools.ant.listener.Log4jListener</code></p>
  276. </blockquote>
  277. <p>To use Log4j you will need the Log4j jar file and a 'log4j.properties'
  278. configuration file. Both should be placed somewhere in your Ant
  279. classpath. If the log4j.properties is in your project root folder you can
  280. add this with <i>-lib</i> option:</p>
  281. <blockquote>
  282. <pre><code>ant -listener org.apache.tools.ant.listener.Log4jListener -lib .</code></pre>
  283. </blockquote>
  284. <p>If, for example, you wanted to capture the same information output to the
  285. console by the DefaultLogger and send it to a file named 'build.log', you
  286. could use the following configuration:</p>
  287. <blockquote>
  288. <pre><code>log4j.rootLogger=ERROR, LogFile
  289. log4j.logger.org.apache.tools.ant.Project=INFO
  290. log4j.logger.org.apache.tools.ant.Target=INFO
  291. log4j.logger.org.apache.tools.ant.taskdefs=INFO
  292. log4j.logger.org.apache.tools.ant.taskdefs.Echo=WARN
  293. log4j.appender.LogFile=org.apache.log4j.FileAppender
  294. log4j.appender.LogFile.layout=org.apache.log4j.PatternLayout
  295. log4j.appender.LogFile.layout.ConversionPattern=[%6r] %8c{1} : %m%n
  296. log4j.appender.LogFile.file=build.log
  297. </code></pre>
  298. </blockquote>
  299. <p>For more information about configuring Log4J see <a href="http://logging.apache.org/log4j/docs/documentation.html">its
  300. documentation page</a>.</p>
  301. <h3><a name="XmlLogger">XmlLogger</a></h3>
  302. <p>Writes all build information out to an XML file named log.xml, or the value
  303. of the <code>XmlLogger.file</code> property if present, when used as a
  304. listener. When used as a logger, it writes all output to either the
  305. console or to the value of <code>-logfile</code>. Whether used as a listener
  306. or logger, the output is not generated until the build is complete, as it
  307. buffers the information in order to provide timing information for task,
  308. targets, and the project.
  309. <p>
  310. By default the XML file creates
  311. a reference to an XSLT file "log.xsl" in the current directory; look in
  312. ANT_HOME/etc for one of these. You can set the property
  313. <code>ant.XmlLogger.stylesheet.uri</code> to provide a uri to a style sheet.
  314. this can be a relative or absolute file path, or an http URL.
  315. If you set the property to the empty string, "", no XSLT transform
  316. is declared at all.
  317. </p>
  318. <blockquote>
  319. <p><code>ant -listener org.apache.tools.ant.XmlLogger</code><br>
  320. <code>ant -logger org.apache.tools.ant.XmlLogger -verbose -logfile build_log.xml</code></p>
  321. </blockquote>
  322. <h3><a name="TimestampedLogger">TimestampedLogger</a></h3>
  323. <p>
  324. Acts like the default logger, except that the final success/failure message also includes
  325. the time that the build completed. For example:
  326. </p>
  327. <pre>
  328. BUILD SUCCESSFUL - at 16/08/05 16:24
  329. </pre>
  330. <blockquote>
  331. <code>ant -logger org.apache.tools.ant.listener.TimestampedLogger</code>
  332. </blockquote>
  333. <h2><a name="dev">Writing your own</a></h2>
  334. <p>See the <a href="develop.html#buildevents">Build Events</a> section for
  335. developers.</p>
  336. <p>Notes:</p>
  337. <ul>
  338. <li>A listener or logger should not write to standard output or error; Ant
  339. captures these internally and may cause an infinite loop.</li>
  340. </ul>
  341. </body>
  342. </html>