一、入门log4j实例
    1.1 下载解压log4j.jar(地址:http://mirrors.hust.edu.cn/apache/logging/log4j/2.11.0/
    log4j - 图1
    1.2 新建工程,导入jar包
    log4j - 图2

    1. package test6;
    2. import org.apache.log4j.Logger;
    3. /**
    4. * @author mmb
    5. * @function test log4j
    6. * */
    7. public class Log4jTest {
    8. private static Logger logger = Logger.getLogger(Log4jTest.class);
    9. public static void main(String [] args){
    10. //BasicConfigurator.configure(); //自动快速地使用缺省Log4j环境。
    11. logger.info("aaaa");
    12. logger.debug("bbbb");
    13. logger.error("cccc");
    14. }
    15. }

    log4j.properties 文件

    1. ### log4j.properties 文件
    2. ### 设置###
    3. log4j.rootLogger = debug,stdout,D,E
    4. ### 输出信息到控制抬 ###
    5. log4j.appender.stdout = org.apache.log4j.ConsoleAppender
    6. log4j.appender.stdout.Target = System.out
    7. log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
    8. log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
    9. ### 输出DEBUG 级别以上的日志到=E://logs/error.log ###
    10. log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
    11. log4j.appender.D.File = E://logs/log.log
    12. log4j.appender.D.Append = true
    13. log4j.appender.D.Threshold = DEBUG
    14. log4j.appender.D.layout = org.apache.log4j.PatternLayout
    15. log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
    16. ### 输出ERROR 级别以上的日志到=E://logs/error.log ###
    17. log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
    18. log4j.appender.E.File =E://logs/error.log
    19. log4j.appender.E.Append = true
    20. log4j.appender.E.Threshold = ERROR
    21. log4j.appender.E.layout = org.apache.log4j.PatternLayout
    22. log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

    image.png