- 浏览: 1000773 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (1355)
- test (75)
- 红茶和绿茶 (1)
- Jave SE (206)
- Oracle (19)
- English (177)
- Log4j (5)
- RIA(Rich Internet Applications) (9)
- Ext Js (6)
- Android (14)
- Logo (0)
- 文字采撷 (287)
- 使用技巧 (92)
- Project Management (22)
- Hibernate (12)
- Struts (5)
- 规则引擎 (1)
- Html & Javasctipt (56)
- Spring MVC (10)
- Maven (17)
- Java Test (17)
- Linux (16)
- Tools (1)
- CV (0)
- Middleware (2)
- HTML5 (2)
- Algorithms (4)
- Web Service (15)
- 留学 (15)
- LADP (5)
- PXCOA (0)
- SysLog (6)
- SSO (3)
- Spring Security (4)
- Spring Batch (1)
- Jmail (1)
- Bible (4)
- Java Thread (5)
- Architect (6)
- github (2)
- Java Swing (12)
- NoSQL (7)
- UML (2)
- 敏捷(Agile) (7)
- Hudson+Maven+SVN (15)
- cloud computing (2)
- Bahasa Indonesia (1)
- jBPM (6)
- 民俗知识 (3)
- Consulting (1)
- Mysql (5)
- SAP (1)
- 微信公众平台接口开发 (3)
- 做生意 (1)
- 西餐 (1)
- Banking (1)
- Flex (0)
- 黄金投资 (1)
- Apache Tomcat 集群 (3)
- Hadoop (7)
- 需求分析 (1)
- 银行知识 (3)
- 产品管理 (2)
- 钢琴Music (3)
- 设计 (3)
- Marketing (2)
- US Life (3)
- 算法 (14)
- BigData (4)
- test红茶和绿茶Jave SEOracleEnglishLog4jRIA(Rich Internet Applications)Ext JsAndroidLogo文字采撷 (0)
- Design Pattern (5)
- NodeJS&AngularJS (9)
- Python (1)
- Spring boot (0)
- ACM (3)
最新评论
-
心往圣城:
微时代-最专业的微信第三方平台。LBS定位导航,微网站,自定义 ...
微信公众平台 /微信公众平台怎么用 -
zhaojiafan:
return ReverseStr1(str.substrin ...
逆转字符串 Write a String Reverser (and use Recursion!) -
zhaojiafan:
public class StringUtils {
p ...
逆转字符串 Write a String Reverser (and use Recursion!)
Are your Java programs littered with a multitude of randomly placed System.out.println statements and stack traces? When you add debugging messages to a class in a project, are the outputs of your messages interleaved among dozens of messages from other developers, making your messages difficult to read? Do you use a simple, hand-rolled logging API, and fear that it may not provide the flexibility and power that you need once your applications are in production? If you answered yes to any of the above questions, it's time for you to pick an industrial-strength logging API and start using it.
This article will help you choose a logging API by evaluating two of the most widely used Java logging libraries: the Apache Group's Log4j and the java.util.logging package (referred to as "JUL"). This article examines how each library approaches logging, evaluates their differences and similarities, and offers a few simple guidelines that will help you decide which library to choose.
Introduction to Log4j
Log4j is an open source logging library developed as a subproject of the Apache Software Foundation's Logging Services Project. Based on a logging library developed at IBM in the late 1990s, its first versions appeared in 1999. Log4j is widely used in the open source community, including by some big name projects such as JBoss and Hibernate.
Log4j's architecture is built around three main concepts: loggers, appenders, and layouts. These concepts allow developers to log messages according to their type and priority, and to control where messages end up and how they look when they get there. Loggers are objects that your applications first call on to initiate the logging of a message. When given a message to log, loggers generate Logging-Event objects to wrap the given message. The loggers then hand off the LoggingEvents to their associated appenders. Appenders send the information contained by the LoggingEvents to specified output destinations - for example, a ConsoleAppender will write the information to System.out, or a FileApppender will append it to a log file. Before sending LoggingEvent information to its final output target, some appenders use layouts to create a text representation of the information in a desired format. For example, Log4j includes an XMLLayout class that can be used to format LoggingEvents as strings of XML.
In Log4j, LoggingEvents are assigned a level that indicates their priority. The default levels in Log4j are (ordered from highest to lowest): OFF, FATAL, ERROR, WARN, INFO, DEBUG, and ALL. Loggers and appenders are also assigned a level, and will only execute logging requests that have a level that is equal to or greater than their own. For example, if an appender whose level is ERROR is asked to write out a LoggingEvent that has a level of WARN, the appender will not write out the given LogEvent.
All loggers in Log4j have a name. Log4j organizes logger instances in a tree structure according to their names the same way packages are organized in the Java language. As Log4j's documentation succinctly states: "A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger." For example, a logger named "org.nrdc" is said to be the child of the "org" logger. The "org.nrdc.logging" logger is the child of the "org.nrdc" logger and the grandchild of the "org" logger. If a logger is not explicitly assigned a level, it uses the level of its closest ancestor that has been assigned a level. Loggers inherit appenders from their ancestors, although they can also be configured to use only appenders that are directly assigned to them.
When a logger is asked to log a message, it first checks that the level of the request is greater than or equal to its effective level. If so, it creates a LoggingEvent from the given message and passes the LoggingEvent to its appenders, which format it and send it to its output destinations.
Introduction to JUL
The java.util.logging package, which Sun introduced in 2002 with Java SDK version 1.4, came about as a result of JSR 47, Logging API Specification. JUL is extremely similar to Log4j - it more or less uses exactly the same concepts, but renames some of them. For example, appenders are "handlers," layouts are "formatters," and LoggingEvents are "LogRecords." Figure 1 summarizes Log4j and JUL names and concepts. JUL uses levels the same way that Log4J uses levels, although JUL has nine default levels instead of seven. JUL organizes loggers in a hierarchy the same way Log4j organizes its loggers, and JUL loggers inherit properties from their parent loggers in more or less the same way that Log4j loggers inherit properties from their parents. Concepts pretty much map one-to-one from Log4j to JUL; though the two libraries are different in subtle ways, any developer familiar with Log4j needs only to adjust his or her vocabulary to generally understand JUL.
Functionality Differences
While Log4j and JUL are almost conceptually identical, they do differ in terms of functionality. Their difference can be summarized as, "Whatever JUL can do, Log4j can also do - and more." They differ most in the areas of useful appender/handler implementations, useful formatter/layout implementations, and configuration flexibility.
JUL contains four concrete handler implementations, while Log4j includes over a dozen appender implementations. JUL's handlers are adequate for basic logging - they allow you to write to a buffer, to a console, to a socket, and to a file. Log4j's appenders, on the other hand, probably cover every logging output destination that you could think of. They can write to an NT event log or a Unix syslog, or even send e-mail. Figure 2 provides a summary of JUL's handlers and Log4j's appenders.
JUL contains two formatter classes: the XMLFormatter and SimpleFormatter. Log4j includes the corresponding layouts: the XMLLayout and SimpleLayout. Log4j also offers the TTCCLayout, which formats LoggingEvents into content-rich strings, and the HTMLLayout, which formats LoggingEvents as an HTML table.
While the TTCCLayout and HTMLLayout are useful, Log4j really pulls ahead of JUL in the formatter/handler arena because of the PatternLayout. PatternLayout instances can be configured with an enormous amount of flexibility via string conversion patterns, similar to the conversion patterns used by the printf function in C. In PatternLayout conversion patterns, special conversion characters are used to specify the information included in layout's formatted output. For example, "%t" is used to specify the name of the thread that started the logging of the message; "%C" is used to specify the name of the class of the object that started the logging of the message; and "%m" specifies the message. "%t: %m" would result in output such as "main thread: This is my message." "%C - %t: %m" would result in output such as "org.nrdc.My-Class - main thread: This is my message." The Pattern-Layout is extremely useful, and JUL's two formatter classes don't come anywhere near to matching its versatility. It's not uncommon for JUL users to write their own custom formatter class, whereas most Log4j users generally need to just learn how to use PatternLayout conversion patterns.
While both Log4j and JUL can be configured with configuration files, Log4j allows for a broader range of configuration possibilities through configuration files than JUL does. JUL can be configured with .properties files, but until J2SE 5.0 the configuration of handlers was only on a per-class rather than a per-instance basis. This means that if you are going to be using a pre-Tiger SDK, you'll miss out on useful configuration options, such as the ability to set up different FileHandler instances to send their output to different files.
It's important to note that pre-Tiger JUL can easily be configured to write to multiple output files in code, just not through its default configuration mechanism. Log4j can be configured with .properties and/or XML files, and appenders can be configured on a per-instance basis. Also, Log4j allows developers to associate layout instances with appender instances, and configure layouts on a per-instance basis. This includes PatternLayout instances - you can set the conversion pattern each uses in the configuration file. During development, it usually isn't a problem to recompile an application to adjust its logging configuration; after deployment, however, you may want to be able to tweak or even completely reconfigure your application's logging without recompiling. In that case, Log4j offers more flexibility, especially pre-Tiger.
http://java.sys-con.com/node/48541
发表评论
-
Testing Syslog is working
2012-07-26 14:59 844vi /etc/sysconfig/syslog sys ... -
Logging to the syslog from a java application
2012-07-24 10:21 971Every application needs logging ... -
Log4j输出日志到syslog
2012-07-03 16:56 701http://www.micmiu.com/opensourc ... -
Logging and Syslog Best Practices
2012-05-22 15:03 910In this post, I will cover a ba ... -
Establishing a Hardened Syslog Log Server
2012-05-22 14:59 1097Maintaining a reliable and secu ...
相关推荐
详细介绍了java.util.logging.Logger的用法和结构,对如果扩展Logger起到抛砖引玉的作用!尊重劳动成果,亲下载了要给个评价!
Tomcat内存溢出的解决方法(java.util.concurrent.ExecutionException:java.lang.OutOfMemoryError),内附解决方案!
1. java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...
log4j-1.2.16.jar,三个文件一起组成一个完整的日志输出,Apache的Common Logging只是一个高层的日志框架,本身并没有实现真正的写日志能力,而是依赖其它的日志系统如Log4j或者java本身的java.util.logging。...
主要给大家介绍了关于tomcat启动报:java.util.zip.ZipException错误的解决方法,文中通过示例代码介绍的非常详细,同样遇到这个问题的朋友可以参考借鉴,下面随着小编来一起学习学习吧
java发布wsdl部署到was 8.5报错的时候引入的jar包,比如如下情况: Caused by: java.lang.NoClassDefFoundError: com.sun.org.apache.xml.internal.resolver.CatalogManager at ...
JavaMail的java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream错误 原因: MyEclipse6.5的javaee.jar中的mail包与JavaMail包有冲突。 解决: 在MyEclipse目录下(D:\Program Files\MyEclipse ...
解决方案:Exceptionin thread "main" java.lang.UnsatisfiedLinkError:org.apache.hadoop.util.NativeCrc32.nativeCo
java.util.ConcurrentModificationException 异常问题详解1
本文通过对数据压缩算法的简要介绍,然后以详细的示例演示了利用java.util.zip包实现数据的压缩与解压,并扩展到在网络传输方面如何应用java.util.zip包现数据压缩与解压
java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:953) java.util.LinkedList$ListItr.next(LinkedList.java:886) JMeter.plugins.functional.samplers.websocket.ServiceSocket....
java.util.ConcurrentModificationException 解决方法 在使用iterator.hasNext()操作迭代器的时候,如果此时迭代的对象发生改变,比如插入了新数据,或者有数据被删除。 则使用会报以下异常: Java.util....
Exception in thread “main“ java.util.InputMismatchException
很详细的apache-tomcat-8.0.523月 17, 2019 10:51:41 上午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' ...
<Call Stack = DEBUG_FRAME = org.apache.axis2.util.JavaUtils.callStackToString(JavaUtils.java:564) DEBUG_FRAME = org.apache.axis2.description.ParameterIncludeImpl.debugParameterAdd(ParameterIncludeImpl...
1、文件解压zip通用机制方法、一行代码支持中文; 2、一行代码解决 java.util.zip.ZipInputStream 中文乱码; 3、删除指定路径内的所有文件通用机制方法;
主要介绍了java.util.concurrent.ExecutionException 问题解决方法的相关资料,需要的朋友可以参考下
axis2解决 org.apache.axis2.util.JavaUtils.callStackToString问题
log4j.appender.ROLLING_FILE.File=rolling.log //文件位置,也可以用变量${java.home}、rolling.log log4j.appender.ROLLING_FILE.Append=true //true:添加 false:覆盖 log4j.appender.ROLLING_FILE.MaxFileSize=10...