In development scenarios where things can't be run in a debugger, or when you run the risk of masking the problem, logs are the greatest source of information about running a program. Pro Apache Log4j, Second Edition provides best practices guidelines and comprehensive coverage of the most recent release. Step by step, the book explains core concepts, from basic to advanced. Code samples are in Java and include guidelines for different application-specific needs. You’ll also learn how to extend the API to write custom components and best practices for using the feature-rich log4j API. This book concludes with enterprise Java applications using log4j with JSP and J2EE. Table of Contents Introduction to Application Logging Understanding Apache log4j Destination of Logging—The Appender Objects Formatting Logging Information in log4j Filtering, Error Handling, and Special Rendering of Log Messages Extending log4j to Create Custom Logging Components Complete log4j Example Log4j and J2EE Using the Apache Log Tag Library Best Practices and Looking Forward to 1.3 Contents......Page 6 About the Author......Page 12 About the Technical Reviewer......Page 13 Acknowledgments......Page 14 Introduction......Page 15 What Is Logging?......Page 17 Logging Information Represents the Application State......Page 18 Disadvantages of Logging......Page 19 How Logging Works......Page 20 Evaluating a Logging Package......Page 21 Commons Logging API......Page 22 The Road Ahead......Page 23 Installing log4j......Page 24 Overview of the log4j Architecture......Page 25 Naming and Placing the Configuration File......Page 27 What We Can Configure......Page 28 XML-Style Configuration......Page 30 Initialization Through VM Parameters......Page 31 Creating a Log4j Configuration File......Page 32 Why the Message Printed Twice......Page 33 Dynamic Loading of Configuration......Page 34 Setting Up Tomcat......Page 35 An Example Servlet......Page 37 Level Object......Page 38 Logger Object......Page 39 How to Obtain a Logger......Page 40 Logging Information......Page 41 Conditions of Successful Logging......Page 43 A Logger Example......Page 44 LogManager Object......Page 46 Message Diagnostic Context (MDC)......Page 47 Conclusion......Page 49 CHAPTER 3 Destination of Logging—The Appender Objects......Page 50 Properties of Appender......Page 51 Adding Appenders to Loggers......Page 52 WriterAppender......Page 53 Properties of WriterAppender......Page 54 A ConsoleAppender Example......Page 55 Properties of FileAppender......Page 56 Sample Configuration of FileAppender......Page 57 Properties of RollingFileAppender......Page 58 Useful Operations in RollingFileAppender......Page 59 Sample Configuration of RollingFileAppender......Page 60 Rolling the File Daily—DailyRollingFileAppender......Page 61 A File-Based Logging Example......Page 63 Logging Asynchronously—AsyncAppender......Page 66 Sample Use of AsyncAppender......Page 67 Logging to a Database with JDBCAppender......Page 68 Configuring JDBCAppender......Page 69 Sample Configuration for JDBCAppender......Page 70 What Is JMS?......Page 72 JMS and log4j......Page 73 Working with SocketAppender......Page 75 Configuring SocketAppender......Page 76 Logging to Windows NT Event Log with NTEventLogAppender......Page 77 E-mail the Logging Message—SMTPAppender......Page 78 The Scenario......Page 80 A JMSAppender Example......Page 82 A SocketAppender Example......Page 86 Final Words......Page 89 Conclusion......Page 90 The Layout Hierarchy......Page 91 The Layout Objects in log4j......Page 92 Keeping It Simple—SimpleLayout......Page 93 The Mandatory Information......Page 94 Configuring TTCCLayout Programmatically......Page 95 Configuring TTCCLayout via Configuration File......Page 97 HTMLLayout......Page 99 XMLLayout......Page 104 Give It a Pattern—PatternLayout......Page 106 Conclusion......Page 108 Filtering Explained......Page 109 Level-Based Filtering......Page 110 Using LevelRangeFilter......Page 111 A Custom Level-Based Filter—SelectedLevelFilter......Page 113 Filter Chaining......Page 116 Error Handling......Page 117 Writing a Custom ErrorHandler......Page 120 Configuring ErrorHandler......Page 122 A Custom Renderer Example......Page 123 Configuring ObjectRenderer......Page 126 Renderer in Action......Page 127 Conclusion......Page 128 Creating the Custom WindowAppender......Page 129 The Custom WindowAppender Architecture......Page 130 Implementing the Custom WindowAppender......Page 131 Testing the Custom WindowAppender......Page 134 Configuring log4j from a Database......Page 136 Reading Configuration Information from the Database via the Configuration Loader......Page 137 Writing the Configuration Class......Page 144 A Database-Based Configuration Example......Page 147 Creating a Custom Level Class......Page 149 Generating a Custom Logger Factory Object......Page 151 Using Custom Logging Components......Page 152 A Simpler Approach to Using a Custom Level Class......Page 154 Writing a Custom Layout......Page 158 Conclusion......Page 161 CHAPTER 7 A Complete log4j Example......Page 162 Conclusion......Page 168 Why Logging Is Difficult in J2EE......Page 169 WebLogic Class Loaders Explained......Page 170 Case 2......Page 171 Making Log4j Work in J2EE......Page 172 Conclusion......Page 173 What Is a Tag?......Page 174 A Simple Example of Using the Log Tag Library......Page 175 Configuration File for the Log Tag Library......Page 176 Setting the Environment......Page 177 Using a Custom Logger with the Log Tag Library......Page 178 Description of Log Tags......Page 179 Creating a New Tag......Page 181 The Custom TraceTag in Action......Page 183 Conclusion......Page 184 Obtaining a Logger......Page 185 Using Logger Hierarchy......Page 186 Logging Messages Efficiently......Page 187 Formatting Logging Information......Page 189 Using Filter Objects......Page 190 Configuration Issues......Page 191 Comparing log4j and the JDK Logging API......Page 192 Formatting Logging Information: Comparing Formatter and Layout Objects......Page 193 Comparing Error Handling......Page 194 Plug-ins......Page 195 Conclusion......Page 196 APPENDIX A. The log4j Configuration Parameters......Page 197 APPENDIX B. The log4j DTD......Page 200 A......Page 204 D......Page 205 H......Page 206 L......Page 207 M......Page 208 S......Page 209 W......Page 210 X......Page 211 Any Java language based application developer will benefit from the topics covered in this book. Also non-Java developers can benefit by reading this book to understand how to implement and architect a robust logging framework in any language. Java Logging is designed to let a Java program, servlet, applet, or EJB component produce messages of interest to end users, system administrators, field engineers, and software developers. It comes in handy especially in development scenarios where things can't be run in a debugger, or if doing so masks the problem that is occurring (because it is timing related, for example). In these situations, logs are frequently the greatest (and sometimes the only) source of information about a running program. This book covers in detail the most popular Java-based Logging API named Apache Log4j. This book is based on the most recent release version 1.2.9. The book includes a step-by-step approach in explaining the concepts from the very basics to the advanced concepts. Apart from explaining in details the architecture of the API and it use; this book also shows how to extend the API to write custom components. It also describes the best practices involved in using this feature rich log4j API. This book finally also provides a comparison with the other popular JDK Logging API bundled with JDK itself. * Illustrates each concept with code samples in Java language; provides guidelines for different application-specific needs. * Describes the techniques to distribute the logging activity—critical to implement in an enterprise-wide logging framework. * The only Java Logging book on the market, and includes Best Practices guidelines.