Usefull links
1.2 JavaDoc: http://logging.apache.org/log4j/1.2/apidocs/index.html
1.2 Maual Short Introduction: http://logging.apache.org/log4j/1.2/manual.html
Quick Start
Example - Uses the root logger to process logs
Main process MyApp.java
package log4J;
import log4J.com.foo.Bar;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class MyApp {
// Define a static logger variable so that it references the
// Logger instance named "MyApp".
static Logger logger = Logger.getLogger(MyApp.class);
public static void main(String[] args) {
// Set up a simple configuration that logs on the console.
BasicConfigurator.configure();
logger.info("Entering application.");
Bar bar = new Bar();
bar.doIt();
logger.info("Exiting application.");
}
}
Source code of BasicConfigurator.configure();
public class BasicConfigurator {
............................
static public void configure() {
Logger root = Logger.getRootLogger();
root.addAppender(new ConsoleAppender(
new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));
}
............................
}
BasicConfigurator.configure() configures the root logger to process the logs manually
Functional process Bar.java
package log4J.com.foo;
import org.apache.log4j.Logger;
public class Bar {
static Logger logger = Logger.getLogger(Bar.class);
public void doIt() {
logger.debug("Did it again!");
}
}
The logs printed back
0 [main] INFO log4J.MyApp - Entering application. 16 [main] DEBUG log4J.com.foo.Bar - Did it again! 16 [main] INFO log4J.MyApp - Exiting application.
Deeper Log4J
Concept
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
1. Logger Hierarchy
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.
Example belows,
- "com.foo" is the parent of the logger named "com.foo.bar"
- "java" is the parent of "java.util" and is the ancestor of "java.util.List"
Root Logger
- Root logger at the top of the Logger Hierarchy
- it always exists,
- it cannot retrieved by name. Invoking the class static Logger.getRootLogger method retrieves it.
All the other loggers are instantiated and retrieved with the class static Logger.getLogger method, This method takes the name(always the class name) of the desired logger as a parameter.
Source code of 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);
}
2. Assign Level
3. Assign Level Inheritance
4. Selection Rule
Appender
... in progress
Layout
... in progress
本文介绍如何使用 Log4J 进行日志记录配置,包括基本配置、日志级别设置及继承规则等核心概念,并通过示例展示如何手动配置根日志处理器。

被折叠的 条评论
为什么被折叠?



