天天看點

自定義封裝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) {

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);

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源碼貌似強大應用場景