1. 程式人生 > >log4j(五)——如何控制不同目的地的日誌輸出?

log4j(五)——如何控制不同目的地的日誌輸出?

主函數 exc not 文件復制 ron rst it is target sunday

一、測試環境

  與log4j(一)——為什麽要使用log4j?一樣,這裏不再重述

二、老規矩,先來個例子,然後再聊聊感受

package com.sc.log4j;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.log4j.Appender;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Layout; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout; import org.apache.log4j.WriterAppender; /** * Created by Miss黃 */ public class UseLog4j { //日誌記錄器 private static Logger LOGGER = LogManager.getLogger(UseLog4j.class
); //程序入口——主函數 public static void main(String[]args) { //設置日誌信息的格式化方式 String pattern = "[%d] - %l - %p - %m%n"; Layout layout = new PatternLayout(pattern); /** * 設置日誌信息的輸出目的地,日誌輸出的目的地主要有以下幾種: * 常用的公共屬性如下所示: * 1)immediateFlush 控制消息是否立即被輸出,默認值是true,意謂著所有的消息都會被立即輸出。 * 2)encoding 它可以使用任何字符編碼,默認情況下是特定於平臺的編碼方案 * 3)threshold 指定日誌消息的輸出最低層次。
*/ Appender appender= null; /** * 1)org.apache.log4j.ConsoleAppender(將日誌信息輸出到控制臺) * 可以設置日誌信息輸出的目標,通過 target 屬性 或 對應構造方法 來設置,目前有兩種屬性值可選 System.out 和 System.err 默認是System.out */ // appender = new ConsoleAppender(layout); // appender = new ConsoleAppender(layout,"System.err"); /** * 2)org.apache.log4j.FileAppender(將日誌信息輸出到一個文件) * 將日誌信息輸出到文件中時可以設置一些控制屬性,比如: * 1)filename 日誌文件的名稱,日誌文件的全路徑 * 2)fileAppend 控制日誌信息是否被附加到同一個文件的末尾,默認為true,意味著日誌信息會被附加到同一文件的末尾 * 3)bufferedIO 控制日誌信息是否寫入緩存,默認為false,意味著日誌信息不會寫入緩存之中 * 4)bufferSize 如果 bufferedI/O 啟用,表示緩沖區的大小,默認設置為8KB */ // try { // appender = new FileAppender(layout,"D:/log4j/testFileAppender.log"); // appender = new FileAppender(layout,"D:/log4j/testFileAppender.log",false); // } catch (IOException e) { // e.printStackTrace(); // } /** * 3)org.apache.log4j.DailyRollingFileAppender(將日誌信息輸出到一個文件,但是這個文件是可控的,可以配置多久產生一個新的日誌信息文件), * DailyRollingFileAppender 繼承自 FileAppender,所以他有 FileAppender 的所有非私屬性,同時他也多了一個控制何時產生一個新的日誌文件的屬性 datePattern * * datePattern 有以下幾種屬性值: * 1:‘.‘yyyy-MM Rollover at the beginning of each month * At midnight of May 31st, 2002 /foo/bar.log will be copied to /foo/bar.log.2002-05. * Logging for the month of June will be output to /foo/bar.log until it is also rolled over the next month. * 2:‘.‘yyyy-ww Rollover at the first day of each week. * The first day of the week depends on the locale. * Assuming the first day of the week is Sunday, on Saturday midnight, June 9th 2002, the file /foo/bar.log will be copied to /foo/bar.log.2002-23. * Logging for the 24th week of 2002 will be output to /foo/bar.log until it is rolled over the next week. * 3:‘.‘yyyy-MM-dd Rollover at midnight each day. * At midnight, on March 8th, 2002, /foo/bar.log will be copied to /foo/bar.log.2002-03-08. * Logging for the 9th day of March will be output to /foo/bar.log until it is rolled over the next day. * 4:‘.‘yyyy-MM-dd-a Rollover at midnight and midday of each day. * At noon, on March 9th, 2002, /foo/bar.log will be copied to /foo/bar.log.2002-03-09-AM. * Logging for the afternoon of the 9th will be output to /foo/bar.log until it is rolled over at midnight. * 5:‘.‘yyyy-MM-dd-HH Rollover at the top of every hour. * At approximately 11:00.000 o‘clock on March 9th, 2002, /foo/bar.log will be copied to /foo/bar.log.2002-03-09-10. * Logging for the 11th hour of the 9th of March will be output to /foo/bar.log until it is rolled over at the beginning of the next hour. * 6:‘.‘yyyy-MM-dd-HH-mm Rollover at the beginning of every minute. * At approximately 11:23,000, on March 9th, 2001, /foo/bar.log will be copied to /foo/bar.log.2001-03-09-10-22. * Logging for the minute of 11:23 (9th of March) will be output to /foo/bar.log until it is rolled over the next minute. * * For example, if the File option is set to /foo/bar.log and the DatePattern set to ‘.‘yyyy-MM-dd, on 2001-02-16 at midnight, * the logging file /foo/bar.log will be copied to /foo/bar.log.2001-02-16 and logging for 2001-02-17 will continue in /foo/bar.log * until it rolls over the next day. * * 上面是API中的原文,大概的意思是這樣的 DailyRollingFileAppender 這個日誌文件存儲器的生成原則是根據配置的 datePattern 來決定的,比如: * 我們有一個日誌文件 /foo/bar.log 我們設置的 datePattern 是 ‘.‘yyyy-MM-dd,在2001-02-16當天的淩晨,就會生成一個新的日誌文件了 * 這個日誌文件的名字是 /foo/bar.log.2001-02-16,這個文件的內容使從 /foo/bar.log 這個文件中拷貝過來的 * 2001-02-17當天產生的日誌信息,會繼續存放在日誌文件 /foo/bar.log 中,以此類推,會不斷的產生新的日誌文件,過一天就會產生一個 * * datePattern 有如上六種常見的重新記錄日誌的規則,翻譯成中文大概意思如下所示: * 1:‘.‘yyyy-MM Rollover at the beginning of each month 每個月的月初,將當前日誌文件復制一份,並且在原文件重新記錄日誌信息,會根據原文件的名稱創建一個新的文件 * 2:‘.‘yyyy-ww Rollover at the first day of each week. 每個周的第一天,將當前日誌文件復制一份,並且在原文件重新記錄日誌信息,會根據原文件的名稱創建一個新的文件 * 3:‘.‘yyyy-MM-dd Rollover at midnight each day. 每天的淩晨,將當前日誌文件復制一份,並且在原文件重新記錄日誌信息,會根據原文件的名稱創建一個新的文件 * 4:‘.‘yyyy-MM-dd-a Rollover at midnight and midday of each day. 每天的淩晨和中午,將當前日誌文件復制一份,並且在原文件重新記錄日誌信息,會根據原文件的名稱創建一個新的文件 * 5:‘.‘yyyy-MM-dd-HH Rollover at the top of every hour.每小時結束,將當前日誌文件復制一份,並且在原文件重新記錄日誌信息,會根據原文件的名稱創建一個新的文件 * 6:‘.‘yyyy-MM-dd-HH-mm Rollover at the beginning of every minute.每分鐘的開始,將當前日誌文件復制一份,並且在原文件重新記錄日誌信息,會根據原文件的名稱創建一個新的文件 */ // try { // appender = new DailyRollingFileAppender(layout,"D:/log4j/testDailyRollingFileAppender.log","‘.‘yyyy-MM-dd-HH-mm"); // } catch (IOException e) { // e.printStackTrace(); // } /** * 4)org.apache.log4j.RollingFileAppender(將日誌信息輸出到一個文件,但是這個文件是可控的,可以指定當文件大小到達指定尺寸的時候產生一個新的文件) * RollingFileAppender 繼承自 FileAppender,所以他有 FileAppender 的所有非私屬性,同時他也多兩個控制產生新日誌文件的屬性,如下所示: * * 1)maxFileSize 當日誌文件的大小達到此值時,會產生新的日誌文件,默認值是10MB * 2)maxBackupIndex 此屬性表示要創建的備份文件的最大數量,默認值是1,如果此值為零,則不會產生備份的文件 * * 如果進行如下的設置,setMaximumFileSize(2L) setMaxBackupIndex(5) 那麽產生日誌文件的方式是這樣的 * 第一次運行程序會產生兩個日誌文件 * testRollingFileAppender.log testRollingFileAppender.log.1 * 第二次運行程序會產生三個日誌文件 * testRollingFileAppender.log testRollingFileAppender.log.1 testRollingFileAppender.log.2 * 。。。 * 第五次運行程序會產生六個日誌文件 * testRollingFileAppender.log testRollingFileAppender.log.1 testRollingFileAppender.log.2 。。。 testRollingFileAppender.log.5 * 第n(n>5)次運行程序仍會產生六個日誌文件 * testRollingFileAppender.log testRollingFileAppender.log.1 testRollingFileAppender.log.2 。。。 testRollingFileAppender.log.5 * * 從上面的分析我們,可以看到,當生成的日誌備份等於自己定義的個數時就不在生成新的備份文件了,至少從日誌文件的名字上看是這樣的 * 但是好玩的地方在於,下面再次運行程序的時候每個日誌文件都會發生變化,日誌文件從1到5總是保持最新的五份 * 當我們再次運行程序的時候,會生成一份新的日誌文件,它會被命名1,原來的1會被重命名2,原來的2會被重命名3,以此類推,直到所有的日誌文件都重新命名為止 * 最久的那份日誌文件會被刪除掉 */ // try { // RollingFileAppender rollingFileAppender = new RollingFileAppender(layout,"D:/log4j/testRollingFileAppender.log"); // rollingFileAppender.setMaximumFileSize(2L); // rollingFileAppender.setMaxBackupIndex(5); // appender = rollingFileAppender; // } catch (IOException e) { // e.printStackTrace(); // } /** * 5)org.apache.log4j.WriterAppender(將日誌信息以流格式發送到任意指定的地方) * 這個功能很強大,我們自定義日誌信息的流向,這裏為了方便演示,我就將他他輸出到一個文件之中了 */ OutputStream os = null; try { os= new FileOutputStream("D:/testWriterAppender.log"); appender= new WriterAppender(layout,os); } catch (FileNotFoundException e) { e.printStackTrace(); } //設置日誌信息的輸出配置 BasicConfigurator.configure(appender); //輸出日誌信息 LOGGER.info(" my level is INFO"); } }

三:感受

1)這裏只列出了五種比較常用的日誌輸出目的地,還有好多別的,使用到的時候可以在回頭看看官方文檔,比如:輸出到數據庫,輸出到郵件等等

2)實際工作中使用哪一種,是需要根據具體的業務需求來定的,不過,輸出到控制臺和輸出到通過文件大小的閾值來決定再進行回滾的方式相對使用的比較多

3)註意日誌文件的保留大小和份數,如果過大可能會影響對應的主機的使用

4)上線的應用不應將日誌信息再往控制臺來輸出了,往控制臺輸出的情況,應該只針對代碼調試的情形

5)控制日誌的輸出,我認為針對日誌輸出的目的地的控制是最為要緊的,所以,這裏需要好好的玩玩,徹底能明白輸出到每一個合適的目的地到底該如何控制

log4j(五)——如何控制不同目的地的日誌輸出?