浅谈log4j-4-不同目的地(转)

public class DifferentAppender {

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

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

        //输出目的地
        Appender appender= null;
        //1控制台
        // appender=new ConsoleAppender(layout);
        //logger.addAppender(appender);

        /**
         * 2)org.apache.log4j.FileAppender(将日志信息输出到一个文件)
         *    将日志信息输出到文件中时可以设置一些控制属性,比如:
         *    1)filename    日志文件的名称,日志文件的全路径
         *    2)fileAppend    控制日志信息是否被附加到同一个文件的末尾,默认为true,意味着日志信息会被附加到同一文件的末尾
         *    3)bufferedIO    控制日志信息是否写入缓存,默认为false,意味着日志信息不会写入缓存之中
         *    4)bufferSize    如果 bufferedI/O 启用,表示缓冲区的大小,默认设置为8KB
         */
        //        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(将日志信息输出到一个文件,但是这个文件是可控的,可以配置多久产生一个新的日志信息文件),
         *    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,"log/log.txt","'.'yyyy-MM-dd-HH-mm");
        //            logger.addAppender(appender);
        //        } 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,"log/log.txt");
        //            rollingFileAppender.setMaximumFileSize(2L);
        //            rollingFileAppender.setMaxBackupIndex(5);
        //            appender = rollingFileAppender;
        //            logger.addAppender(appender);
        //        } catch (IOException e) {
        //            e.printStackTrace();
        //        }

        /**
         * 5)org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
         *    这个功能很强大,我们自定义日志信息的流向,这里为了方便演示,我就将他他输出到一个文件之中了
         */
        OutputStream os = null;
        try {
            os= new FileOutputStream("log/log.txt");
            appender= new WriterAppender(layout,os);
            logger.addAppender(appender);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

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

    }

}
原文地址:https://www.cnblogs.com/Danial7777777/p/9136803.html