java Logger 的使用與配置

來源:互聯網
上載者:User
Logger所對應的屬性檔案在安裝jdk目錄下的jre/lib/logging.properties  (比如在我當前的機子裡是C:\Program Files\Java\jre6\lib\logging.properties)
logging.properties檔案中的      (logging.properties檔案裡麵包含了Logger的配置資訊,包括日誌在哪兒輸出,什麼level的,等等)
handlers= java.util.logging.ConsoleHandler  將日誌內容輸出到控制台
handlers= java.util.logging.FileHandler     將記錄檔輸出到檔案中
handlers= java.util.logging.ConsoleHandler,java.util.logging.FileHandler 將日誌內容同時輸出到控制台和檔案中

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO   //日誌輸出層級

java.util.logging.FileHandler.pattern = %h/java%u.log  為產生的輸出檔案名稱指定一個模式。
模式由包括以下特殊組件的字串組成,則運行時要替換這些組件:
"/" 本地路徑名分隔字元 "%t" 系統臨時目錄 "%h" "user.home" 系統屬性的值 "%g" 區分迴圈日誌的組建編號 "%u" 解決衝突的惟一號碼 "%%" 轉換為單個百分數符號"%"  
如果未指定 "%g" 欄位,並且檔案計數大於 1,那麼組建編號將被添加到所組建檔案名末尾的小數點後面。 

java.util.logging.FileHandler.limit = 50000   限制檔案的大小,以位元組為單位
java.util.logging.FileHandler.count = 1       指定有多少輸出檔案參與迴圈(預設為 1)。
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter      指定要使用的 Formatter 類的名稱(預設為 java.util.logging.XMLFormatter)。 另外一個是:java.util.logging. SimpleFormatter。XMLFormatter是以xml樣式輸出,SimpleFormatter是以普通樣式輸出。
java.util.logging.FileHandler.append 指定是否應該將 FileHandler 追加到任何現有檔案上(預設為 false)。

以下是我在類中實現日誌輸出的一個例子

import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.logging.ConsoleHandler;import java.util.logging.FileHandler;import java.util.logging.Level;import java.util.logging.Logger;import java.util.logging.SimpleFormatter;/** * 日誌輸出到登陸系統使用者所在的根目錄的郵政儲蓄日誌的檔案夾下, * 並且根據系統日期命名記錄檔 * @author wang * */public class LoggerUtil {        /** 存放的檔案夾 **/    private static String file_name = "郵政儲蓄日誌";        /**     * 得到要記錄的日誌的路徑及檔案名稱     * @return     */    private static String getLogName() {        StringBuffer logPath = new StringBuffer();        logPath.append(System.getProperty("user.home"));        logPath.append("\\"+file_name);        File file = new File(logPath.toString());        if (!file.exists())            file.mkdir();                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");        logPath.append("\\"+sdf.format(new Date())+".log");                return logPath.toString();    }        /**     * 配置Logger對象輸出記錄檔路徑     * @param logger      * @throws SecurityException     * @throws IOException     */    public static void setLogingProperties(Logger logger) throws SecurityException, IOException {        setLogingProperties(logger,Level.ALL);    }        /**     * 配置Logger對象輸出記錄檔路徑     * @param logger     * @param level 在記錄檔中輸出level層級以上的資訊     * @throws SecurityException     * @throws IOException     */    public static void setLogingProperties(Logger logger,Level level) {        FileHandler fh;        try {            fh = new FileHandler(getLogName(),true);            logger.addHandler(fh);//日誌輸出檔案            //logger.setLevel(level);            fh.setFormatter(new SimpleFormatter());//輸出格式            //logger.addHandler(new ConsoleHandler());//輸出到控制台        } catch (SecurityException e) {            logger.log(Level.SEVERE, "安全性錯誤", e);        } catch (IOException e) {            logger.log(Level.SEVERE,"讀取檔案日誌錯誤", e);        }    }        public static void main(String [] args) {        Logger logger = Logger.getLogger("sgg");        try {            LoggerUtil.setLogingProperties(logger);            logger.log(Level.INFO, "ddddd");            logger.log(Level.INFO, "eeeeee");            logger.log(Level.INFO, "ffffff");            logger.log(Level.INFO, "gggggg");            logger.log(Level.INFO, "hhhhhh");        } catch (SecurityException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }            }}
注意:Logger logger = Logger.getLogger("sgg");  sgg只能出現一次,否則回重複輸出值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.