Pro Apache Log4j, Second Edition Copyright © 2005 by Samudra Gupta All Rights Reserved

Pro Apache Log4j, Second Edition Copyright © 2005 by Samudra Gupta All Rights Reserved

Pro Apache Log4j Second Edition SAMUDRA GUPTA Pro Apache Log4j, Second Edition Copyright © 2005 by Samudra Gupta All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN: 1-59059-499-1 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Steve Anglin Technical Reviewer: Jeff Heaton Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore, Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser Assistant Publisher: Grace Wong Project Manager: Kylie Johnston Copy Manager: Nicole LeClerc Copy Editor: Candace English Production Manager: Kari Brooks-Copony Production Editor: Linda Marousek Compositor: Kinetic Publishing Services, LLC Proofreader: Linda Marousek Indexer: Broccoli Information Management Artist: Kinetic Publishing Services, LLC Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013, and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany. In the United States: phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. Outside the United States: fax +49 6221 345229, e-mail [email protected], or visit http://www.springer.de. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precau- tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liabil- ity to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Downloads section. To the lotus feet of Bhagavan Sri Ramakrishna Contents at a Glance About the Author . xiii About the Technical Reviewer. xv Acknowledgments . xvii Introduction. xix CHAPTER 1 Introduction to Application Logging. 1 CHAPTER 2 Understanding Apache log4j . 9 CHAPTER 3 Destination of Logging—The Appender Objects. 35 CHAPTER 4 Formatting Logging Information in log4j . 77 CHAPTER 5 Filtering, Error Handling, and Special Rendering of Log Messages . 95 CHAPTER 6 Extending log4j to Create Custom Logging Components. 115 CHAPTER 7 A Complete log4j Example . 149 CHAPTER 8 Log4j and J2EE . 157 CHAPTER 9 Using the Apache Log Tag Library . 163 CHAPTER 10 Best Practices and Looking Forward to 1.3 . 175 APPENDIX A The log4j Configuration Parameters . 187 APPENDIX B The log4j DTD . 191 INDEX. 195 v Contents About the Author . xiii About the Technical Reviewer. xv Acknowledgments . xvii Introduction. xix ■CHAPTER 1 Introduction to Application Logging . 1 What Is Logging?. 1 Logging Is Systematic. 2 Logging Is Controlled . 2 Logging Information Represents the Application State . 2 Advantages of Logging. 3 Disadvantages of Logging . 3 How Logging Works . 4 Evaluating a Logging Package . 5 Popular Java-Based Logging APIs . 6 JDK Logging API. 6 Apache log4j. 6 Commons Logging API . 6 The Road Ahead. 7 ■CHAPTER 2 Understanding Apache log4j . 9 Installing log4j . 9 Overview of the log4j Architecture . 10 Configuring log4j . 12 Naming and Placing the Configuration File . 12 What We Can Configure . 13 XML-Style Configuration. 15 Notable Points About Configuration . 16 Log4j Initialization . 16 Initialization Through VM Parameters. 16 Creating a Log4j Configuration File . 17 Running the Program . 18 Why the Message Printed Twice . 18 vii viii ■CONTENTS Configuring log4j Programmatically . 19 Dynamic Loading of Configuration . 19 Configuring log4j in Web Applications . 20 Setting Up Tomcat . 20 An Example Servlet . 22 Level Object . 23 Logger Object. 24 How to Obtain a Logger . 25 Logging Information . 26 Configuration Methods . 28 Conditions of Successful Logging. 28 A Logger Example . 29 LogManager Object. 31 Nested Diagnostic Context (NDC) . 32 Message Diagnostic Context (MDC). 32 Conclusion . 34 ■CHAPTER 3 Destination of Logging—The Appender Objects . 35 Properties of Appender. 36 Adding Appenders to Loggers . 37 Logger-to-Appender Collaboration . 38 Thread-Safety in log4j . 38 WriterAppender . 38 Properties of WriterAppender. 39 Variations of WriterAppender . 40 ConsoleAppender . ..

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    211 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us