Tag:log4j随笔
Article From:https://www.cnblogs.com/Danial7777777/p/9136803.html
public class DifferentAppender {

    private static Logger logger=Logger.getLogger(DifferentAppender.class);

    public static void main(String[] args) {
        //Output format
        String pattern="[%d] - %l - %p - %m%n";    
        Layout layout=new PatternLayout(pattern);

        //Output destination
        Appender appender= null;
        //1Console// appender=new ConsoleAppender(layout);
        //logger.addAppender(appender);

        /**
         * 2)org.apache.log4j.FileAppender(Output the log information to a file.* you can set some control attributes when exporting log information to a file, such as:* 1) the name of the filename log file and the full path of the log file.* 2) if the fileAppend control log information is attached to the end of the same file, the default is true, which means that the log information is attached to the end of the same file.* 3) buffeRedIO controls whether log information is written to the cache, defaults to false, which means that log information will not be written to the cache.* 4) bufferSize if bufferedI/O is enabled, it indicates the buffer area.The size is set to 8KB by default*/
        //        try {
        //            appender=new FileAppender(layout, "log/log.txt", false);
        //            logger.addAppender(appender);
        //        } catch (IOException e) {
        //            // TODO Auto-generated catch block
        //            e.printStackTrace();
        //        }

        /**
         * 3)org.apache.log4j.DailyRollingFileAppender(Output log information to a file, but the file is controllable, and how long it can be configured to produce a new log file.* DailyRollingFileAppender inherited from FileAppenderSo he has all the non private attributes of FileAppender, and at the same time he has more than one attribute to control when to produce a new log file datePattern.* datePattern has the following attribute values:* 1:'.'yyyy-MM Rollover at the beginning of each month* At midnight ofMay 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 it*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 onT, 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 to* 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 it* 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 until* 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 MarchHe beginning of the next hour.* 6:'.'yyyy-MM-dd-HH-mm Rollover at the beginning ofEvery minute.* At approximately 11:23000, 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 over** ForExample, if the File option is set to /foo/bar.log and the and, the 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 theNext day.*The above is the original text in API, which is probably DailyRollingFileAppender. This log file memory generation principle is based on matching.The datePattern is determined, such as:* we have a log file /foo/bar.log the datePattern we set is'.'yyyy-MM-dd, at 2001.-02-16 will generate a new log file in the early hours of the day.* the name of this log file is /foo/bar.log.2001-02-16, which is made from /foo/bar.log.Copied from the file* the log information generated on the day of 2001-02-17 will continue to be stored in the log file /foo/bar.log, and so on, the new log files will be produced continuously, and one day will produce one** datePattern has the following six common rules for re recording logs, translated into Chinese roughly the following meaning:* 1:'.'yyyy-MM RolThe lover at the beginning of each month copies a copy of the current log file and rerecords the log information in the original file, creating a new file based on the name of the original file.*2:'.'yyyy-ww Rollover at the first day of each week., the first day of each week, copy a copy of the current log file, and rerecord the log information in the original file, based on the original fileCreate a new file by the name* 3:'.'yyyy-MM-dd Rollover at midnight each day. copies the current log files in the early hours of the morning, and is in the original file.Rerecording log information will create a new file based on the name of the original file.* 4:'.'yyyy-MM-dd-a Rollover at midnight and midday of EACH day. copies a copy of the current log file every day in the morning and noon, and rerecords the log information in the original file, creating a new file based on the name of the original file.* 5:'.'yyyy-MM-dd-HH RolThe lover at the top of every hour. ends every hour, copies a copy of the current log file, and rerecords the log information in the original file, creating a new file based on the name of the original file.* 6:'.The'yyyy-MM-dd-HH-mm Rollover at the beginning of every minute. copies a copy of the current log file and rerecords the log information in the original file, based onThe name of the original file creates a new file*/

        //        try {
        //            appender = new DailyRollingFileAppender(layout,"log/log.txt","'.'yyyy-MM-dd-HH-mm");
        //            logger.addAppender(appender);
        //        } catch (IOException e) {
        //            e.printStackTrace();
        //        }

        /**
         * 4)org.apache.log4j.RollingFileAppender(The log information is output to a file, but the file is controlled, and you can specify a new file when the size of the file reaches the specified size.* RollingFileAppender inherits from FileAppendEr, so he has all the non private attributes of FileAppender, and at the same time, he has more than two controls to generate the properties of the new log file.** 1) maxFileSize when the log file isWhen the value reaches this value, a new log file will be generated. The default value is 10MB.* 2) maxBackupIndex this attribute represents the maximum number of backup files to be created, and the default value is 1. If this value is zero, no backup files will be generated.** if the following settings are set, setMaximumFileSize (2L) setMaxBackupIndex (5), then the way to generate log files is like this.* the first run program produces two log files* testRollingFileAppender.log testRollingFileAppender.log.1*The second run program produces three log files* testRollingFileAppender.log testRollingFileAppender.log.1 testRollingFiLeAppender.log.2*.* the fifth run program produces six log files* testRollingFileAppender.log tEstRollingFileAppender.log.1 testRollingFileAppender.log.2. TestRollingFileAppender.log.5* the n (n> 5) operation will still produce six log files.* testRollingFileAppender.log testRollingFileAppender.log.1 tEstRollingFileAppender.log.2. TestRollingFileAppender.log.5** from the above analysis we can see that,The generated log backup is no longer generating a new backup file when it is equal to the number it defines, at least from the name of the log file.But what's interesting is that every log file will change when you run the program again below, and the log files range from 1 to 5.Always keep the latest five* when we run the program again, a new log file will be generated, it will be named 1, the original 1 will be renamed 2, the original 2 will be renamed 3, and so on, until all the log files are renamed.* the longest log file will be deleted*/

        //        try {
        //            RollingFileAppender rollingFileAppender = new RollingFileAppender(layout,"log/log.txt");
        //            rollingFileAppender.setMaximumFileSize(2L);
        //            rollingFileAppender.setMaxBackupIndex(5);
        //            appender = rollingFileAppender;
        //            logger.addAppender(appender);
        //        } catch (IOException e) {
        //            e.printStackTrace();
        //        }

        /**
         * 5)org.apache.log4j.WriterAppender(Send log information in stream format to any specified place)* this function is very powerful, we customize the flow of log information, here to facilitate the demonstration, I will export him to a file.*/
        OutputStream os = null;
        try {
            os= new FileOutputStream("log/log.txt");
            appender= new WriterAppender(layout,os);
            logger.addAppender(appender);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        //output
        logger.warn(" this is a warn!!!");
        logger.info(" this is a info!!!");
        logger.debug(" this is a debug!!!");
        logger.error(" this is a error!!!");

    }

}

 

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *