A Checkstyle listener monitors the progress of a Checker
during the audit of files. The Checker
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
Checker
. An audit always adds one of
the distribution listeners, DefaultLogger
or XMLLogger,
to report events. A DefaultLogger
produces simple text output for the events it receives, and a
XMLLogger
produces an XML document for
its events.
Listeners DefaultLogger
and XMLLogger
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.
A listener is an implementation of the AuditListener
interface. During an audit, a Checker
informs its attached AuditListeners
of
six kinds of events: audit started/ended, file started/ended,
and logging of an error/exception.
An audit passes an event to a listener as an AuditEvent.
A file-related AuditEvent
contains the
name of that file. An AuditEvent
for
error logging has a message, a severity level, a message source
such as the name of a Check
, and file
line and column numbers that may be relevant to the error. The
notification of an exception to a AuditListener
includes an error AuditEvent
and the details of the
exception. Here is a UML diagram for classes AuditListener
and AuditEvent
.
A custom listener is an implementation of the AuditListener
interface. If the listener has properties that can be set from a
configuration, the listener must extend AutomaticBean.
An AutomaticBean
uses JavaBean
introspection to set JavaBean properties.
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 System.out
. In order to enable the
specification of output to a file through property file
, the class extends AutomaticBean
and defines method setFile(String)
.
A listener that filters error events could perform the filtering
in methods addError
and addException
. As further examples of
listeners, CommonsLoggingListener
reports its events through the Commons Logging API, and MailLogger e-mails the audit report of a
DefaultLogger
.
To incorporate a custom listener in the set of listeners for a
Checker
, include a module element for
the listener in the configuration file. For
example, to configure a Checker
so
that it uses custom listener VerboseListener
to print audit messages to a
file named "audit.txt", include the following module
in the configuration file:
Here is a truncated example of audit output from a VerboseListener
:
This section describes two examples based on ANT listeners. The first
listener, CommonsLoggingListener
,
hands off events to the Apache
Commons Logging facade and the second, MailLogger
, sends a report of an audit via
e-mail. The discussion of these examples and how to use them is
derived from material in "Java Development
with Ant" by Eric Hatcher and Steve Loughran, an
excellent ANT book.
Apache
Commons Logging provides a facade for logging tools
log4j,
, J2SE 1.4, and others. Checkstyle listener CommonsLoggingListener
responds to an
AuditEvent by handing it off to the current Commons Logging Log.
The source code for CommonsLoggingListener
is in distribution
directory https://github.com/checkstyle/contribution/examples/listeners
. Notice that
each AuditListener
method that
receives an AuditEvent
calls a method
for the Commons Logging log level corresponding to the
Checkstyle SeverityLevel
of the AuditEvent
.
The easiest way to use CommonsLoggingListener
is to include checkstyle-${projectVersion}-all.jar
in the classpath because that jar file contains all the Commons
Logging classes. The default Log under J2SE 1.4 is wrapper
class Jdk14Logger.
Under earlier Java versions, the default Log is a simple wrapper
class, SimpleLog.
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.
Logging configuration details for Jakarta Commons Logging are in
the documentation.
As a simple example, assume that log4j.jar
is in the classpath and the
following log4j.properties
file is
in the current directory:
Running a Checkstyle audit with a CommonsLoggingListener
yields this
(abbreviated) output:
MailLogger
sends an audit report in an
email message. The listener uses a DefaultLogger
to prepare the text of the
message. The listener obtains other message parameters such as
to
and subject
from environment properties that
can be read from a properties file.
The source code for CommonsLoggingListener
is in distribution
directory https://github.com/checkstyle/contribution/examples/listeners
. This
implementation uses the JavaMail API as
the mail system, and you must include appropriate jar files in
the classpath.
As an example of using MailLogger
, set
system property -DMailLogger.properties.file=MailLogger.properties
,
so that MailLogger
reads message
parameters from file MailLogger.properties
of the current
directory:
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.
We need your help to keep improving Checkstyle. Whenever you write a listener that you think is generally useful, please consider contributing it to the Checkstyle community and submit it for inclusion in the next release of Checkstyle.