You are previewing Pro Apache Log4j, Second Edition.
O'Reilly logo
Pro Apache Log4j, Second Edition

Book Description

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

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Author
  8. About the Technical Reviewer
  9. Acknowledgments
  10. Introduction
  11. Chapter 1 Introduction to Application Logging
    1. What Is Logging?
      1. Logging Is Systematic
      2. Logging Is Controlled
      3. Logging Information Represents the Application State
    2. Advantages of Logging
    3. Disadvantages of Logging
    4. How Logging Works
    5. Evaluating a Logging Package
    6. Popular Java-Based Logging APIs
      1. JDK Logging API
      2. Apache log4j
      3. Commons Logging API
    7. The Road Ahead
  12. Chapter 2 Understanding Apache log4j
    1. Installing log4j
    2. Overview of the log4j Architecture
    3. Configuring log4j
      1. Naming and Placing the Configuration File
      2. What We Can Configure
      3. XML-Style Configuration
      4. Notable Points About Configuration
    4. Log4j Initialization
      1. Initialization Through VM Parameters
    5. Creating a Log4j Configuration File
      1. Running the Program
      2. Why the Message Printed Twice
      3. Configuring log4j Programmatically
      4. Dynamic Loading of Configuration
    6. Configuring log4j in Web Applications
      1. Setting Up Tomcat
      2. An Example Servlet
    7. Level Object
    8. Logger Object
      1. How to Obtain a Logger
      2. Logging Information
      3. Configuration Methods
      4. Conditions of Successful Logging
      5. A Logger Example
    9. LogManager Object
    10. Nested Diagnostic Context (NDC)
    11. Message Diagnostic Context (MDC)
    12. Conclusion
  13. Chapter 3 Destination of Logging—The Appender Objects
    1. Properties of Appender
      1. Adding Appenders to Loggers
      2. Logger-to-Appender Collaboration
    2. Thread-Safety in log4j
    3. WriterAppender
      1. Properties of WriterAppender
      2. Variations of WriterAppender
    4. ConsoleAppender
      1. Properties of ConsoleAppender
      2. A ConsoleAppender Example
    5. FileAppender
      1. Properties of FileAppender
      2. Writing the Message Quietly
      3. Sample Configuration of FileAppender
    6. Rolling in Multiple Files—RollingFileAppender
      1. Properties of RollingFileAppender
      2. Useful Operations in RollingFileAppender
      3. Sample Configuration of RollingFileAppender
    7. Rolling the File Daily—DailyRollingFileAppender
    8. A File-Based Logging Example
    9. Logging Asynchronously—AsyncAppender
      1. Sample Use of AsyncAppender
    10. Sending Logging Information to Remote Destinations
    11. Logging to a Database with JDBCAppender
      1. Configuring JDBCAppender
      2. Creating a Table for Storing Logging Information
      3. Sample Configuration for JDBCAppender
    12. Implementing JMS-Based Logging with JMSAppender
      1. What Is JMS?
      2. JMS and log4j
    13. Working with SocketAppender
      1. Fault Tolerance
      2. Configuring SocketAppender
    14. Logging to Windows NT Event Log with NTEventLogAppender
    15. E-mail the Logging Message—SMTPAppender
    16. Logging Through Telnet—TelnetAppender
      1. TelnetAppender—An Exercise
    17. Distributed Logging Examples
      1. The Scenario
      2. A JMSAppender Example
      3. A SocketAppender Example
      4. Sending Log Messages to a Database
    18. Final Words
    19. Conclusion
  14. Chapter 4 Formatting Logging Information in log4j
    1. The Layout Hierarchy
    2. The Layout Objects in log4j
    3. Keeping It Simple—SimpleLayout
    4. Thread-Time-Category-Context—TTCCLayout
      1. The Mandatory Information
      2. Configuring TTCCLayout Programmatically
      3. Configuring TTCCLayout via Configuration File
    5. The Date—DateLayout
    6. HTMLLayout
    7. XMLLayout
    8. Give It a Pattern—PatternLayout
    9. Conclusion
  15. Chapter 5 Filtering, Error Handling, and Special Rendering of Log Messages
    1. Filtering Explained
    2. Level-Based Filtering
      1. Using LevelRangeFilter
    3. Matching a Particular Level—LevelMatchFilter
    4. Matching a String—StringMatchFilter
    5. A Custom Level-Based Filter—SelectedLevelFilter
    6. Filter Chaining
    7. Error Handling
      1. ErrorCode Interface
      2. Writing a Custom ErrorHandler
      3. Configuring ErrorHandler
      4. Running the Example
    8. ObjectRenderer
      1. A Custom Renderer Example
      2. Configuring ObjectRenderer
      3. Renderer in Action
    9. What Is Formatting and What Is Rendering?
    10. Conclusion
  16. Chapter 6 Extending log4j to Create Custom Logging Components
    1. Why We Need Custom Logging Components
    2. Creating the Custom WindowAppender
      1. Features of the Custom WindowAppender
      2. The Bottlenecks
      3. The Custom WindowAppender Architecture
      4. Implementing the Custom WindowAppender
      5. Testing the Custom WindowAppender
    3. Configuring log4j from a Database
      1. The Database Table Design for Storing Logging Information
      2. Reading Configuration Information from the Database via the Configuration Loader
      3. Writing the Configuration Class
      4. A Database-Based Configuration Example
    4. Custom Logging Framework
      1. Creating a Custom Level Class
      2. Creating a Custom Logger
      3. Generating a Custom Logger Factory Object
      4. Using Custom Logging Components
      5. A Simpler Approach to Using a Custom Level Class
    5. Writing a Custom Layout
    6. Conclusion
  17. Chapter 7 A Complete log4j Example
    1. Conclusion
  18. Chapter 8 Log4j and J2EE
    1. Why Logging Is Difficult in J2EE
    2. WebLogic Class Loaders Explained
    3. When Log4j Does Not Work
      1. Case 1
      2. Case 2
      3. Case 3
      4. Case 4
    4. Making Log4j Work in J2EE
    5. Sometimes It Works on Its Own
    6. Conclusion
  19. Chapter 9 Using the Apache Log Tag Library
    1. What Is a Tag?
    2. Installing the Log Tag Library
    3. A Simple Example of Using the Log Tag Library
      1. Configuration File for the Log Tag Library
      2. Setting the Environment
      3. The Log Example in Action
    4. Using a Custom Logger with the Log Tag Library
    5. Description of Log Tags
    6. Creating Custom Tags with the Log Tag Library to Use a Custom Level
      1. Creating a New Tag
      2. The Custom TraceTag in Action
    7. Conclusion
  20. Chapter 10 Best Practices and Looking Forward to 1.3
    1. Obtaining a Logger
    2. Using Logger Hierarchy
    3. Logging Messages Efficiently
    4. Issues with Localization
    5. Using Location Information
    6. Formatting Logging Information
    7. Using Renderer Objects
    8. Using Asynchronous Logging
    9. Using Filter Objects
    10. Using Nested Diagnostic Context
    11. Configuration Issues
    12. Comparing log4j and the JDK Logging API
      1. Comparing Configuration Options
      2. Formatting Logging Information: Comparing Formatter and Layout Objects
      3. Output Destination: Comparing Handler and Appender Objects
      4. Comparing Filter Objects
      5. Comparing Location Information Options
      6. Comparing Error Handling
    13. Looking Forward to log4j 1.3
      1. Joran Configurator
      2. Plug-ins
      3. More Filters
    14. Conclusion
  21. Appendix A The log4j Configuration Parameters
  22. Appendix B The log4j DTD
  23. Index