自定义封装Logger demo 小测试

1》  log4j.properties 文件:

log4j.rootLogger=DEBUG,MINA,file

### 控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n

log4j.appender.MINA=org.apache.log4j.ConsoleAppender
log4j.appender.MINA.layout=org.apache.log4j.PatternLayout    
log4j.appender.MINA.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} %-5p %c{1} %x - %m%n

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:/logfile/minademos.log
log4j.appender.file.MaxFileSize=5120KB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[luozhonghua-error][%d] %p | %m | [%t] %C.%M(%L)%n


2》Log4jConfig:

import org.apache.log4j.PropertyConfigurator;
public class Log4jConfig {
private static boolean isReload = true;


/**
* 装载log4j配置文件
*/
public static void load() {
String path = Log4jConfig.class.getClass().getResource("/").getPath()
+ "config/log4j.properties";


System.out.println("log4j configfile path=" + path);
PropertyConfigurator.configureAndWatch(path, 1000);// 间隔特定时间,检测文件是否修改,自动重新读取配置
}


public static void main(String[] args) {
load();
}


private static void reload() {
if (isReload) {
load();
}
isReload = false;
}


public void setReload(boolean flag) {
isReload = flag;
}
}


3》Logger文件



import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class Logger {

private Log log = null;
static {
Log4jConfig.load();// 装载log4j配置文件
}


private Logger() {
log = LogFactory.getLog(this.getClass());
}


private Logger(Class c) {
log = LogFactory.getLog(c);
}


private Logger(String className) {
log = LogFactory.getLog(className);
}


public static Logger getLogger() {
return new Logger();
}


public static Logger getLogger(Class c) {


return new Logger(c);
}


public static Logger getLogger(String className) {
return new Logger(className);
}


public void trace(String info) {
if (log.isTraceEnabled())
log.trace(info);
}


public void debug(String info) {
if (log.isDebugEnabled())
log.debug(info);
}


public void info(String info) {
if (log.isInfoEnabled())
log.info(info);
}


public void warn(String info) {
if (log.isWarnEnabled())
log.warn(info);
}


public void error(String info) {
if (log.isErrorEnabled())
log.error(info);
}


     public void error(Object info, Throwable t){
    if (log.isErrorEnabled())
  log.error(info+","+t);
}

public void fatal(String info) {
if (log.isFatalEnabled())
log.fatal(info);
}


public boolean isTraceEnabled() {
return log.isTraceEnabled();
}


public boolean isDebugEnabled() {
return log.isDebugEnabled();
}


public boolean isInfoEnabled() {
return log.isInfoEnabled();
}


public boolean isWarnEnabled() {
return log.isWarnEnabled();
}


public boolean isErrorEnabled() {
return log.isErrorEnabled();
}


public boolean isFatalEnabled() {
return log.isFatalEnabled();
}
}


4:测试 

public class test {


static Logger log = Logger.getLogger(test.class);


public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
log.info("----------info");
log.debug("----------debug");
log.error("----------error");


System.out.println("***********************");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


测试通过,但貌似可用,不过log4j源码貌似强大应用场景



 

posted @ 2014-09-24 00:45  172257861  阅读(175)  评论(0编辑  收藏  举报