日期:2014-05-20  浏览次数:20877 次

在项目中自己用java写个日志输出程序,如何根据日期不同,将日志写入到不同的文件夹里
package com.southinfo.kedaoservice.log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.util.Date;
import com.southinfo.kedaoservice.util.DateUtil;

public class Logger {
private Class<?> cls;
private PrintStream printStream;
public static Logger getLogger(Class<?> cls) {
String filePath = null;
try {
Class<?> forName = Class
.forName("com.southinfo.kedaoservice.core.Config");
Field declaredField = forName.getDeclaredField("ID");
filePath = "logs/[" + declaredField.get(null) + "]"
+ cls.getSimpleName() + ".log";
} catch (ClassNotFoundException e) {
filePath = "logs/[_]" + cls.getSimpleName() + ".log";
} catch (Exception e) {
System.err.println("生成日志文件出错!");
System.exit(1);
}
Logger logger = new Logger();
logger.cls = cls;
try {
logger.printStream = new PrintStream(new FileOutputStream(new File(
filePath)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return logger;
}
public void info(String message) {
info(message, null);
}
public void info(String message, Exception e) {
String output = DateUtil.format(new Date()) + " INFO "
+ cls.getSimpleName() + " " + message;
printStream.append(output + "\r\n");
System.out.println(output);
if (e != null) {
e.printStackTrace(printStream);
}
}
public void error(String message) {
error(message, null);
}
public void error(String message, Exception e) {
String output = DateUtil.format(new Date()) + " ERROR "
+ cls.getSimpleName() + " " + message;
printStream.append(output + "\r\n");
System.out.println(output);
if (e != null) {
e.printStackTrace(printStream);
}
}
public void debug(String message) {
debug(message, null);
  }
public void debug(String message, Exception e) {
String output = DateUtil.format(new Date()) + " DEBUG "
+ cls.getSimpleName() + " " + message;
System.out.println(output);
printStream.append(output + "\r\n");
if (e != null) {
e.printStackTrace(printStream);
}
}
public Class<?> getCls() {
return cls;
}
public void setCls(Class<?> cls) {
this.cls = cls;
}
public PrintStream getPrintStream() {
return printStream;
}
public void setPrintStream(PrintStream printStream) {
this.printStream = printStream;
}
}
上面是我写的日志类,下面是在其他类中调用日志,并把此类中的log输出:
private static Logger logger = Logger.getLogger(ServiceStarter.class);
现在用上面的内容可以实现日志正常输出功能,但是所有的日志都只能写入一个文件夹里面,例如:
E:\kedaoservice\logs\Connector.log
E:\kedaoservice\logs\ServiceStarter.log
E:\kedaoservice\logs\Connector.log
E:\kedaoservice\logs\ServiceEnd.log
E:\kedaoservice\logs\............
现在,我想实现的目的是把日志写入到不同的文件夹里面,根据日期来命名文件夹。例如:
E:\kedaoservice\2011-11-1\Connector.log
E:\kedaoservice\2011-11-1\............

E:\kedaoservice\2011-11-2\Connector.log
E:\kedaoservice\2011-11-2\............


------解决方案--------------------
Java code
public class Logger {
private Class<?> cls;
private PrintStream printStream;
private Calendar calendar = Calendar.getInstance();
...

public void info(String message, Exception e) {
checkDate(); //每次写log的时候,判断一下日期是否改变,如果改变,重新设置PrintStream
...
synchronized(this) { //注意同步
    printStream.append(output + "\r\n");
)

public void setPrintStream(PrintStream printStream) {
synchronized(this) {
    this.printStream = printStream;
}
}


protected void checkDate() {
    Calendar c = Calendar.getInstance();
    synchronized (this) {
        if (calendar.get(Calendar.DATE) != c.get(Calendar.DATE)) {
            calendar = c;
            filePath = ...;
            setPrintStream(new PrintStream(new FileOutputStream(filePath))); //注意同步
        }
    }
}