spring.xml不记录应用程序日志

w1e3prcc  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(467)

我使用的是springboot2.3.1版本,应用程序日志有问题。我的logback-spring.xml文件如下所示,

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ} [%thread] %-5level %logger{5} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="APP_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/var/log/app.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/var/log/app.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <encoder>
            <Pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ} %-5level %-12X{process} %-30([%thread]) %-29logger - %m%n</Pattern>
        </encoder>
    </appender>
    <logger name="com.sample" level="DEBUG" additivity="false"/>
    <logger name="org.springframework" level="INFO"/>

    <root level="DEBUG">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="APP_LOGGER"/>
    </root>
</configuration>

我的控制器是,

package com.sample;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;

@RestController
public class Controller {

  private static final Logger LOGGER = LoggerFactory.getLogger(Controller.class);

  @GetMapping("/")
  public void greetings(){
    LOGGER.info("AAAAA....");
    LOGGER.debug("DDDD...");
    LOGGER.warn("WWW...");
    LOGGER.error("EEEE....")
  }

}

我尝试在application.properties文件中添加以下内容,

logging.level.root=DEBUG
logging.level.org.springframework=INFO
logging.level.com.sample=DEBUG

运气不好,还尝试了以下其他替代方法,并添加了文件路径 logging.file.name=/var/log/app.logapplication.properties .

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />

    <logger name="org.springframework.web" level="INFO"/>
    <logger name="com.sample" level="DEBUG"/>

    <root level="DEBUG">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>

</configuration>

app.log文件正在创建,我在app.log文件中只看到与spring相关的日志,而没有看到应用程序日志。

f8rj6qna

f8rj6qna1#

您的配置运行良好。从中删除日志记录配置 application.properties . 你需要点击baseurl (example: http://localhost:8080/) 用于监视控制器内定义的日志。我修改了 logback.xml 一点点使用一个变量的日志目录。在这里:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="LOG_PATH" value="/home/none47/logs"/>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ} [%thread] %-5level %logger{5} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="APP_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/app.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/app.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <encoder>
            <Pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ} %-5level %-12X{process} %-30([%thread]) %-29logger - %m%n</Pattern>
        </encoder>
    </appender>
    <logger name="com.sample" level="DEBUG" additivity="false"/>
    <logger name="org.springframework" level="INFO"/>

    <root level="DEBUG">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="APP_LOGGER"/>
    </root>
</configuration>

相关问题