Topic: log4j使用大全 |
Print this page |
1.log4j使用大全 | Copy to clipboard |
Posted by: chengbd Posted on: 2004-06-21 10:50 一:配置 A:)在Web Application 中使用log4j Step 1:配置log4j配置文件 ========================= # Set root logger level to DEBUG and its only appender to A1 #log4j中有五级logger #FATAL0 #ERROR3 #WARN4 #INFO6 #DEBUG7 log4j.rootLogger=DEBUG, A1 # A1 is set to be a ConsoleAppender. #log4j中Appender有几层 #ConsoleAppender输出到控制台 log4j.appender.A1=org.apache.log4j.ConsoleAppender # A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.PatternLayout #输出格式 具体查看log4j javadoc org.apache.log4j.PatternLayout #d 时间 .... log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n ================================另外一种配置 log4j.rootLogger=debug, stdout, R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n #R 输出到文件 log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=example.log log4j.appender.R.MaxFileSize=100KB # Keep one backup file log4j.appender.R.MaxBackupIndex=1 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n Step 2:写启动的Servlet public class InitServlet extends HttpServlet { public void init() { ServletContext sct = getServletContext(); String prefix = sct.getRealPath("/"); //log4j 配置文件存放目录 System.out.println("[....Log4j]: The Root Path: " + prefix); System.out.println("[....Log4j]: InitServlet init start..."); String file = getInitParameter("log4j"); //log4j 配置文件 if (file != null) { PropertyConfigurator.configure(prefix + file); //根据配置初始化log4j } System.out.println("[....Log4j]: InitServlet Init Sucess..."); } public void doGet(HttpServletRequest req, HttpServletResponse res) { } public void doPost(HttpServletRequest req, HttpServletResponse res) { } } B:在一般的IDE中使用log4j,因为使用log4j需要做一些初始化,在Web Application 中这一部分初始化是有AppServer 启动是加载的。在IDE中,则我们在使用log4j 之前需要自己配置一下。 如下文件: public final class Lo4jTest { private static String CLASS_NAME = "com.bingo.test.Lo4jTest"; private static Logger log = Logger.getLogger(CLASS_NAME); //在Web Application 中下面的pattern是在log4j的配置文件中配置的。 //现在我们手动配置 private static String pattern="%-4r [%-t] [%p] [%c] [%x] - [%m]%n"; private static String pattern2="%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n"; //设置输出层 // private static ConsoleAppender consAppender = new ConsoleAppender( new PatternLayout(pattern2)); public static void main(String[] args) { //配置log4j BasicConfigurator.configure(consAppender); // void configure(Appender appender); // void configure(); //配置时log4j提供了两种方式 //后一种比较简单,输出的信息不够详细 //我们可以使用前一种输出我们希望的格式 log.debug("Log4j Debug."); log.error("Log4j Error."); log.info("Log4j Info."); log.fatal("Log4j Fatal."); } } //使用pattern2上面的输出结果如下 2002-08-30 13:49:09 [com.bingo.test.Lo4jTest]-[DEBUG] Log4j Debug. 2002-08-30 13:49:09 [com.bingo.test.Lo4jTest]-[ERROR] Log4j Error. 2002-08-30 13:49:09 [com.bingo.test.Lo4jTest]-[INFO] Log4j Info. 2002-08-30 13:49:09 [com.bingo.test.Lo4jTest]-[FATAL] Log4j Fatal. //假如我们使用pattern输出如下: 0 [main] [DEBUG] [com.bingo.test.Lo4jTest] [] - [Log4j Debug.] 0 [main] [ERROR] [com.bingo.test.Lo4jTest] [] - [Log4j Error.] 0 [main] [INFO] [com.bingo.test.Lo4jTest] [] - [Log4j Info.] 0 [main] [FATAL] [com.bingo.test.Lo4jTest] [] - [Log4j Fatal.] 二:使用log4j 在实际使用过程中其实非常简单 1:)如下定义log,在log4j1.2以前使用Category,log4j1.2以后使用Logger代替 private static String CLASS_NAME = "com.bingo.test.Lo4jTest"; //log4j1.2 private static Logger log = Logger.getLogger(CLASS_NAME); //lo4j1.2以前 private static Category log =Category.getInstance(CLASS_NAME); //取得一个Category or Logger有几种方式,根据自己需要使用 2:)写日志 log.debug("Log4j Debug."); log.error("Log4j Error."); log.info("Log4j Info."); log.fatal("Log4j Fatal."); //void debug(Object message) //Log a message object with the DEBUG level. //void debug(Object message, Throwable t) //Log a message object with the DEBUG level including the //stack trace of the Throwable t passed as parameter. 三:注意事项 1:在具体使用中注意不同的信息使用不同的log方式,选择debug,error, info,fatal中的一种,以后可以根据需要屏蔽部分输出 2:开发过程中尽量输出到控制台,运行过程中则修改配置使其输出到文件. 3:定义log尽量使用文件名作为参数,这样容易查找错误. 出处: http://www.javaunion.org/bbs/cgi-bin/topic.cgi?forum=36&topic=20&show=25 |
2.Re:log4j使用大全 [Re: chengbd] | Copy to clipboard |
Posted by: helloworld Posted on: 2004-06-21 10:56 好文章! { This is kind of 灌水, I'll merge this with the next post. } |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |