|
首先介绍一下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 ] |
|