public class AccLogBase {
/**致命错误. */
public static final int LOG_LEVEL_FATAL = 1;
/** 错误. */
public static final int LOG_LEVEL_ERROR = 2;
/** 警告. */
public static final int LOG_LEVEL_WARN = 3;
/**信息. */
public static final int LOG_LEVEL_INFO = 4;
/** debug. */
public static final int LOG_LEVEL_DEBUG = 5;
/** 定义log基类 */
private static Logger log = null;
/**
* 获得log实例.
* @param cls 输出log日志class
*/
@SuppressWarnings("unchecked")
protected final void getLogInstance(final Class cls) {
if (log == null) {
log = Logger.getLogger(cls);
}
}
/**
* 构造函数.
*/
public AccLogBase() {
//这里面将log4j.properties放到src路径下,如果是weblogic服务器,放到domain目录下
//或者通过 Properties prop = new Properties();InputStream inputStream = new FileInputStream(“C:/log4j.properties”); //log4j.properties的绝对路径
//prop.load(inputStream); PropertyConfigurator.configure(prop);
PropertyConfigurator.configure("log4j.properties");
getLogInstance(this.getClass());
}
/**
* 输出log日志.
* @param className
* @param methodName
* @param level
* @param msg
*/
protected final void writeLog(final String className,final String msg, final String methodName,final Throwable exp,final int level) {
StringBuffer message = new StringBuffer();
message.append("[");
message.append(className);
message.append("#");
message.append(methodName);
message.append("]");
switch (level) {
case LOG_LEVEL_FATAL:
if (exp != null) {
log.fatal(message.toString(), exp);
} else {
log.fatal(message.toString());
}
break;
case LOG_LEVEL_ERROR:
if (exp != null) {
log.error(message.toString(), exp);
} else {
log.error(message.toString());
}
break;
case LOG_LEVEL_WARN:
if (exp != null) {
log.warn(message.toString(), exp);
} else {
log.warn(message.toString());
}
break;
case LOG_LEVEL_INFO:
if (exp != null) {
log.info(message.toString(), exp);
} else {
log.info(message.toString());
}
break;
case LOG_LEVEL_DEBUG:
if (exp != null) {
log.debug(message.toString(), exp);
} else {
log.debug(message.toString());
}
break;
default:
break;
}
}
}
public class TestLog extends AccLogBase {
/**
* 构造函数.
*/
public TestLog() {
}
private String className = this.getClass().getSimpleName();
private String methodName = "";
/**
* 测试日志输出.
*/
public final void testLog () {
methodName = new Throwable().getStackTrace()[0].getMethodName();
String message = “测试日志输出”;
//+ 処理開始,输出日志文件。
writeLog(className,message, methodName,"",4);
}
}
#######log4j.properties配置信息########
log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=./servers/AdminServer/logs/acc_servelet.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %m %n log4j.rootLogger=DEBUG, file