logging services (以下两个问题答案摘抄自该网站)为什么要使用日志 为什么要使用log4j
Inserting log statements into your code is a low-tech method for debugging it. It may also be the only way because debuggers are not always available or applicable. This is often the case for distributed applications.
On the other hand, some people argue that log statements pollute source code and decrease legibility. (We believe that the contrary is true). In the Java language where a preprocessor is not available, log statements increase the size of the code and reduce its speed, even when logging is turned off. Given that a reasonably sized application may contain thousands of log statements, speed is of particular importance.
无责任直译:
在你的代码中插入日志项对于debug代码来说是一种低技术含量的方法,不过它却是唯一的方法,因为程序猿不可能时时刻刻都能够做事高效或者脑袋灵光,这在分布式应用中通常是典型案例。
然而另一方面,一些人争辩说日志项污染了源代码,并且降低了代码可读性。(我们认为这点确实是真的)。在java语言中,预处理器不高效,都是因为日志项增加了代码量,减小了运行速度,甚至当日志关闭的时候,该情况还是会发生。一个比较合理代码量的应用可能含有成千上万条日志项代码,然而速度可是非常重要的。
With log4j it is possible to enable logging at runtime without modifying the application binary. The log4j package is designed so that these statements can remain in shipped code without incurring a heavy performance cost. Logging behavior can be controlled by editing a configuration file, without touching the application binary.
Logging equips the developer with detailed context for application failures. On the other hand, testing provides quality assurance and confidence in the application. Logging and testing should not be confused. They are complementary. When logging is wisely used, it can prove to be an essential tool.
One of the distinctive features of log4j is the notion of inheritance in loggers. Using a logger hierarchy it is possible to control which log statements are output at arbitrarily fine granularity but also great ease. This helps to reduce the volume of logged output and the cost of logging.
The target of the log output can be a file, an OutputStream, a java.io.Writer, a remote log4j server, a remote Unix Syslog daemon, or many other output targets.
无责任直译:
不需要修改应用分支项,log4j就可以加快日志项运行时的速度。设计出log4j包主要是为了让日志项能保存于一个装载代码中,并且不需要巨大的消耗。通过编辑构造文件就能控制日志的表现,这不需要修改应用分支。(未翻译完 觉得没必要 自己看懂了就行了)
Log4j介绍
Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件、甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式,通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。
Log4j组件构成
Log4j由三个重要的组件构成:
1.日志信息的优先级(Logger)
2.日志信息的输出目的地(Appender)
3.日志信息的输出格式(Layout)。
概要:
日志信息的优先级从高到低有ERROR、WARN、 INFO、DEBUG,分别用来指定这条日志信息的重要程度;
日志信息的输出目的地指定了日志将打印到控制台还是文件中;
而输出格式则控制了日志信息的显示内容。
Log4j配置文件的格式
Log4j支持两种配置文件格式:
1. XML格式的文件
2. properties格式的文件
也可以完全不使用配置文件,而是在代码中配置Log4j环境。但是,使用配置文件将使您的应用程序更加灵活。
以上摘抄自http://www.cnblogs.com/dennisit/archive/2013/01/01/2841603.html