package io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
*
* @author chunhuif 2008-7-3
* @version 1.0.0
*/
public class MyLog4j {
private static String fileName="mylog.txt"; //文件路径
private static String maxFileSize = "10k"; //单个文件大小的最大值 可以为 6M 、10G 、3m等等字符格式
private static String maxBackupIndex ="10"; //最多备份文件数
private int maxIndex;
private long maxSize; //文件最大值
private BufferedWriter bw;
private long currentSize=0; //byte
private File myFile;
public MyLog4j(){
super();
init();
}
private void init(){
myFile = new File(fileName);
try {
rollingFile();
} catch (IOException e) {
e.printStackTrace();
}
currentSize=myFile.length();
maxIndex=Integer.valueOf(maxBackupIndex.trim());
if (maxFileSize.contains("k")) {
maxSize = Long.valueOf(maxFileSize.replace("k", "").trim()) * 1024;
} else if (maxFileSize.contains("K")) {
maxSize = Long.valueOf(maxFileSize.replace("K", "").trim()) * 1024;
} else if (maxFileSize.contains("m")) {
maxSize = Long.valueOf(maxFileSize.replace("m", "").trim()) * 1024 * 1024;
} else if (maxFileSize.contains("M")) {
maxSize = Long.valueOf(maxFileSize.replace("M", "").trim()) * 1024 * 1024;
} else if (maxFileSize.contains("G")) {
maxSize = Long.valueOf(maxFileSize.replace("G", "").trim()) * 1024 * 1024 * 1024;
} else if (maxFileSize.contains("g")) {
maxSize = Long.valueOf(maxFileSize.replace("g", "").trim()) * 1024 * 1024 * 1024;
} else if (maxFileSize.contains("T")) {
maxSize = Long.valueOf(maxFileSize.replace("T", "").trim()) * 1024 * 1024 * 1024 * 1024;
} else if (maxFileSize.contains("t")) {
maxSize = Long.valueOf(maxFileSize.replace("t", "").trim()) * 1024 * 1024 * 1024 * 1024;
}
}
private synchronized void rollingFile() throws IOException{
File f = new File(fileName);
if(f.exists()){
rolling(f);
}
f.createNewFile();
bw = new BufferedWriter(new FileWriter(f,true));
}
/**
* 重命名,内部调用例如 将mylog.txt 命名为mylog1.txt mylog1.txt 命名为mylog2.txt
* @param ff
*/
private void rolling(File ff){
if(bw!=null)
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
String fn = ff.getName();
String preFileName=null;
String tailFileName=null;
int fileNum = 1;
if(fileName.contains(".")){
int index = fileName.lastIndexOf(".");
preFileName=fileName.substring(0,index);
String str=fn.substring(preFileName.length(),fn.indexOf("."));
if(str!=null&&!str.equals("")){
fileNum = Integer.valueOf(str)+1;
}
tailFileName=fileName.substring(index);
}else {
preFileName=fileName;
fileNum = Integer.valueOf(fn.substring(preFileName.length()));
tailFileName="";
}
if(fileNum<=maxIndex){
File nextFile=new File(preFileName+fileNum+tailFileName);
if(nextFile.exists()){
rolling(nextFile);
}
ff.renameTo(nextFile);
}else{
ff.delete();
}
}
/**
* 将信息写入log中
* @param message
*/
public synchronized void write(String message){
if(currentSize+message.getBytes().length>maxSize){
try {
rollingFile();
currentSize=0;
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bw.write(message);
bw.flush();
currentSize+=message.getBytes().length;
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 将信息写入log中
* @param message
*/
public synchronized void writeLine(String message){
write(message+"\n");
}
/**
* 将文件 file 中的内容写入log中
* @param
*/
public synchronized void write(File file){
if(!file.exists()){
System.out.println("\""+file.getName()+"\" not exist!");
}
BufferedReader br=null;
try {
br= new BufferedReader(new FileReader(file));
String message=null;
while((message=br.readLine())!=null)
write(message+"\n");
} catch (IOException e1) {
e1.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
final MyLog4j log = new MyLog4j();
for(int l=0;l<50;l++){
Thread t = new Thread(){
public void run(){
for(int i=0;i<10;i++){
log.write(Thread.currentThread().getName()+" "+i+"\n");
}
}
};
t.setName("Thread_"+l);
t.start();
}
for(int i=0;i<10;i++){
MyLog4j log1 = new MyLog4j();
log1.write(Thread.currentThread().getName()+" = "+i+"\n");
}
}
}
|