mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 log 相关的目录。还未完成
This commit is contained in:
@@ -1,13 +1,20 @@
|
||||
package cn.iocoder.springboot.lab37.loggingdemo;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(Application.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
|
||||
// 打印日志
|
||||
logger.debug("just do it");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package cn.iocoder.springboot.lab37.loggingdemo.controller;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo")
|
||||
public class DemoController {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@GetMapping("/debug")
|
||||
public void debug() {
|
||||
logger.debug("debug");
|
||||
}
|
||||
|
||||
@GetMapping("/info")
|
||||
public void info() {
|
||||
logger.info("info");
|
||||
}
|
||||
|
||||
@GetMapping("/error")
|
||||
public void error() {
|
||||
logger.error("error");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<configuration>
|
||||
|
||||
<!-- 引入 Spring Boot 默认的 logback XML 配置文件 -->
|
||||
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
|
||||
|
||||
<springProperty scope="context" name="applicationName" source="spring.application.name"/>
|
||||
|
||||
<property name="LOG_FILE" value="/Users/yunai/logs/${applicationName}.log"/>
|
||||
|
||||
<!-- 控制台 Appender -->
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<!-- 日志的格式化 -->
|
||||
<encoder>
|
||||
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
|
||||
<charset>utf8</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- 从 Spring Boot 配置文件中,读取 spring.application.name 应用名 -->
|
||||
<springProperty name="applicationName" scope="context" source="spring.application.name" />
|
||||
<!-- 日志文件的路径 -->
|
||||
<property name="LOG_FILE" value="/Users/yunai/logs/${applicationName}.log"/>
|
||||
<!-- 日志文件 Appender -->
|
||||
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${LOG_FILE}</file>
|
||||
<!--滚动策略,基于时间 + 大小的分包策略 -->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
|
||||
<maxHistory>7</maxHistory>
|
||||
<maxFileSize>10MB</maxFileSize>
|
||||
</rollingPolicy>
|
||||
<!-- 日志的格式化 -->
|
||||
<encoder>
|
||||
<pattern>${FILE_LOG_PATTERN}</pattern>
|
||||
<charset>utf8</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="console"/>
|
||||
<appender-ref ref="file"/>
|
||||
</root>
|
||||
<!-- 测试环境,独有的配置 -->
|
||||
<springProfile name="dev">
|
||||
<!-- 设置 Appender -->
|
||||
<root level="INFO">
|
||||
<appender-ref ref="console"/>
|
||||
</root>
|
||||
|
||||
<!-- 设置 "cn.iocoder.springboot.lab37.loggingdemo" 的 Logger 的日志级别为 DEBUG -->
|
||||
<logger name="cn.iocoder.springboot.lab37.loggingdemo" level="DEBUG"/>
|
||||
</springProfile>
|
||||
|
||||
<!-- 生产环境,独有的配置 -->
|
||||
<springProfile name="prod">
|
||||
<!-- 设置 Appender -->
|
||||
<root level="INFO">
|
||||
<appender-ref ref="console"/>
|
||||
<appender-ref ref="file"/>
|
||||
</root>
|
||||
</springProfile>
|
||||
|
||||
</configuration>
|
||||
|
||||
Reference in New Issue
Block a user