項目中logger、message錯誤信息的配置


申明:在一個項目中必不可少的是Logger和錯誤信息的配置,現在給出在我們常用的處理方法。

—、創建一個ConfigUtils類和他對應的rah.properties文件和Test測試類

ConfigUtis:

package com.rah;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigUtils {
    
    private static final String PROPERTIES_FILE = "com/rah/rah.properties";
    
    private static Properties prop = null;

    static{
        InputStream propStream = ConfigUtils.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE);
        prop = new Properties();
        try {
            prop.load(propStream);
        } catch (IOException e) {
            System.out.println("讀取文件失敗");
        } 
    }
    
    public static  String getProperty(String key){
        return prop.getProperty(key);
    }
}

rah.properties

photoDir=d:/temp/photo
videoDir=d:/temp/video

test

package com.rah;

public class Test {
    public static void main(String[] args) {
        String photoDir = ConfigUtils.getProperty("photoDir");
        String videoDir = ConfigUtils.getProperty("videoDir");
        System.out.println("photoDir Path is: " + photoDir);
        System.out.println("videoDir path is: " + videoDir);
    }
}

測試結果:

photoDir Path is: d:/temp/photo
videoDir path is: d:/temp/video

二、創建MessageManager類、message.properties和測試類Test.

MessageManager

package com.rah;

import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties;

public class MessageManager {
    private static final String PROPERTIES_FILE = "/properties/message.properties";
    private static Properties prop = null;
    static{
        InputStream propStream = MessageManager.class.getResourceAsStream(PROPERTIES_FILE);
        prop = new Properties();
        try {
            prop.load(propStream);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("讀取文件失敗");
        }
    }
    
    public static String getProperty(String messageCode){
        return prop.getProperty(messageCode);
    }
    
    public static String getProperty(String messageCode, String arg1){
        Object[] args = new Object[1];
        args[0] = arg1;
        
        return getFormatMessage(messageCode, args);
    }
    
    public static String getProperty(String messageCode, String arg1, String arg2){
        Object[] args = new Object[2];
        args[0] = arg1;
        args[1] = arg2;
        
        return getFormatMessage(messageCode, args);
    }
    
    public static String getProperty(String messageCode, String arg1, String arg2, String arg3){
        Object[] args = new Object[3];
        args[0] = arg1;
        args[1] = arg2;
        args[2] = arg3;
        
        return getFormatMessage(messageCode, args);
    }
    
    private static String getFormatMessage(String messageCode, Object[] args) {
        String argMessage = getProperty(messageCode);
        
        return MessageFormat.format(argMessage, args);
    }

}

Message.properties

MSG_E00001=password is not correct
MSG_E00002=country is {0}
MSG_E00003=country is {0} provice is {1}
MSG_E00004=country is {0} provice is {1} city is {2}

Test

package com.rah;

public class Test {
    public static void main(String[] args) {
        System.out.println("MSG_E00001 data is: " +  MessageManager.getProperty("MSG_E00001"));
        System.out.println("MSG_E00002 data is: " +  MessageManager.getProperty("MSG_E00002", "CHINA"));
        System.out.println("MSG_E00003 data is: " +  MessageManager.getProperty("MSG_E00003", "CHINA", "JIANGXI"));
        System.out.println("MSG_E00004 data is: " +  MessageManager.getProperty("MSG_E00004", "CHINA", "JIANGXI", "SHANGRAO"));
    }
}

測試結果:

MSG_E00001 data is: password is not correct
MSG_E00002 data is: country is CHINA
MSG_E00003 data is: country is CHINA provice is JIANGXI 
MSG_E00004 data is: country is CHINA provice is JIANGXI city is SHANGRAO 

三、Loger日志輸出,其實就是對log4j的一點封裝,方便開發人員使用

log4j-1.2.13.jar下載

public class Logger {

    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger
            .getLogger(org.apache.log4j.Logger.class);

    public static void debug(String message) {
        logger.debug(message);
    }

    public static void debug(String message, Throwable ex) {
        logger.debug(message, ex);
    }

    public static void info(String message) {
        logger.info(message);
    }

    public static void info(String message, Throwable ex) {
        logger.info(message, ex);
    }

    public static void error(String message) {
        logger.error(message);
    }

    public static void error(String message, Throwable ex) {
        logger.error(message, ex);
    }

    public static void fatal(String message) {
        logger.fatal(message);
    }

    public static void fatal(String message, Throwable ex) {
        logger.fatal(message, ex);
    }


    public static void warn(String message) {http://i.cnblogs.com/EditPosts.aspx?opt=1
        logger.warn(message);
    }

    public static void warn(String message, Throwable ex) {http://i.cnblogs.com/EditPosts.aspx?opt=1
        logger.warn(message, ex);
    }
}

四、對class.getResourceAsStream()、class.getClassLoader().getResourceAsStream()區別的分析

  思心的網友肯定會發現我上面的兩個測試分別采用了class.getResourceAsStream(),和class.getClassLoader().getResourceAsStream().其實一開始我也沒有注意,是在查API的時候發現有不同的方法,於是為了試試他們的用法特地采用了不同的寫法。

class.getResourceAsStream()會指定的加載的資源路徑與當前類所在的包的路徑一致

  像上面的MessageManager類如果寫成getResourceAsStream("message.properties")則他就只會在ciom.rah包下尋找,此時我們采用"/"開頭,那么就會從classpath的根路徑開始查找(SRC根目錄)getResourceAsStream("/properties/message.properties")就是在SRC目錄下創建了properties目錄接着創建了message.properties文件。

ClassLoader.gettResourceAsStream()無論要查找的資源前面是否有"/"都是從classpath的根路徑下查找。

  像上面的ConfigUtil類getResourceAsStream("/rah.properties")和("rah.properties")都是直接從SRC目錄下找rah.properties文件。

最后補充:

程序運行的是最后編譯成.class的文件。這個SRC目錄下的所有東西都會編譯在bin目錄下。

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM