日志

作者: Yanl__ | 来源:发表于2020-01-04 15:16 被阅读0次

    日志级别:
    从高到低分别是 ERROR、WARN、INFO、DEBUG。

    解决方案

    1.slf4j+logback

    image.png
    1. 先排除commons-logging
      将依赖commons-logging的包全部排除
    <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <!--            排除commons-loggin-->
                <exclusions>
                    <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
    1. 用中间包来替换原有的日志框架
    <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>jcl-over-slf4j</artifactId><!-- 替换commons-logging-->
           <version>1.7.25</version>
         </dependency> 
         <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>jul-to-slf4j</artifactId><!-- 替换java.util.logging-->
           <version>1.7.25</version>
    </dependency>
    

    日志配置文件

                <!-- 日志 -->
                <dependency>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                    <version>${log4j.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                    <version>${slf4j.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                    <version>${slf4j.version}</version>
                </dependency>
                <dependency>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                    <version>${logback.version}</version>
                </dependency>
    
    1. 配置文件logback.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
            </encoder>
        </appender>
    
        <root level="INFO">
            <appender-ref ref="STDOUT" />
        </root>
    
    </configuration>
    
    1. 测试使用
    public class Slf4j {
        public static void main(String[] args) {
            Logger log = LoggerFactory.getLogger(Slf4j.class);
    
            //        {} 为占位符,可以接收后面的参数
            log.debug("debug消息 id={},name={}", 1, "zhang");
            log.info("info msg");
            log.warn("warn msg");
            log.error("error msg");
    
        }
    }
    

    相关文章

      网友评论

          本文标题:日志

          本文链接:https://www.haomeiwen.com/subject/jtaxactx.html