大家在项目中经常会用到Log4j,一般包括4个步骤:
1.编写log4j配置文件;
2.初始化log4j;
3.获取一个logger实例;
4.调用logger.debug() 等记录日志。
为了能详细控制每个包和类的日志输出,一般在每个类中都需要定一个logger实例,类似下面这样:
public class ContextFactory { private static final Logger s_logger = Logger.getLogger(ContextFactory.class); public void test() { s_logger.debug("this is debug."); } }
为了解决这个问题,本人对Log4j进行了一个简单的封装LogHelper.java:
import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; public class LogHelper { static { DOMConfigurator.configure("./config/log4j.xml"); } public static void info(Object msg) { StackTraceElement stack[] = (new Throwable()).getStackTrace(); Logger logger = Logger.getLogger(stack[1].getClassName()); logger.log(LogHelper.class.getName(), Level.INFO, msg, null); } public static void error(Object msg) { StackTraceElement stack[] = (new Throwable()).getStackTrace(); Logger logger = Logger.getLogger(stack[1].getClassName()); logger.log(LogHelper.class.getName(), Level.ERROR, msg, null); } public static void debug(Object msg) { StackTraceElement stack[] = (new Throwable()).getStackTrace(); Logger logger = Logger.getLogger(stack[1].getClassName()); logger.log(LogHelper.class.getName(), Level.DEBUG, msg, null); } public static void warn(Object msg) { StackTraceElement stack[] = (new Throwable()).getStackTrace(); Logger logger = Logger.getLogger(stack[1].getClassName()); logger.log(LogHelper.class.getName(), Level.WARN, msg, null); } public static void exception(Exception e) { StackTraceElement stack[] = (new Throwable()).getStackTrace(); Logger logger = Logger.getLogger(stack[1].getClassName()); logger.log(LogHelper.class.getName(), Level.ERROR, ExceptionUtil.toString(e), null); } }
而在LogHelper.debug()中封装了获取logger对象及获取调用者位置等步骤。
最后附上一个log4j.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <!-- Appenders --> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="Threshold" value="debug" /> <param name="Target" value="System.out" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{yyy-M-dd HH:mm:ss}] %-5p: %c - %m (%F:%L)%n" /> </layout> </appender> <!-- 服务器主日志文件 --> <appender name="file" class="org.apache.log4j.DailyRollingFileAppender"> <param name="Threshold" value="debug" /> <!-- The active file to log to --> <param name="file" value="logs/asynctask.log" /> <param name="Append" value="false" /><!-- 设置是否在重新启动服务时,在原有日志的基础添加新日志 --> <param name="append" value="true" /> <param name="encoding" value="UTF-8" /> <layout class="org.apache.log4j.PatternLayout"> <!-- The log message pattern --> <param name="ConversionPattern" value="[%d{yyy-M-dd HH:mm:ss}] %-5p: %c - %m (%F:%L)%n" /> </layout> </appender> <!-- Application Loggers --> <span style="color:#FF0000;"><strong><logger name="com.lenovo"> <level value="debug" /> </logger></strong></span> <strong><span style="color:#FF0000;"><logger name="org.apache"> <level value="warn" /> </logger></span></strong> <!-- Root Logger --> <span style="color:#FF0000;"><strong><root> <priority value="debug" /> <appender-ref ref="console" /> <appender-ref ref="file" /> </root></strong></span> </log4j:configuration>解释:
2. org.apache下的log显示warn信息。
3. root显示debug信息,记录到控制台和文件。
4.配置是有顺序的,虽然root是debug级别,但在他之前定义了org.apache是warn级别,所以org.apache只会输出warn级别的log。
原文地址:http://blog.csdn.net/jmppok/article/details/44828335