log4j日志处理工具类的使用代码:[quote]
import java.io.*;
import java.net.URISyntaxException;
import org.apache.log4j.*;
public class Log {
private static String CLASS_NAME = "com.bingo.test.Lo4jTest";
private static Logger log = Logger.getLogger(CLASS_NAME);
public Log() {
}
static {
PropertyConfigurator.configureAndWatch("log4j.properties");
}
public static void log(String str) {
int nDebug = 0;
String strDebug=null;
strDebug = Config.getProp("Debug");
if(strDebug!=null)
nDebug = Integer.parseInt(strDebug);
log(str,nDebug);
}
public static void log(String str,int i) {
if (Config.getProp("log").equalsIgnoreCase("yes")) {
if(i==0){
log.info(str);
}
else if(i==1){
// log.debug(str);
}
else if(i==2){
log.error(str);
}
else{
log.fatal(str);
}
}
else if (Config.getProp("log").equalsIgnoreCase("print")) {
System.out.println(str);
}
else {
//do nothing;
}
}
public static void main(String arg[]) {
//配置log4j
// PropertyConfigurator.configure("log4j.properties");
// File file = null;
// try {
// file = new File(Log.class.getResource("log4j.properties").toURI());
// if(file.exists()){
// System.out.println(file.getName());
// System.out.println(file.getAbsolutePath());
// }
// } catch (URISyntaxException e) {
// e.printStackTrace();
// }
PropertyConfigurator.configureAndWatch("src/log4j.properties");
log.debug("Log4j Debug.");
log.error("Log4j Error.");
log.info("Log4j Info.");
log.fatal("Log4j Fatal.");
log.fatal("log4j.rootLogger.");
System.out.println("aaaaaaaaa");
}
}[/quote]
log4j.properties 文件内容为:
log4j.rootLogger=DEBUG,A2
#log4j.rootLogger=INFO, A2
#log4j.logger.DAO=DEBUG,A2
log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.File=./interface.log
log4j.appender.A2.MaxFileSize=1024KB
log4j.appender.A2.MaxBackupIndex=10
#log4j.appender.A2.DatePattern='.'yyyy-MM-dd
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss===}%m%n
#method:%l%n%m%n
#log4j.appender.R=org.apache.log4j.RollingFileAppender
使用中注意配置文件的相对位置
import java.io.*;
import java.net.URISyntaxException;
import org.apache.log4j.*;
public class Log {
private static String CLASS_NAME = "com.bingo.test.Lo4jTest";
private static Logger log = Logger.getLogger(CLASS_NAME);
public Log() {
}
static {
PropertyConfigurator.configureAndWatch("log4j.properties");
}
public static void log(String str) {
int nDebug = 0;
String strDebug=null;
strDebug = Config.getProp("Debug");
if(strDebug!=null)
nDebug = Integer.parseInt(strDebug);
log(str,nDebug);
}
public static void log(String str,int i) {
if (Config.getProp("log").equalsIgnoreCase("yes")) {
if(i==0){
log.info(str);
}
else if(i==1){
// log.debug(str);
}
else if(i==2){
log.error(str);
}
else{
log.fatal(str);
}
}
else if (Config.getProp("log").equalsIgnoreCase("print")) {
System.out.println(str);
}
else {
//do nothing;
}
}
public static void main(String arg[]) {
//配置log4j
// PropertyConfigurator.configure("log4j.properties");
// File file = null;
// try {
// file = new File(Log.class.getResource("log4j.properties").toURI());
// if(file.exists()){
// System.out.println(file.getName());
// System.out.println(file.getAbsolutePath());
// }
// } catch (URISyntaxException e) {
// e.printStackTrace();
// }
PropertyConfigurator.configureAndWatch("src/log4j.properties");
log.debug("Log4j Debug.");
log.error("Log4j Error.");
log.info("Log4j Info.");
log.fatal("Log4j Fatal.");
log.fatal("log4j.rootLogger.");
System.out.println("aaaaaaaaa");
}
}[/quote]
log4j.properties 文件内容为:
log4j.rootLogger=DEBUG,A2
#log4j.rootLogger=INFO, A2
#log4j.logger.DAO=DEBUG,A2
log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.File=./interface.log
log4j.appender.A2.MaxFileSize=1024KB
log4j.appender.A2.MaxBackupIndex=10
#log4j.appender.A2.DatePattern='.'yyyy-MM-dd
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss===}%m%n
#method:%l%n%m%n
#log4j.appender.R=org.apache.log4j.RollingFileAppender
使用中注意配置文件的相对位置