zoukankan      html  css  js  c++  java
  • springboot整合日志

    1,排除spring-boot-starter自带的logback依赖

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

    2、添加如下依赖:

            <!-- springboot-log4j -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j</artifactId>
            </dependency>

    3、创建log4j.properties,并添加相应配置信息

    #log4j.rootLogger=CONSOLE,info,error,DEBUG
    log4j.rootLogger=info,error,CONSOLE,DEBUG
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm}[%t][%c][%p]-%m%n
    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=/home/admin/pms-api-services/logs/info/api_services_info
    log4j.appender.info.File=D://logs/info/api_services_info
    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=/home/admin/pms-api-services/logs/error/api_services_error
    log4j.appender.error.File=D://logs/error/api_services_error
    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=/home/admin/pms-api-services/logs/debug/api_services_debug
    log4j.appender.DEBUG.File=D://logs/debug/api_services_debug

    4、在主要添加的类内部创建Logger对象,如:private static Logger log = Logger.getLogger(UserController.class);

    而后就可以分级别添加日志信息了。

    注意:如果不更改依赖,会出现日志文件无法导出的现象!

  • 相关阅读:
    Eclipse:构造函数不提示才发现
    Java:终于找到了在alloy中的JFileChooser中的弹出式菜单不显示文字的解决办法
    java:给图片上加水印
    Asp:函数是用传值还是传址
    数据库移植注意事项
    51nod 1009 数字1的数量
    51nod1003 阶乘后面0的数量
    51nod 1002 数塔取数问题
    2017 Multi-University Training Contest
    HDU 3251 Being a Hero 最小割
  • 原文地址:https://www.cnblogs.com/sunshinekevin/p/9227582.html
Copyright © 2011-2022 走看看