• springboot整合log4j


    1、排除springboot的自带的logback

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <!-- 排除自带的logback依赖 -->
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    添加依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
        <version>1.3.8.RELEASE</version>
    </dependency>

    如果抛出异常

    SLF4J: Class path contains multiple SLF4J bindings.

    查看依赖(IDEA右侧)

    比如aop

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
        <exclusions>
        <!-- 排除自带的logback依赖 -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
        </exclusions>
    </dependency>

    也是需要排除的

    2、引入log4j

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
        <version>1.3.8.RELEASE</version>
    </dependency>

    3、配置

    application.yml

    logging:
      # 设置logback.xml位置
      #  config: classpath:log/logback.xml
      # 设置log4j.properties位置
      config: classpath:log4j.properties

    log4j.properties

    log4j.rootLogger=DEBUG,error,CONSOLE,info
    
    # appender
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender     
    # PatternLayout
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout     
    log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n    
    
    # info
    log4j.logger.info=info
    log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.info.layout=org.apache.log4j.PatternLayout     
    log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n
    log4j.appender.info.datePattern='.'yyyy-MM-dd
    log4j.appender.info.Threshold = info
    log4j.appender.info.append=true
    log4j.appender.info.File=D:/dance/demo/log/info.log
    
    log4j.logger.error=error  
    log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.error.layout=org.apache.log4j.PatternLayout     
    log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
    log4j.appender.error.datePattern='.'yyyy-MM-dd
    log4j.appender.error.Threshold = error   
    log4j.appender.error.append=true
    log4j.appender.error.File=D:/dance/demo/log/error.log
    
    log4j.logger.DEBUG=DEBUG
    log4j.appender.DEBUG=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.DEBUG.layout=org.apache.log4j.PatternLayout     
    log4j.appender.DEBUG.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
    log4j.appender.DEBUG.datePattern='.'yyyy-MM-dd
    log4j.appender.DEBUG.Threshold = DEBUG   
    log4j.appender.DEBUG.append=true
    log4j.appender.DEBUG.File=D:/dance/demo/log/dubug.log

    4. 这里对log4j2的记录一下,log4j2的简单配置 

    <configuration status="OFF" monitorInterval="30">
        <Appenders>
            <!--设置在控制台打印日志-->
            <Console name="console" target="SYSTEM_OUT">
                <PatternLayout
                        pattern="%highlight{%date{yyyy-MM-dd HH:mm:ss.SSS}456789 %-5level [%-6.-6pid] %-20.-20logger{1.}[line:%-4line] [%thread] - %msg%n}"/>
            <RegexFilter regex=".*securityLog.*" onMatch="ACCEPT" onMismatch="DENY"/> </Console> <!--自定义appender--> <Console name="console_security_appender" target="SYSTEM_OUT"> <PatternLayout pattern="%m%n"/>
            <RegexFilter regex=".*securityLog.*" onMatch="DENY" onMismatch="ACCEPT"/> </Console> </Appenders> <Loggers> <!-- security_log 日志 --> <Logger name="security_log" additivity="true"> <AppenderRef ref="console_security_appender"/> </Logger> <!--debug级别以上都输出到console--> <Root level="DEBUG"> <AppenderRef ref="console"/> </Root> </Loggers> </configuration>

    需要在application.yml引入

    logging:
      config: classpath:config/log4j2.xml

    以上的 log4j2.xml 和 application.yml 都在resouce的config目录

    参考博客:

    https://blog.csdn.net/u010663021/article/details/108388817

    关于log4j2的详细过滤器

    https://logging.apache.org/log4j/2.x/manual/filters.html

    转载自: https://cloud.tencent.com/developer/article/1782929

  • 相关阅读:
    信息安全大赛出的题目
    RedHat6 —— 配置IP地址
    PHP环境下配置WebGrind——让你的网站性能看得见
    PHP实例——验证邮件的主机是否存在
    Linux ——记一记那恐怖的 rm f
    这大半天就耗在一个jQuery跨域Json上了——jQuery跨域获取json数据总结
    常用的Mysql数据库操作语句大全
    ZOJ 3209 Treasure Map
    HDU 3452 Bonsai
    HDU 2577 How to Type
  • 原文地址:https://www.cnblogs.com/xuweiqiang/p/16292268.html
Copyright © 2020-2023  润新知