log4j.rootLogger=WARN, stdout, logfile log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.RollingFileAppender log4j.appender.logfile.File=${log4jDIR}/dialOut.log log4j.appender.logfile.append=true log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d [%c] - %m%n log4j.appender.logfile.MaxFileSize=2MB log4j.appender.logfile.MaxBackupIndex=3
The 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; else return "/" + 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; else return "/" + 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] - 0 2009-04-28 17:37:13,123 [com.rv.stresstest.logger.TestLog] - 1 2009-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