log4j 介绍(1)-- Logger篇

本文详细介绍了Log4j的三个核心组件:Logger、Appender和Layout。解释了它们如何协同工作来实现不同级别的日志记录,并控制运行时的日志格式及报告位置。还深入探讨了Logger层次结构的概念及其在日志系统中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[color=darkred][b][size=large]Loggers, Appenders and Layouts[/size][/b][/color]

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.

Logger用来定义level级别和所处的层(hierarchy);Apender用来定义将这些Log messages存放在哪里;Layout用来定义存放log messages的格式;

了解定义
[color=darkred][b]Logger hierarchy[/b][/color]

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.

For example, the logger named "com.foo" is a parent of the logger named "com.foo.Bar". Similarly, "java" is a parent of "java.util" and an ancestor of "java.util.Vector". This naming scheme should be familiar to most developers.

一个logger对象可以有parent和ancestor,比如
Logger logger1 = Logger.getLogger("com");
Logger logger2 = Logger.getLogger("com.foo");
Logger logger3 = Logger.getLogger("com.foo.bar");

logger1是logger2的parent,是logger3的ancestor;

The root logger resides at the top of the logger hierarchy. It is exceptional in two ways:
(这里没明白)
1. it always exists,
2. it cannot be retrieved by name.
Invoking the class static Logger.getRootLogger method retrieves it. All other loggers are instantiated and retrieved with the class static Logger.getLogger method.
//root logger有待继续理解,不甚了解

package org.apache.log4j;

public class Logger {

// Creation & retrieval methods:
public static Logger getRootLogger();
public static Logger getLogger(String name);

// printing methods:
public void trace(Object message);
public void debug(Object message);
public void info(Object message);
public void warn(Object message);
public void error(Object message);
public void fatal(Object message);

// generic printing method:
public void log(Level l, Object message);
}


http://logging.apache.org/log4j/1.2/manual.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值