• 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;
    
    @Component
    public 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;
    
    @Component
    public 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;
    
    @SpringBootApplication
    public 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)

  • 相关阅读:
    2017-2018-1 20155326 实验四 外设驱动程序设计
    2017-2018-1 20155326 20155320《信息安全技术》实验四 木马及远程控制技术
    20155326 2017-2018-1 《信息安全系统设计基础》第六章课上考试题
    6月20日云栖精选夜读:阿里怎么发工资?自研薪酬管理系统首次曝光
    一个成功的研发团队应具备的9大属性
    那些创业的艰辛整理
    明明可以靠脸吃饭偏要靠才华_你身边有女神程序员吗?
    程序猿们_一二三四线城市你更愿意选择去哪里工作?
    微服务架构实践之邮件通知系统改造
    谈谈“僵尸猎手小明”手游兼容性踩到的坑
  • 原文地址:https://www.cnblogs.com/lm970585581/p/9842174.html
Copyright © 2020-2023  润新知