赞
踩
java异步写日志到文件中详解
实现代码:
package com.tydic.ESUtil;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
public class LogWriter {
// 日志的配置文件
public static final String LOG_CONFIGFILE_NAME = "log.properties";
// 日志文件名在配置文件中的标签
public static final String LOGFILE_TAG_NAME = "logfile";
// 默认的日志文件的路径和文件名称
private final String DEFAULT_LOG_FILE_NAME = "./logtext.log";
// 该类的唯一的实例
private static LogWriter logWriter;
// 文件输出流
private PrintWriter writer;
// 日志文件名
private String logFileName;
/**
* 默认构造函数
*/
private LogWriter() throws LogException{
this.init();
}
private LogWriter(String fileName) throws LogException{
this.logFileName = fileName;
this.init();
}
/**
* 获取LogWriter的唯一实例。
* @return
* @throws LogException
*/
public synchronized static LogWriter getLogWriter()throws LogException{
if (logWriter == null){
logWriter = new LogWriter();
}
return logWriter;
}
public synchronized static LogWriter getLogWriter(String logFileName)throws LogException{
if (logWriter == null){
logWriter = new LogWriter(logFileName);
}
return lo
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。