六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 44|回复: 0

a application of log4j

[复制链接]

升级  46.33%

99

主题

99

主题

99

主题

举人

Rank: 3Rank: 3

积分
339
 楼主| 发表于 2013-1-15 02:57:42 | 显示全部楼层 |阅读模式
log4j.rootLogger=WARN, stdout, logfilelog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%nlog4j.appender.logfile=org.apache.log4j.RollingFileAppenderlog4j.appender.logfile.File=${log4jDIR}/dialOut.loglog4j.appender.logfile.append=truelog4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=%d [%c] - %m%nlog4j.appender.logfile.MaxFileSize=2MBlog4j.appender.logfile.MaxBackupIndex=3The above code block is log4j.properties, you can save this file anywhere.
Look at the block, you can find a parameter:log4jDIR. This is a user define one. you could understand it at the following code block.
 
package com.rv.stresstest.logger;import java.io.File;import org.apache.log4j.Level;import org.apache.log4j.Logger;import org.apache.log4j.PropertyConfigurator;import com.rv.stresstest.StressApplication;public class RVAppLog {private static Logger logger;@SuppressWarnings("unchecked")public static Logger getLogger(Class clazz, Level level) {if (logger == null) {String path = StressApplication.getInstance().getRootPath();System.setProperty("log4jDIR", path+"logs"+File.separator);String property = path + "log4j.properties";PropertyConfigurator.configure(property);logger = Logger.getLogger(clazz);logger.setLevel(level);}return logger;}} 
In this block, I use a class to find the path. You can review this class in the following .
package com.rv.stresstest;public class StressApplication {private static StressApplication a;private StressApplication() {}public static StressApplication getInstance() {if (a == null)a = new StressApplication();return a;}public String getClassRootPath() {try {String result = StressApplication.class.getResource("StressApplication.class").toString();int index = result.indexOf("classes");result = result.substring(0, index) + "classes";if (result.startsWith("jar")) {result = result.substring(10);} else if (result.startsWith("file")) {result = result.substring(6);}String os = System.getProperty("os.name");if (os.indexOf("Windows") >= 0)return result;elsereturn "/" + result;} catch (Exception e) {return "";}}public String getRootPath() {String result = StressApplication.class.getResource("StressApplication.class").toString();int index = result.indexOf("WEB-INF");if (index == -1) {index = result.indexOf("bin");}result = result.substring(0, index);if (result.startsWith("jar")) {result = result.substring(10);} else if (result.startsWith("file")) {result = result.substring(6);}String os = System.getProperty("os.name");if (os.indexOf("Windows") >= 0)return result;elsereturn "/" + result;}} 
At last, I will give guys a test.
package com.rv.stresstest.logger;import org.apache.log4j.Level;import org.apache.log4j.Logger;public class TestLog {public static void main(String[] args) {Logger logger = RVAppLog.getLogger(TestLog.class, Level.DEBUG);for (int i = 0; i < 111; i++) {logger.debug(i);}}}The result log as the following.
 
2009-04-28 17:37:13,123 [com.rv.stresstest.logger.TestLog] - 02009-04-28 17:37:13,123 [com.rv.stresstest.logger.TestLog] - 12009-04-28 17:37:13,123 [com.rv.stresstest.logger.TestLog] - 2……2009-04-28 17:37:13,154 [com.rv.stresstest.logger.TestLog] - 110 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表