package com.zhihua.file;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Properties;
/**
* 日志工具类
* 使用了单例模式,保证只有一个实例
* 为了更方便的配置日志文件名,使用属性文件配置。
* 也可以在程序中指定日志文件名
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月16日]
*/
public class LogWriterUtils {
//日志的配置文件
public static final String LOG_CONFIGFILE_NAME = "log.properties";
//日志文件名在配置文件中的标签
public static final String LOGFILE_TAG_NAME = "logfile";
//默认的日志文件的路径和文件名称
private static final String DEFAULT_LOG_FILE_NAME = "./logtext.log";
//该类的唯一的实例
private static LogWriterUtils logWriter;
//文件输出流
private PrintWriter writer;
//日志文件名
private String logFileName;
/**
* 默认构造函数
* @throws Exception
*/
private LogWriterUtils()throws Exception{
this.init();
}
private LogWriterUtils(String fileName)throws Exception{
this.logFileName = fileName;
this.init();
}
/**
* 初始化LogWriterUtils
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月16日]
* @throws Exception
*/
private void init() throws Exception {
//如果用户没有在参数中指定日志文件名,则从配置文件中获取
if(this.logFileName == null){
this.logFileName = this.getLogFileNameFromConfigFile();
//如果配置文件不存在或者没有指定日志文件名,则用默认的日志文件名
if(logFileName == null){
this.logFileName = DEFAULT_LOG_FILE_NAME;
}
}
File logFile = new File(this.logFileName);
try{
/**
* 其中的FileWriter()中的第二个参数的含义是:是否在文件中追加内容
* PrintWriter()中的第二个参数的含义是:自动将数据flush到文件中
*/
writer = new PrintWriter(new FileWriter(logFile, true), true);
System.out.println("日志文件的位置:" + logFile.getAbsolutePath());
}catch (IOException ex) {
String errmsg = "无法打开日志文件:" + logFile.getAbsolutePath();
throw new Exception(errmsg);
}
}
/**
* 从配置文件名中取日志文件名
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @return
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月16日]
*/
private String getLogFileNameFromConfigFile() {
try {
Properties pro = new Properties();
//在类的当前位置,查找属性配置文件log.properties
InputStream fin = getClass().getResourceAsStream(LOG_CONFIGFILE_NAME);
if(fin!=null){
pro.load(fin);//载入配置文件
fin.close();
return pro.getProperty(LOGFILE_TAG_NAME);
}else{
System.out.println("无法打开属性配置文件:log.properties");
}
} catch (IOException e) {
System.out.println("无法打开属性配合文件:log.properties");
e.printStackTrace();
}
return null;
}
/**
* 获取LogWriterUtils的唯一实例
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @return
* @throws Exception
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月16日]
*/
public synchronized static LogWriterUtils getLogWriter()throws Exception{
if (logWriter == null){
logWriter = new LogWriterUtils();
}
return logWriter;
}
public synchronized static LogWriterUtils getLogWriter(String logFileName)throws Exception{
if (logWriter == null){
logWriter = new LogWriterUtils(logFileName);
}
return logWriter;
}
/**
* 往日志文件中写一条日志信息
* 为了防止多线程同时操作日志文件,造成文件“死锁”。使用synchronized关键字
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @param logMsg
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月16日]
*/
public synchronized void log(String logMsg){
this.writer.println(new Date()+": "+logMsg);
}
/**
* 往日志文件中写一条异常信息
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @param e
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月16日]
*/
public synchronized void log(Exception e){
writer.println(new Date()+": ");
e.printStackTrace(writer);
}
/**
* 关闭LogWriter
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月16日]
*/
public void close(){
logWriter = null;
if(writer!=null){
writer.close();
}
}
}
测试类:
package com.zhihua.file.test;
import org.junit.Test;
import com.zhihua.file.LogWriterUtils;
public class LogWriterUtilsTest {
/**
*
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月16日]
* @throws Exception
*/
@Test
public void log() throws Exception{
LogWriterUtils log = null;
String fileName = "E:/log.log";
log = LogWriterUtils.getLogWriter(fileName);
log.log("first log!");
log.log("First log!");
log.log("第二个日志信息");
log.log("Third log");
log.log("第四个日志信息");
log.close();
}
}