纵有疾风起
人生不言弃

SpringBoot日志

工程结构:

SpringBoot日志插图

默认使用logback日志

pom

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.example</groupId>    <artifactId>boot-logging</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>boot-logging</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.0.6.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <!-- 使用logback日志时取消注释        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>-->        <!--使用log4j2-->        <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>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-log4j2</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

在application.properties中可以直接配置

# 设置日志级别(默认是info)#logging.level.com.example.bootlogging.b.TestLogger1 = debug#logging.level.com.example.bootlogging.a.TestLogger = debug## 设置日志文件的名称及路径## logging.file=d:/logs/my.log## 设置日志文件存储的位置#logging.path=d:/tmp/logs## 设置控制台输出格式#logging.pattern.console=%d{HH:mm:ss.SSS} [boot-logging] [%t] %-5level %logger{36} - %msg%n## 设置文件的输出格式#logging.pattern.file= %d{HH:mm:ss.SSS} [boot-logging] [%t] %-5level %logger{36} - %msg%n

还可以使用XML配置,名字是固定的logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?><configuration debug="false">    <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->    <property name="LOG_HOME" value="/home"/>    <!-- 控制台输出 -->    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>        </encoder>    </appender>    <!-- 按照每天生成日志文件 -->    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">            <!--日志文件输出的文件名-->            <FileNamePattern>${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.log</FileNamePattern>            <!--日志文件保留天数-->            <MaxHistory>30</MaxHistory>        </rollingPolicy>        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>        </encoder>        <!--日志文件最大的大小-->        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">            <MaxFileSize>10MB</MaxFileSize>        </triggeringPolicy>    </appender>    <!-- 日志输出级别 -->    <root level="INFO">        <appender-ref ref="STDOUT"/>    </root></configuration>

如果使用log4j2

建立log4j2-spring.xml文件

<?xml version="1.0" encoding="UTF-8"?><!-- 设置log4j2的自身log级别为warn --><!-- OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL --><configuration status="WARN" monitorInterval="30">    <appenders>        <console name="Console" target="SYSTEM_OUT">            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>        </console>    </appenders>    <loggers>        <root level="all">            <appender-ref ref="Console"/>        </root>    </loggers></configuration>

 

 

package com.example.bootlogging.a;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;@Componentpublic class TestLogger {    // 获取日志对象    private Logger logger = LoggerFactory.getLogger(TestLogger.class);    public void testLog() {        logger.warn("警告日志");        logger.info("信息日志");        logger.debug("调试日志");        logger.error("错误日志");    }}

 

package com.example.bootlogging.b;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;@Componentpublic class TestLogger1 {    // 获取日志对象    private Logger logger = LoggerFactory.getLogger(TestLogger1.class);    public void testLog() {        logger.warn("警告日志-b");        logger.info("信息日志-b");        logger.debug("调试日志-b");        logger.error("错误日志-b");    }}

 

测试类:

package com.example.bootlogging;import com.example.bootlogging.a.TestLogger;import com.example.bootlogging.b.TestLogger1;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplicationpublic class BootLoggingApplication {    public static void main(String[] args) {        ConfigurableApplicationContext                context = SpringApplication.run(BootLoggingApplication.class, args);        TestLogger logger = context.getBean(TestLogger.class);        logger.testLog();        TestLogger1 logger1 = context.getBean(TestLogger1.class);        logger1.testLog();        context.close();    }}

输出结果:(log4j2)

SpringBoot日志插图(1)

 

文章转载于:https://www.cnblogs.com/lm970585581/p/9842174.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » SpringBoot日志

分享到: 生成海报
avatar

评论 抢沙发

评论前必须登录!

立即登录   注册

切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活