Springboot日志集成

概 述

Java应用中,日志一般分为以下5个级别:

  • ERROR 错误信息
  • WARN 警告信息
  • INFO 一般信息
  • DEBUG 调试信息
  • TRACE 跟踪信息

Spring Boot使用Apache的Commons Logging作为内部的日志框架,其仅仅是一个日志接口,在实际应用中需要为该接口来指定相应的日志实现。
SpringBt默认的日志实现是Java Util Logging,是JDK自带的日志包,此外SpringBt当然也支持Log4J、Logback这类很流行的日志实现。

SpringBoot Logging

控制台输出

  1. 配置文件
logging.level.root=INFO
  1. controller
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoggerTestController {

   Logger logger = LoggerFactory.getLogger(this.getClass());

   @GetMapping("/test")
   public String test() {
       logger.info("test logger....");
       return "hello";
   }
}
  1. 启动
    Springboot日志集成_第1张图片
    Springboot日志集成_第2张图片

输出到文本

  1. 配置文件
logging:
  level:
    root: error # error 级别以下不会打印
    com.cloud.log.controller: debug # controller下的dug级别
  file: ${user.home}/logs/hello.log # 输出到文件
  1. 启动
    Springboot日志集成_第3张图片
    Springboot日志集成_第4张图片

集成 Log4J

  1. pom.xml(需要排除logging)
<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4j2artifactId>
        dependency>
    dependencies>
    
  1. application.yml
logging:
  level:
    root: error # error 级别以下不会打印
    com.cloud.log.controller: debug # controller下的dug级别
  1. log4j2.xml

<configuration status="warn">
    <properties>
        <Property name="app_name">log4j2Property>
        <Property name="log_path">logs/${app_name}Property>
    properties>

    <appenders>
        <console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%d][%t][%p][%l] %m%n"/>
        console>
        <RollingFile name="RollingFileInfo" fileName="${log_path}/info.log"
                filePattern="${log_path}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz">
            <Filters>
                <ThresholdFilter level="INFO"/>
                <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
            Filters>
            <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n"/>
            <Policies>
                
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                
                <SizeBasedTriggeringPolicy size="2 MB"/>
            Policies>
            
            <DefaultRolloverStrategy compressionLevel="0" max="10"/>
        RollingFile>

        <RollingFile name="RollingFileWarn" fileName="${log_path}/warn.log"
                filePattern="${log_path}/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz">
            <Filters>
                <ThresholdFilter level="WARN"/>
                <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
            Filters>

            <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n"/>
            <Policies>
                
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                
                <SizeBasedTriggeringPolicy size="2 MB"/>
            Policies>
            
            <DefaultRolloverStrategy compressionLevel="0" max="10"/>
        RollingFile>
        <RollingFile name="RollingFileError" fileName="${log_path}/error.log"
                filePattern="${log_path}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz">
            <ThresholdFilter level="ERROR"/>
            <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n"/>
            <Policies>
                
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                
                <SizeBasedTriggeringPolicy size="2 MB"/>
            Policies>
            
            <DefaultRolloverStrategy compressionLevel="0" max="10"/>
        RollingFile>
    appenders>
    <loggers>
        <root level="info">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
            <appender-ref ref="RollingFileWarn"/>
            <appender-ref ref="RollingFileError"/>
        root>
    loggers>
configuration>
  1. controller
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoggerTestController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @GetMapping("/test")
    public String test() {
        logger.info("test logger....");
        return "hello";
    }


    @GetMapping("/hello")
    public String hello() {
        for (int i = 0; i < 100000; i++) {
            logger.info("info execute index method");
            logger.warn("warn execute index method");
            logger.error("error execute index method");
        }
        return "My First SpringBoot Application";
    }
}
  1. 启动
    Springboot日志集成_第5张图片
    Springboot日志集成_第6张图片
    Springboot日志集成_第7张图片
    Springboot日志集成_第8张图片

你可能感兴趣的:(spring,boot,java,spring,log4j2)