Log4j

log4j.xml
1. <?xml version="1.0" encoding="UTF-8" ?> 
2. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">  
3. <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" >   
4.     <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">   
5.         <layout class="org.apache.log4j.PatternLayout"/>   
6.     </appender>   
7.     <root >  
8.         <appender-ref ref="CONSOLE"/>   
9.     </root> 
10. </log4j:configuration>

Log4j等级(Level):all < trace < debug < info < warn < error < fatal < off
Log4j有三个主要组件: loggers、appenders和layouts

封装的log4j:
public class LoggerHelper {
private Logger logger = null;
private Class clazz = null;
public LoggerHelper(Class clazz){
this.clazz = clazz;
logger = Logger.getLogger(LoggerHelper.class);
}
public void debug(String message){
logger.debug(String.format("[%s] %s", clazz.getName(), message));
}
public void info(String message){
logger.info(String.format("[%s] %s", clazz.getName(), message));
}
public void warn(String message){
logger.warn(String.format("[%s] %s", clazz.getName(), message));
}
public void error(String message){
logger.error(String.format("[%s] %s", clazz.getName(), message));
}
public void fatal(String message){
logger.fatal(String.format("[%s] %s", clazz.getName(), message));
}
}

补充:1.添加private static final Logger logger = Logger.getLogger(Test.class);
      2.在src目录添加log4j.xml

你可能感兴趣的:(log4j,xml)