Red Hat Web Application Framework 6.1 Manuale Utente

Pagina di 230
48
Chapter 7. Developing with WAF
What kind of I/O does log4j use: buffered or unbuffered? It depends on how the particular appender
is configured. In the case of
FileAppender
13
, you have the option of configuring it either way. See
the methods
getBufferedIO()
14
and
setBufferedIO(boolean)
15
.
For maximum performance, you should use buffered I/O. Only switch to unbuffered I/O when you
suspect logged information may be getting lost. By default, the file appender is buffered.
7.5.6. Performance Considerations
For maximum performance, follow these rules:
1. Turn off all unnecessary logging. Set the threshold to
error
,
fatal
, or
off
to minimize the
amount of logging produced.
2. As has been pointed out in Section 7.5.5 Beware of Buffered Streams, make sure you are using
buffered I/O for logging.
3. If logging statements appear in a performance-critical section of code, they should be wrapped
inside an
if
statement that checks the current level of the logger. The "\" character has been
inserted where the lines were artificially broken for printing purposes:
if ( s_log.isDebugEnbled() ) {
Object expensiveMsg = "parameter foo " + foo + " expected by the \
object " +
frob + " was not supplied. Oh, and the value of baz was " + \
baz);
s_log.debug(expensiveMsg);
}
By making the above logging call conditional, we avoid the high cost of constructing the
ex-
pensiveMsg
object, if the logging level is to stricter than
debug
.
13. http://jakarta.apache.org/log4j/docs/api/org/apache/log4j/FileAppender.html
14. http://jakarta.apache.org/log4j/docs/api/org/apache/log4j/FileAppender.html#getBufferedIO()
15. http://jakarta.apache.org/log4j/docs/api/org/apache/log4j/FileAppender.html#setBufferedIO(boolean)