码迷,mamicode.com
首页 > 编程语言 > 详细

Springboot日志初探

时间:2020-05-30 11:02:19      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:coder   system   boot   from   you   factory   mba   ons   iter   

1.新建module,springboot-log

2.pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lxcourse</groupId>
    <artifactId>springboot-log</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-log</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3.application.properties

logging.pattern.console=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %thread %c %M ---- %msg%n 
logging.level.com.lxcourse=trace
logging.file.path=/Users/doubledumbao/logs/springboot-log
logging.pattern.file=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %thread %c %M ---- %msg%n 
spring.profiles.active=dev

4.logback-spring.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>

    <property name="log_dir" value="/Users/doubledumbao/logs/logger"></property>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

        <target>System.err</target>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <springProfile name="dev">
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread]------- %msg%n</pattern>
            </springProfile>
            <springProfile name="pro">
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] xxxxxxxx %msg%n</pattern>
            </springProfile>
        </encoder>
    </appender>

    <!-- 自定义logger对象  additivity="false" 自定义logger对象是否继承rootLogger -->
    <logger name="com.lxcourse" level="info" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>
</configuration>

5.测试类

package com.lxcourse.springbootlog;

import org.apache.logging.log4j.LogManager;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootLogApplicationTests {

    public static final Logger LOGGER = LoggerFactory.getLogger(SpringbootLogApplication.class);

    @Test
    public void testQuick(){
        LOGGER.error("error");
        LOGGER.warn("warn");
        LOGGER.info("info");
        LOGGER.debug("debug");
        LOGGER.trace("trace");

        org.apache.logging.log4j.Logger logger = LogManager.getLogger(SpringbootLogApplication.class);
        logger.info("log4j2 log");
    }

}

6.切换日志实现

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lxcourse</groupId>
    <artifactId>springboot-log</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-log</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 排除logback依赖 -->
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 添加log4j2依赖 -->
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" monitorInterval="5">
    <properties>
        <property name="LOG_HOME">/Users/doubledumbao/logs/logger</property>
    </properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_ERR">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] [%-5level] %c{36}:%L --- %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

Springboot日志初探

标签:coder   system   boot   from   you   factory   mba   ons   iter   

原文地址:https://www.cnblogs.com/zhaoran8775/p/12990758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!