每個項目都要遇到的打log

每個項目都要求打log,我也編寫了一個適合我現在的機能的log類,它能夠每天,針對每一個線程創建一個文件並在裏邊填寫log文字。感覺還有可以提高的地方,比如,log級別debug和運行實現不同的log,好像要用到log4j的什麼東西,我沒深研究,但是,控制檯程序也沒那麼多配置。至於功能上,我覺得還有可以實現的功能,比如不跨行的單行打印,還有針對類名的多信息打印。

打出來的log類似:


[11:04:31]: system property:mail.smtp.host = lzt-server01
[11:04:31]: prepare to read mail body object
[11:04:31]: create mime mail body!
[11:04:31]: config smtp auth:mail.smtp.auth = true
[11:04:31]: config mail subject
[11:04:31]: set fromer!
[11:04:31]: config mail header :X-IFArea
[11:04:31]: sending mail....
[11:04:31]: sending mail succeed!

 

源程序如下:

/*
 * Logger.java
 *
 * Created on 2008.7.10, pm2:01
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package mail;
import com.sun.org.apache.bcel.internal.generic.CALOAD;
import java.io.*;
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
/**
 *
 * @author user
 */
public class Logger {
    
    /** Creates a new instance of Logger */
    public Logger() {
    }
    
    public static void println(String log){
        print(log,true);
    }
    
    public static void println(){
        print("",true);
    }
    
    public static void print(String log){
        print(log,false);
    }
    public static void print(String log, boolean newLine){
        try{
            //add time to the log
            log = getSysSecondInString() + log;
            
            //make log file
            File directory = new File("");
            String pathStr = directory.getAbsolutePath();
            File folder = new File(CONSTANT.logAddress);
            folder.mkdir();
            String fileName = CONSTANT.logPrefix + Thread.currentThread().getName() + "_"  + getSystimeInString();
            FileWriter writeFile = new FileWriter(pathStr + "//" + CONSTANT.logAddress + "//" + fileName + CONSTANT.logFileType ,true);
            
            //write log
            PrintWriter bw = new PrintWriter(writeFile);
            if(newLine){
                System.out.println("["+Thread.currentThread().getName() + "]" + log);
                bw.write("/r/n"+log);
            }else{
                System.out.print(log);
                bw.write(log);
            }
            bw.close();
            writeFile.close();
        }catch(FileNotFoundException ex){
            ex.printStackTrace();
        }catch(IOException ex){
            ex.printStackTrace();
        }
    }
    
    private static String getSysSecondInString(){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format=new SimpleDateFormat("[hh:mm:ss]:/t");
        String nowtime=format.format(calendar.getTime());
        return nowtime;
    }
    
    private static String getSystimeInString(){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format=new SimpleDateFormat("yyyy.MM.dd");
        String nowtime=format.format(calendar.getTime());
        return nowtime;
    }
    
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章