Log日志学习

Log4j2


Spring-boot使用

一,引入依赖

      <dependency><!-- 去除自带的日志框架. -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency> <!-- 引入log4j2依赖 -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

二,编写配置文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appenders>
      <!-- 标准输出 -->
    <Console name="STDOUT" target="SYSTEM_OUT">
      <!-- 输出格式 -->
      <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}][%p][%l][%t]%m%n"/>
    </Console>
  </appenders>
  <loggers>
    <!-- 配置记录器级别 -->
    <root level="debug">
          <appender-ref ref="STDOUT"/>
    </root>
    <logger name="org.springframework" level="INFO" />
  </loggers>
</configuration>

注:配置文件一般放在resource文件夹下

三,配置文件的引用设置

  在application.properties 中写入: logging.config=classpath:log4j2.xml  (配置文件放在resource文件夹下及与application.properties同级)

四,使用

package com.springboot.market.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloSpring {

    private static final Logger log = LogManager.getLogger(HelloSpring.class);

    @RequestMapping(path = "/index.tml", method = RequestMethod.GET)
    public String temp_index() {
        log.debug("返回响应!");
        return "Hello Spring Boot";
    }
}

jar包引用


一,引入依赖

        <dependency>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-api</artifactId>
          <version>2.8</version>
        </dependency>

        <dependency>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-core</artifactId>
          <version>2.8</version>
        </dependency>

        <dependency>
          <groupId>com.lmax</groupId>
          <artifactId>disruptor</artifactId>
          <version>3.3.6</version>
        </dependency>

二,配置文件同上(位置相同)

三,可以使用了(不需要指定配置文件啥的)

猜你喜欢

转载自www.cnblogs.com/iszhangheng/p/10413592.html