Chapter 14. Logging Messages from Servlets and JSPs


Logging involves sending messages from your application and displaying this information in a variety of ways for web developers and administrators. The messages can be delivered to a console, or they can be stored persistently in files or databases. Logging may be used only for sending debug-related information while a web application is being developed, or these messages may provide information from an application in production, including data about warnings and fatal errors.

This chapter describes a very powerful open source logging tool called log4j . This is a Java ARchive (JAR) file (log4j-1.2.8.jar) that you can add to your web application by placing it in your WEB-INF/lib directory. This makes it available for use in any servlets or beans that you want to send logging messages from. This section provides only a brief introduction to log4j, because its power does entail some complexity.

log4j involves three main concepts: loggers, appenders, and layouts. log4j uses an external configuration file, similar to a deployment descriptor, to configure these three logging elements. The upcoming recipes provide some examples of these configuration files, which are mostly simple text files involving a list of name/value pairs. The power of using external files is that you can change the properties in these text files to alter a logger (for instance, modify the format of its messages) instead of recompiling the servlet code. ...

Get Java Servlet & JSP Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.