User's Manual

Chapter 7. Developing with WAF 47
}
Example 7-9. Adjusting logging level temporarily
In the above example, you end up enabling the PreparedStatement logger only for the duration
of the doSomething() method. This may significantly cut down the number of queries logged, thus
making it easier for you to see what is going on.
The obvious caveat here is that extraneous queries may still end up getting logged in the presence of
multiple threads. This is because the logger’s level cannot be adjusted on a per-thread basis - it can
only be changed globally.
7.5.4. Custom Appenders
Out of the box, WAF provides two appenders: ConsoleAppender
4
, wired to your standard output
device (usually a TTY), and RollingFileAppender
5
tied to a file whose location is configurable.
You can also use other appenders. One of the interesting possibilities is the SocketAppender
6
. It
expects a log viewer (or some other log processing application) to listen on the specified TCP port,
possibly on a different machine. There are a number of GUI log viewers that work with the socket
appender. These viewers give you fine-grained control over your logging output. See, for example,
Chainsaw
7
and Lumbermill
8
. Both of these log viewers allow you to filter the output by level,
logger, thread name, etc.
7.5.5. Beware of Buffered Streams
The Java language provides two abstractions for doing output: output streams
9
for working with
sequences of bytes, and writers
10
for working with sequences of characters. An output stream or a
writer can be buffered or unbuffered. For more information, read about BufferedOutputStream
11
and about BufferedWriter
12
.
When I/O is buffered, it means that bytes or characters that you write do not necessarily go to their
intended output device immediately. They are cached in an intermediate buffer. When the buffer fills
up, it is flushed — an actual write to the underlying physical device occurs.
Why should you care about this? If you log a message to a buffered output immediately before the
system crashes, you may not see the message logged anywhere. The system didn’t have a chance
to flush the buffered stream or writer. This situation is fairly rare, but when it does happen, it may
stump the unwary troubleshooter. If you want to be absolutely sure you are not losing any logging
statements, you must use unbuffered output.
For example, the standard output System.out may be buffered, while the standard error device Sys-
tem.err is usually not. Therefore, System.err is preferable when you want to make sure no logging
is lost.
4. http://jakarta.apache.org/log4j/docs/api/org/apache/log4j/ConsoleAppender.html
5. http://jakarta.apache.org/log4j/docs/api/org/apache/log4j/RollingFileAppender.html
6. http://jakarta.apache.org/log4j/docs/api/org/apache/log4j/net/SocketAppender.html
7. http://jakarta.apache.org/log4j/docs/api/org/apache/log4j/chainsaw/package-summary.html
8. http://traxel.com/lumbermill/
9. http://java.sun.com/j2se/1.3/docs/api/java/io/OutputStream.html
10. http://java.sun.com/j2se/1.3/docs/api/java/io/Writer.html
11. http://java.sun.com/j2se/1.3/docs/api/java/io/BufferedOutputStream.html
12. http://java.sun.com/j2se/1.3/docs/api/java/io/BufferedWriter.html.