工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 2515|回复: 9

[原创]学习log4j

[复制链接]
发表于 2006-1-7 21:27 | 显示全部楼层 |阅读模式
首先介绍一下log4j

log4j是apache的一个开源项目,我们可以通过它灵活地把日志按一定的格式输出到控制台,文件,GUI组件等.
通常我们会用到System.out.print()方式来输出信息到控制台.带是程序一大,众多的System.out.print()会增大程序的维护成本.
log4j提供的是分级的方法在程序中嵌入日志记录语句.可以用配置的方法来灵活配置.

以下部分是从log4j指南的一些语句摘录下来的,翻译得不好.别见笑.

http://logging.apache.org/log4j/docs/manual.html

Log4j has three main components: loggers, appenders and layouts. These three types of components work together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.
log4j由三个主要组件组构:loggers,appenders,layouts.三种组件共同协作使开发者能够根据消息的类型与级别记录日志并在运行期控制消息的格式与输出目的地.
Logger names are case-sensitive and they follow the hierarchical naming rule:
Named Hierarchy
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.
日志记录器(logger)的命名是区分大小写的,它们遵循以下的命名规则:
指定的层次
一个祖先日志记录器的名字后面跟着一个点,且它是它的后代日志记录器的前缀.如果没有其它祖先日志记录器位于祖先日志记录器与后代日志记录器之间,则这两个记录器具有父子关系.

Invoking the class static Logger.getRootLogger method retrieves it. All other loggers are instantiated and retrieved with the class static Logger.getLogger method. This method takes the name of the desired logger as a parameter.
通过调用类静态方法Logger.getRootLogger可以重新得到日志记录器.所有其它的日志记录器都是这个记录器的实例并可通过类静态方法Logger.getRootLogger重新获得.这个方法要求一个期望得到的日志名作为参数.
Loggers may be assigned levels. The set of possible levels, that is DEBUG, INFO, WARN, ERROR and FATAL are defined in the org.apache.log4j.Level class.
日志记录器是分等级的.一般的级别是:DEBUG,INFO,WARN,ERROR,FATAL.它是是在org.apache.log4j.Level这个类里定义的.
If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level.
如果一个指定的日志记录器没有指定级别,那么就会从最近的祖先那里继承一个已指定的级别.
Level Inheritance
The inherited level for a given logger C, is equal to the first non-null level in the logger hierarchy, starting at C and proceeding upwards in the hierarchy towards the root logger.
级别的遗传
一个日志记录器C通过继承得到的级别就是与从C开始向上直到root日志记录器的第一个非空级别的层级.

By definition, the printing method determines the level of a logging request.
A logging request is said to be enabled if its level is higher than or equal to the level of its logger. Otherwise, the request is said to be disabled.
确切地说,打印的方法决定了日志记录请求的级别.当日志记录请求的级别高于或等于它的日志记录器的级别时就会打开日志记录.否则,日志记录请求就会关闭.

Basic Selection Rule
A log request of level p in a logger with (either assigned or inherited, whichever is appropriate) level q, is enabled if p >= q.
基本选择规则
在一个级别是q(不管是指定还是继承得到)的日志记录器里一个日志记录请求的级别是p,如果p>=q则会打开日志记录.

This rule is at the heart of log4j. It assumes that levels are ordered. For the standard levels, we have DEBUG < INFO < WARN < ERROR < FATAL.
这条规则是log4j的核心.它设定了级别是有序的.对于标准的级别,顺序是:DEBUG < INFO < WARN < ERROR < FATAL.
Calling the getLogger method with the same name will always return a reference to the exact same logger object.
使用相同的名字来调用getLogger方法总能返回指向相同的的日志记录器的引用.

More than one appender can be attached to a logger.
一个日志记录器可以附加多个appender.

More often than not, users wish to customize not only the output destination but also the output format. This is accomplished by associating a layout with an appender.
通常,用户不但希望可以自定义日志输出目的地,还希望可以自定义日志的输出格式.这个工作由与appender联合的layout来完成的.

Configuration
Currently, configuration files can be written in XML or in Java properties (key=value) format.
配置
目前,支持两种配置文件,它们是xml和java属性文件(关键字=值).


[ Last edited by hjack on 2006-1-7 at 21:32 ]
 楼主| 发表于 2006-1-7 21:28 | 显示全部楼层
下面开始进行log4j之旅
首先到http://logging.apache.org/log4j/docs/download.html下载log4j的包.目前的稳定版本是1.2.13.另一个1.3的是alpha版的.
解压缩得到logging-log4j-1.2.13文件夹.在该目录下可以看到log4j的包.\dist\lib\log4j-1.2.13.jar,等下要用到它.

