<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <!-- Content Stylesheet for Site --> <!-- start the processing --> <!-- ====================================================================== --> <!-- GENERATED FILE, DO NOT EDIT, EDIT THE XML FILE IN xdocs INSTEAD! --> <!-- Main Page Section --> <!-- ====================================================================== --> <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type" /> <meta name="author" content="Lars K?hne"> <meta name="email" content="$au.getAttributeValue("email")"> <title>Checkstyle - Writing Listeners</title> <style type="text/css" media="all"> @import url("./css/cs-layout.css"); @import url("./css/cs-style.css"); </style> </head> <body> <div class="csTopBar"> <div class="csTopBarContents"> <div class="csLeft"> <h1>Writing Listeners</h1> </div> <div class="csRight"> <a href="http://checkstyle.sourceforge.net/"> <img src="images/logo.png" alt="Checkstyle Logo" height="70"/> </a> </div> </div> </div> <div class="csMiddleContainer"> <div class="csLeftBar"> <div class="csLeftBarContents"> <!-- ============================================================ --> <!-- start makeNavigationBar --> <ul> <li> <strong>About</strong> </li> <li> <a href="./index.html">Checkstyle</a> </li> <li> <strong>Documentation</strong> </li> <li> <a href="./releasenotes.html">Release Notes</a> </li> <li> <a href="./config.html">Configuration</a> </li> <li> <a href="./property_types.html"> Property Types</a> </li> <li> <a href="./running.html">Running</a> </li> <li> <a href="./anttask.html"> Ant Task</a> </li> <li> <a href="./cmdline.html"> Command Line</a> </li> <li> <a href="./availablechecks.html">Available Checks</a> </li> <li> <a href="./checks.html">Standard Checks</a> </li> <li> <a href="./config_javadoc.html"> Javadoc Comments</a> </li> <li> <a href="./config_naming.html"> Naming Conventions</a> </li> <li> <a href="./config_header.html"> Headers</a> </li> <li> <a href="./config_imports.html"> Imports</a> </li> <li> <a href="./config_sizes.html"> Size Violations</a> </li> <li> <a href="./config_whitespace.html"> Whitespace</a> </li> <li> <a href="./config_modifier.html"> Modifiers</a> </li> <li> <a href="./config_blocks.html"> Block Checks</a> </li> <li> <a href="./config_coding.html"> Coding</a> </li> <li> <a href="./config_design.html"> Class Design</a> </li> <li> <a href="./config_duplicates.html"> Duplicate Code</a> </li> <li> <a href="./config_metrics.html"> Metrics</a> </li> <li> <a href="./config_misc.html"> Miscellaneous</a> </li> <li> <a href="./optional.html">Optional Checks</a> </li> <li> <a href="./config_j2ee.html"> J2EE Checks</a> </li> <li> <a href="./extending.html">Extending Checkstyle</a> </li> <li> <a href="./writingchecks.html"> Writing checks</a> </li> <li> <a href="./writingfilters.html"> Writing filters</a> </li> <li><b> <a href="./writinglisteners.html"> Writing listeners</a> </b></li> <li><a href="#Overview"> Overview</a></li> <li><a href="#Writing Listeners"> Writing Listeners</a></li> <li><a href="#Using Listeners"> Using Listeners</a></li> <li><a href="#Examples"> Examples</a></li> <li><a href="#Huh? I can't figure it out!"> Huh? I can't figure it out!</a></li> <li><a href="#Contributing"> Contributing</a></li> <li> <a href="./api/index.html">Javadoc</a> </li> <li> <strong>SourceForge</strong> </li> <li> <a href="http://sourceforge.net/mail/?group_id=29721">Mailing Lists</a> </li> <li> <a href="http://sourceforge.net/tracker/?atid=397078&group_id=29721&func=browse">Bug Database</a> </li> <li> <a href="http://sourceforge.net/tracker/?atid=397081&group_id=29721&func=browse">Feature Requests</a> </li> <li> <strong>Developers</strong> </li> <li> <a href="http://sourceforge.net/projects/checkstyle">Project Page</a> </li> <li> <a href="http://checkstyle.svn.sourceforge.net/viewvc/checkstyle/">SVN Repository</a> </li> <li> <a href="./contributing.html">Contributing</a> </li> </ul> <ul> <li> <strong>Hosted by:</strong> <div align="center"> <p><a href="http://sourceforge.net"> <img src="http://sourceforge.net/sflogo.php?group_id=29721&type=1" width="88" height="31" alt="SourceForge Logo"/></a> </p> </div> </li> </ul> <!-- end makeNavigationBar --> <!-- ============================================================ --> </div> </div> <div class="csMain"> <a id="Overview"></a> <h2>Overview</h2> <p> A Checkstyle listener monitors the progress of a <span class="code">Checker</span> during the audit of files. The <span class="code">Checker</span> notifies its attached listeners of significant events such as the start of the audit of a file and the logging of a Check error, and the listeners respond appropriately. Any number of listeners can be attached to a <span class="code"> Checker</span>. An audit always adds one of the distribution listeners, <a href="api/com/puppycrawl/tools/checkstyle/DefaultLogger.html">DefaultLogger</a> or <a href="api/com/puppycrawl/tools/checkstyle/XMLLogger.html">XMLLogger</a>, to report events. A <span class="code">DefaultLogger</span> produces simple text output for the events it receives, and a <span class="code">XMLLogger</span> produces an XML document for its events. </p> <p> Listeners <span class="code">DefaultLogger</span> and <span class="code"> XMLLogger</span> are sufficient for most Checkstyle users, but you may find a need for a custom listener. For example, a user has requested verbose output of progress information during a Checkstyle run. Another user would like to filter error events. This document explains how to write listeners for such tasks and how to integrate them in a Checker module. It also describes two custom listeners that are inspired by ANT listeners: a listener that is a wrapper for the Jakarta Commons Logging API, and a listener that sends its results via email. </p> <p> A listener is an implementation of the <a href="api/com/puppycrawl/tools/checkstyle/api/AuditListener.html">AuditListener</a> interface. During an audit, a <span class="code">Checker</span> informs its attached <span class="code">AuditListeners</span> of six kinds of events: audit started/ended, file started/ended, and logging of an error/exception. </p> <p> An audit passes an event to a listener as an <a href="api/com/puppycrawl/tools/checkstyle/api/AuditEvent.html">AuditEvent</a>. A file-related <span class="code">AuditEvent</span> contains the name of that file. An <span class="code">AuditEvent</span> for error logging has a message, a severity level, a message source such as the name of a <span class="code">Check</span>, and file line and column numbers that may be relevant to the error. The notification of an exception to a <span class="code">AuditListener</span> includes an error <span class="code">AuditEvent</span> and the details of the exception. Here is a UML diagram for classes <span class="code">AuditListener</span> and <span class="code">AuditEvent</span>. </p> <image src="images/AuditListener.gif" width="381" height="488" alt="AuditListener UML diagram" /> <a id="Writing Listeners"></a> <h2>Writing Listeners</h2> <p> A custom listener is an implementation of the <a href="api/com/puppycrawl/tools/checkstyle/api/AuditListener.html">AuditListener</a> interface. If the listener has properties that can be set from a configuration, the listener must extend <a href="api/com/puppycrawl/tools/checkstyle/api/AutomaticBean.html">AutomaticBean</a>. An <span class="code">AutomaticBean</span> uses JavaBean introspection to set JavaBean properties. </p> <p> The custom listener that we demonstrate here is a verbose listener that simply prints each event notification to an output stream, and reports the number of errors per audited file and the total number of errors. The default output stream is <span class="code">System.out</span>. In order to enable the specification of output to a file through property <span class="code">file</span>, the class extends <span class="code">AutomaticBean</span> and defines method <span class="code">setFile(String)</span>. </p> <div align="left"> <table cellspacing="4" cellpadding="0" border="0"> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#ffffff"><pre>package com.mycompany.listeners; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.PrintWriter; import com.puppycrawl.tools.checkstyle.api.AuditEvent; import com.puppycrawl.tools.checkstyle.api.AuditListener; import com.puppycrawl.tools.checkstyle.api.AutomaticBean; import com.puppycrawl.tools.checkstyle.api.SeverityLevel; public class VerboseListener extends AutomaticBean implements AuditListener { private PrintWriter mWriter = new PrintWriter(System.out); private boolean mCloseOut = false; private int mTotalErrors; private int mErrors; public void setFile(String aFileName) throws FileNotFoundException { final OutputStream out = new FileOutputStream(aFileName); mWriter = new PrintWriter(out); mCloseOut = true; } public void auditStarted(AuditEvent aEvt) { mTotalErrors = 0; mWriter.println("Audit started."); } public void auditFinished(AuditEvent aEvt) { mWriter.println("Audit finished. Total errors: " + mTotalErrors); mWriter.flush(); if (mCloseOut) { mWriter.close(); } } public void fileStarted(AuditEvent aEvt) { mErrors = 0; mWriter.println( "Started checking file '" + aEvt.getFileName() + "'."); } public void fileFinished(AuditEvent aEvt) { mWriter.println("Finished checking file '" + aEvt.getFileName() + "'. Errors: " + mErrors); } public void addError(AuditEvent aEvt) { printEvent(aEvt); if (SeverityLevel.ERROR.equals(aEvt.getSeverityLevel())) { mErrors++; mTotalErrors++; } } public void addException(AuditEvent aEvt, Throwable aThrowable) { printEvent(aEvt); aThrowable.printStackTrace(System.out); mErrors++; mTotalErrors++; } private void printEvent(AuditEvent aEvt) { mWriter.println("Logging error -" + " file: '" + aEvt.getFileName() + "'" + " line: " + aEvt.getLine() + " column: " + aEvt.getColumn() + " severity: " + aEvt.getSeverityLevel() + " message: " + aEvt.getMessage() + " source: " + aEvt.getSourceName()); } }</pre></td> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> </table> </div> <p> A listener that filters error events could perform the filtering in methods <span class="code"> addError</span> and <span class="code">addException</span>. As further examples of listeners, <a href="#CommonsLoggingListener">CommonsLoggingListener</a> reports its events through the Commons Logging API, and <a href="#MailLogger">MailLogger</a> e-mails the audit report of a <span class="code">DefaultLogger</span>. </p> <a id="Using Listeners"></a> <h2>Using Listeners</h2> <p> To incorporate a custom listener in the set of listeners for a <span class="code">Checker</span>, include a module element for the listener in the <a href="config.html#auditlisteners">configuration file</a>. For example, to configure a <span class="code">Checker</span> so that it uses custom listener <span class="code">VerboseListener</span> to print audit messages to a file named "audit.txt", include the following module in the configuration file: </p> <div align="left"> <table cellspacing="4" cellpadding="0" border="0"> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#ffffff"><pre><module name="com.mycompany.listeners.VerboseListener"> <property name="file" value="audit.txt"/> </module></pre></td> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> </table> </div> <p> Here is a truncated example of audit output from a <span class="code"> VerboseListener</span>: </p> <div align="left"> <table cellspacing="4" cellpadding="0" border="0"> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#ffffff"><pre>Audit started. Started checking file 'CommonsLoggingListener.java'. Finished checking file 'CommonsLoggingListener.java'. Errors: 0 Started checking file 'MailLogger.java'. Finished checking file 'MailLogger.java'. Errors: 0 Started checking file 'VerboseListener.java'. Logging error - file: 'VerboseListener.java' line: 23 ... Finished checking file 'VerboseListener.java'. Errors: 1 Audit finished. Total errors: 1</pre></td> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> </table> </div> <a id="Examples"></a> <h2>Examples</h2> <p> This section describes two examples based on <a href="http://ant.apache.org/">ANT</a> listeners. The first listener, <span class="code"> CommonsLoggingListener</span>, hands off events to the <a href="http://jakarta.apache.org/commons/logging.html">Jakarata Commons Logging</a> facade and the second, <span class="code">MailLogger</span>, sends a report of an audit via e-mail. The discussion of these examples and how to use them is derived from material in <a href="http://www.manning.com/antbook">"Java Development with Ant"</a> by Eric Hatcher and Steve Loughran, an excellent ANT book. </p> <h4>CommonsLoggingListener</h4> <p> <a href="http://jakarta.apache.org/commons/logging.html">Jakarata Commons Logging</a> provides a facade for logging tools <a href="http://jakarta.apache.org/log4j/docs/index.html">log4j</a>, <a href="http://jakarta.apache.org/avalon/logkit/index.html">Avalon LogKit</a>, J2SE 1.4, and others. Checkstyle listener <span class="code"> CommonsLoggingListener</span> responds to an AuditEvent by handing it off to the current Commons Logging Log. </p> <p> The source code for <span class="code">CommonsLoggingListener</span> is in distribution directory <span class="default">contrib/examples/listeners</span>. Notice that each <span class="code">AuditListener</span> method that receives an <span class="code"> AuditEvent</span> calls a method for the Commons Logging log level corresponding to the Checkstyle <span class="code">SeverityLevel</span> of the <span class="code"> AuditEvent</span>. </p> <p> The easiest way to use <span class="code">CommonsLoggingListener</span> is to include <span class="default">checkstyle-all-4.4.jar</span> in the classpath because that jar file contains all the Commons Logging classes. The default Log under J2SE 1.4 is wrapper class <a href="http://jakarta.apache.org/commons/logging/api/org/apache/commons/logging/impl/Jdk14Logger.html">Jdk14Logger</a>. Under earlier Java versions, the default Log is a simple wrapper class, <a href="http://jakarta.apache.org/commons/logging/api/org/apache/commons/logging/impl/SimpleLog.html">SimpleLog</a>. Both default logging tools can be used directly from Commons Logging; if you need to use other tools such as log4j, then you must include the appropriate jar file(s) in the classpath. </p> <p> Logging configuration details for Jakarta Commons Logging are in the <a href="http://jakarta.apache.org/commons/logging.html">documentation</a>. As a simple example, assume that <span class="default">log4j.jar</span> is in the classpath and the following <span class="default">log4j.properties</span> file is in the current directory: </p> <div align="left"> <table cellspacing="4" cellpadding="0" border="0"> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#ffffff"><pre># Set root logger level to INFO and its only appender to A1. log4j.rootLogger=INFO, A1 # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.ConsoleAppender # A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-5p %c %x- %m%n</pre></td> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> </table> </div> <p> Running a Checkstyle audit with a <span class="code">CommonsLoggingListener</span> yields this (abbreviated) output: </p> <div align="left"> <table cellspacing="4" cellpadding="0" border="0"> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#ffffff"><pre>INFO com.puppycrawl...Checker - Audit started. INFO com.puppycrawl...Checker - File "CommonsLoggingListener.java" started. INFO com.puppycrawl...Checker - File "CommonsLoggingListener.java" finished. INFO com.puppycrawl...Checker - File "MailLogger.java" started. INFO com.puppycrawl...Checker - File "MailLogger.java" finished. INFO com.puppycrawl...Checker - File "VerboseListener.java" started. ERROR com.puppycrawl...ParenPadCheck - Line: 23 Column: 28 ... INFO com.puppycrawl...Checker - File "VerboseListener.java" finished. INFO com.puppycrawl...Checker - Audit finished.</pre></td> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> </table> </div> <h4>MailLogger</h4> <p> <span class="code">MailLogger</span> sends an audit report in an email message. The listener uses a <span class="code">DefaultLogger</span> to prepare the text of the message. The listener obtains other message parameters such as <span class="default">to</span> and <span class="default">subject</span> from environment properties that can be read from a properties file. </p> <p> The source code for <span class="code">CommonsLoggingListener</span> is in distribution directory <span class="default">contrib/examples/listeners</span>. This implementation uses the <a href="http://java.sun.com/products/javamail">JavaMail API</a> as the mail system, and you must include appropriate jar files in the classpath. </p> <p> As an example of using <span class="code">MailLogger</span>, set system property <span class="default">-DMailLogger.properties.file=MailLogger.properties</span>, so that <span class="code">MailLogger</span> reads message parameters from file <span class="default">MailLogger.properties</span> of the current directory: </p> <div align="left"> <table cellspacing="4" cellpadding="0" border="0"> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#ffffff"><pre>MailLogger.from=user@example.org MailLogger.failure.to=user@example.org MailLogger.success.to=user@example.org MailLogger.mailhost=localhost</pre></td> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> </table> </div> <a id="Huh? I can't figure it out!"></a> <h2>Huh? I can't figure it out!</h2> <p> That's probably our fault, and it means that we have to provide better documentation. Please do not hesitate to ask questions on the user mailing list, this will help us to improve this document. Please ask your questions as precisely as possible. We will not be able to answer questions like "I want to write a listener but I don't know how, can you help me?". Tell us what you are trying to do (the purpose of the listener), what you have understood so far, and what exactly you are getting stuck on. </p> <a id="Contributing"></a> <h2>Contributing</h2> <p> We need <em>your</em> help to keep improving Checkstyle. Whenever you write a listener that you think is generally useful, please consider <a href="contributing.html">contributing</a> it to the Checkstyle community and submit it for inclusion in the next release of Checkstyle. </p> </div> </div> <div class="csBottomBar"> <div class="csBottomBarContents"> Copyright © 2001-2007, Oliver Burn </div> </div> </body> </html> <!-- end the processing -->