a application of log4j
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 - 02009-04-28 17:37:13,123 - 12009-04-28 17:37:13,123 - 2……2009-04-28 17:37:13,154 - 110
页:
[1]