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


一:測試環境與log4j(一)——為什么要使用log4j?一樣,這里不再重述

 

二:老規矩,先來個栗子,然后再聊聊感受

import org.apache.log4j.*;

import java.io.*;
//by godtrue
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:/log4j/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)控制日志的輸出,我認為針對日志輸出的目的地的控制是最為要緊的,所以,這里需要好好的玩玩,徹底能明白輸出到每一個合適的目的地到底該如何控制

 


免責聲明!

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



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