没有合适的资源?快使用搜索试试~ 我知道了~
Logging in Java with the JDK 1.4 Logging API and Apache log4j
需积分: 10 50 下载量 59 浏览量
2008-02-04
17:48:22
上传
评论
收藏 1.4MB PDF 举报
温馨提示
试读
57页
Logging in Java with the JDK 1.4 Logging API and Apache log4j (APress book 2003)
资源推荐
资源详情
资源评论
Logging in Java with
the JDK 1.4 Logging API
and Apache log4j
SAMUDRA GUPTA
0996_Gupta front.fm Page i Tuesday, March 4, 2003 5:06 PM
Logging in Java with the JDK 1.4 Logging API and Apache log4j
Copyright ©2003 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.
Printed and bound in the United States of America 12345678910
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.
Library of Congress Cataloging-in-Publication Data
Gupta, Samudra, 1974-
Logging in Java with the JDK 1.4 logging API and Apache log4j / Samudra Gupta.
p. cm.
ISBN 1-59059-099-6 (hardcover : alk. paper)
1. Java (Computer program language) 2. Apache (Computer file : Apache
Group) 3. Application logging (Computer science) 4. Application software--Development--
Management. 5. Debugging in computer science. I. Title.
QA76.73.J38G86 2003
005.13'3--dc21
2003005480
Technical Reviewer: Jeff Heaton
Editorial Directors: Dan Appleman, Gary Cornell, Simon Hayes, Martin Streicher,
Karen Watterson, John Zukowski
Assistant Publisher: Grace Wong
Developmental Editors: Tracy Brown Collins, Ami Knox
Copy Editor: Ami Knox
Production Manager: Kari Brooks
Compositor: Susan Glinert Stevens
Proofreader: Lori Bring
Indexer: Ann Rogers
Artist and Cover Designer: Kurt Krames
Manufacturing Manager: Tom Debolski
Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 175 Fifth Avenue,
New York, NY, 10010 and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr.
17, 69112 Heidelberg, Germany.
In the United States: phone 1-800-SPRINGER, email orders@springer-ny.com, or visit
http://www.springer-ny.com. Outside the United States: fax +49 6221 345229, email
orders@springer.de, 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, email info@apress.com, or visit
http://www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have
any liability 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.
0996_Gupta front.fm Page ii Monday, March 17, 2003 6:59 PM
121
CHAPTER 5
Understanding
Apache log4j
THE APACHE LOG4J implementation is a highly scalable, robust, and versatile logging
framework. This API simplifies the writing of logging code within an application,
yet allows the flexibility of controlling logging activity from an external configu-
ration file. It also allows us to publish logging information to desired granularity
depending on the detail of the logging information suitable to each application.
We can tailor the granularity of logging activity suitable for the development
phase or the deployment phase of an application without having to change the
source code. It is possible to switch over to a different logging behavior by only
changing a few configuration parameters.
Apache log4j is also capable of publishing logging information to various des-
tinations such as files, consoles, and NT event logs. Moreover, logging information
can even be distributed over Java Message Service ( JMS) or JDBC, or can be output
to a TCP/IP socket. This API lets us take logging information and publish or print it
in different formats and layouts that are human readable and reusable by any error
handling and analyzer program at a future date. The scalability aspect of log4j
allows developers to enhance the capability of logging by creating new logging
destinations and unique formats and layouts, which is done by extending the
existing framework.
Although the application of the log4j framework is easy, it demands certain
methods and practices be adopted for the best possible results. In this chapter, we
will discuss the overall architecture of the log4j framework and different objects
involved within the API, and examine in detail the application and usage of the
core logging objects. This chapter uses the Apache log4j version 1.2.6 release as the
basis for the concepts and examples presented herein. Although the subsequent
releases of log4j will incorporate more features, the basic concepts discussed in
this book will remain unchanged and will prove useful to mastering any subse-
quent release.
Installing log4j
Apache log4j is an open-source project from Apache group. You need to meet the
following criteria to successfully install and use log4j:
0996_Gupta.book Page 121 Tuesday, March 4, 2003 3:49 PM
Chapter 5
122
• Get the latest version of the log4j binary distribution from
http://jakarta.apache.org. The examples and concepts in this book are
based on the log4j version 1.2.6 release. You may obtain any later version of
log4j, if available, and still be able to follow the examples in this book.
• Apache log4j is JDK 1.1.x compatible. Make sure you have the appropriate
JDK version downloaded to your machine.
• You need a JAXP-compatible XML parser to use log4j. Make sure you have
“Xerces.jar” installed on your machine.
• The e-mail–based logging feature in log4j requires the Java Mail API
(“mail.jar”) to be installed in your machine. Apache log4j is tested
against version 1.2 of the Java Mail API.
• The Java Mail API will also require the JavaBeans Activation Framework
(“activation.jar”) be installed in your machine.
• The JMS-compatible features of log4j will require that JMS is installed on
your machine as well as JNDI.
Once you have acquired and installed all the required .jar files, you must make
sure that all these resources are available within the classpath of the Java runtime.
Overview of the log4j Architecture
The architecture of the log4j API is layered. Each layer consists of different objects
performing different tasks. The top layer captures the logging information, the
middle layer is involved in analyzing and authorizing the logging information, and
the bottom layer is responsible for formatting and publishing the logging infor-
mation to a destination. In essence, log4j consists of three types of primary objects:
•
Logger: The Logger object (formerly known as the Category object in releases
prior to log4j 1.2) is responsible for capturing logging information.
Logger
objects are stored in a namespace hierarchy.
•
Appender: The Appender object is responsible for publishing logging infor-
mation to various preferred destinations. Each
Appender object will have at
least one target destination attached to it. For example, a ConsoleAppender
object is capable of printing logging information to a console.
0996_Gupta.book Page 122 Tuesday, March 4, 2003 3:49 PM
Understanding Apache log4j
123
• Layout: The Layout object is used to format logging information in different
styles.
Appender objects utilize Layout objects before publishing logging
information.
Layout objects play an important role in publishing logging
information in a way that is human readable and reusable.
The preceding core objects are central to the architecture of log4j. Apart from
them, there are several auxiliary objects that are plug and play to any layer of the
API. These objects help manage the different
Logger objects active within an appli-
cation and fine-tune the logging process.
Next, let’s go over the principal auxiliary objects in the log4j framework that
play a vital role in the logging framework:
•
Level: The Level object, previously referred to as the Priority object, defines
the granularity and priority of any logging information. Each piece of logging
information is accompanied with its appropriate
Level object, which tells
the
Logger object about the priority of the information. There are seven
levels of logging defined within the API: OFF, DEBUG, INFO, ERROR, WARN,
FATAL, and ALL. Each level defined has a unique integer value associated
with it.
•
Filter: The Filter object is used to analyze logging information and make
further decisions on whether that information should be logged or not. In
the log4j context,
Appender objects can have several Filter objects associated
with them. If logging information is passed to a particular
Appender object,
all the
Filter objects associated with that Appender need to approve the
logging information before it can be published to the preferred destination
attached to the
Appender. Filter objects are very helpful in filtering out
unwanted logging information based on any application-specific criteria.
•
ObjectRenderer: The ObjectRenderer object is specialized in providing a
String representation of different objects passed to the logging framework.
More precisely, when the application passes a custom
Object to the
logging framework, the logging framework will use the corresponding
ObjectRenderer to obtain a String representation of the passed Object.
This is used by Layout objects to prepare the final logging information.
•
LogManager: The LogManager object performs the management of the logging
framework. It is responsible for reading the initial configuration parameters
from a system-wide configuration file or a configuration class. Each
Logger
instance created with a namespace within an application is stored in a
namespace hierarchy by the
LogManager. When we try to obtain the reference
named logger, the
LogManager class returns the already created instance of it,
or else creates a new instance of the named logger, stores it in a repository
for future reference, and returns the new instance to the caller application.
0996_Gupta.book Page 123 Tuesday, March 4, 2003 3:49 PM
剩余56页未读,继续阅读
资源评论
shaun67jp
- 粉丝: 0
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功