新建一个项目名为test
新建一个包为log4j
右键test项目,属性,java构建路径.把log4j-1.2.13.jar这个包添加进去.如图一所示.


新建TestLog4j类.

  1. package log4j;

  2. import org.apache.log4j.Logger;

  3. public class TestLog4j {

  4.         /**
  5.          * @param args
  6.          */
  7.         public static void main(String[] args) {
  8.                 // TODO 自动生成方法存根
  9.                 Logger logger = Logger.getLogger(TestLog4j.class.getName());
  10.                 logger.info("this is an info");
  11.                 logger.debug("this is a debug");
  12.         }

  13. }
复制代码

新建文件log4j.properties位于src目录下.内容如下:

  1. ### direct log messages to stdout ###
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  3. log4j.appender.stdout.Target=System.out
  4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  5. log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

  6. ### set root logger ###
  7. log4j.rootLogger=info, stdout
复制代码

以上配置文件是设置了控制台为日志输出目的地.级别是info.

运行TestLog4j类.结果如下:
20:52:53,828  INFO TestLog4j:13 - this is an info

我们在TestLog4j类里还有一句logger.debug("this is a debug");
但控制台并没有输出这个信息,那是log4j的级别起作用了.我们说过,log4j的级别是有序的,请求级别大于或等于指定级别时会打开日志记录的.我们指定了info级别,logger.info("this is an info");这句与指定级别相同,所以输了出来.但logger.debug("this is a debug");这句里debug的级别是小于info的.所以没有输出来.

[ Last edited by hjack on 2006-1-7 at 21:46 ]
回复

使用道具 举报

 楼主| 发表于 2006-1-7 21:28 | 显示全部楼层
我们也可以把日志信息输出到文件中去.
修改log4j.properties文件如下:

  1. ### direct messages to file test.log ###
  2. log4j.appender.file=org.apache.log4j.FileAppender
  3. log4j.appender.file.File=test.log
  4. log4j.appender.file.layout=org.apache.log4j.PatternLayout
  5. log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

  6. ### set root logger ###
  7. log4j.rootLogger=info, file
复制代码


运行后,可以看到生成了test.log日志文件了.见图二.



[ Last edited by hjack on 2006-1-7 at 21:50 ]
回复

使用道具 举报

 楼主| 发表于 2006-1-7 21:28 | 显示全部楼层
Log4J采用类似C语言中的printf函数的打印格式格式化日志信息,打印参数如下:

%m 输出代码中指定的消息
%p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL
%r 输出自应用启动到输出该log信息耗费的毫秒数
%c 输出所属的类目,通常就是所在类的全名
%t 输出产生该日志事件的线程名
%n 输出一个回车换行符,Windows平台为“\r\n”,Unix平台为“\n”
%d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss,SSS},
%l 输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。

[ Last edited by hjack on 2006-1-7 at 22:02 ]
回复

使用道具 举报

发表于 2006-1-7 23:23 | 显示全部楼层
楼主不错,支持支持。
Log4j作为日志输出确实不错,给我们的调试等方面也都带来了极大的方便。其实jJDK1.4带的ava.util.logging也可以带有日志功能,但这个包的功能比log4j差的很远,性能也一般。所以现在很多项目都应用了Log4j。

[ Last edited by kinven0000 on 2006-1-7 at 23:30 ]
回复

使用道具 举报

发表于 2006-1-7 23:44 | 显示全部楼层
log4j已经是事实上的标准了。

很多流行的框架(如hibernate)就已经把log4j集成在框架中了。
回复

使用道具 举报

 楼主| 发表于 2006-1-8 23:57 | 显示全部楼层
没错.

log4j已经是事实上的标准了.

据说当年apache曾对sun提议把log4j加入java2的.但sun方因为已到发布日期为由没有采纳.
回复

使用道具 举报

发表于 2006-1-12 00:14 | 显示全部楼层
第一次接触,按楼主说的去做了一回,感觉不错.
以后有空也要学一学
谢谢了!
回复

使用道具 举报

 楼主| 发表于 2006-1-13 12:17 | 显示全部楼层
logging 家族现在有以下成员:
Log4Cxx (c++)
Log4j
Log4Net
Log4Perl
Log4PHP
Log4PLSQL
回复

使用道具 举报

发表于 2006-1-13 14:31 | 显示全部楼层
晕...都是啊帕奇搞的?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2024-4-30 22:45

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表