62,567
社区成员




package com.jankey.log;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Log {
@SuppressWarnings("unused")
public Logger loger;
private static Log log;
private Log() {
String filePath = this.getClass().getResource("/").getPath();
filePath = filePath.substring(1).replace("bin", "src");
loger = Logger.getLogger(this.getClass());
PropertyConfigurator.configure(filePath + "log4j.properties");
}
public static Log getLoger() {
if (null != log) {
return log;
} else {
return new Log();
}
}
}
package com.jankey.testLog;
import com.jankey.log.Log;
public class LogTest {
private Log log = Log.getLoger();
public void info(Object obj) {
log.loger.info(obj);
}
public static void main(String[] args) {
LogTest lt = new LogTest();
try {
int i = 0;
int[] a = new int[]{0, 1, 2, 3, 4};
i = a[10];
}catch(Exception ex) {
lt.info(ex);
ex.printStackTrace();
}
}
}
log4j.properties内容:
log4j.rootLogger=DEBUG, R
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.file=D:/logs/Debug.log
log4j.appender.R.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.R.Append=true
log4j.appender.R.ImmediateFlush=true
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%n%n%d%p[%c]-%m%n