Spring boot logback to file 在 tomcat 上不起作用

问题描述 投票:0回答:3

我正在

tomcat
上运行一个打包为 War 的 Spring Boot 应用程序,并使用
logback
进行控制台和文件。

只要我作为Java应用程序运行,就可以了。我可以在控制台和文件中看到日志。 但是,在服务器上运行时,我没有看到日志打印到文件中。

我尝试设置记录器管理器,但不起作用。我想知道是否有人遇到过类似的问题。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <property name="LOG_FILE" 
     value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}app.log}"/>    
    <property name="LOG_FILE_MAX_SIZE" value="10MB" />
    <property name="LOG_TOTAL_SIZE_CAP" value="100MB" />
    <property name="LOG_FILE_MAX_HISTORY" value="20" />
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>
spring-boot tomcat logback
3个回答
0
投票

验证您是否具有以下依赖项:

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

或者即使您添加了

spring-boot-starter-web
依赖项,日志记录也应该可以工作。 并在 yml 或属性文件中包含以下内容:

logging.path=logs
logging.file=${logging.path}/log.log
logging.pattern.file=%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n

你也可以有一个

logback.xml
并使用 spring 默认的
base.xml
,这样所有默认的 spring 配置也适用于你的日志记录:

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.springframework.web" level="DEBUG"/>
</configuration>

0
投票

这是我的 logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <property name="LOG_FILE" 
     value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}app.log}"/>    
    <property name="LOG_FILE_MAX_SIZE" value="10MB" />
    <property name="LOG_TOTAL_SIZE_CAP" value="100MB" />
    <property name="LOG_FILE_MAX_HISTORY" value="20" />
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>

-1
投票

我可以通过将日志写入不同的文件夹来修复它,看起来应用程序没有对路径的写访问权限,但是我需要对 springboot 主类进行一些更改以加载基于应用程序道具的配置文件,请找到下面的类。不确定其他人是否也必须这样做。

无论如何,我很高兴它终于可以工作了:)

公共类Application扩展SpringBootServletInitializer{

public String PROFILE = null;
private static String CONFIG_LOCATION = null;

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    //Grab the active profile from the servlet conext
    PROFILE = servletContext.getInitParameter("spring.profiles.active");
    CONFIG_LOCATION = servletContext.getInitParameter("spring.config.path");        
    super.onStartup(servletContext);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    //...and pass it to the boot application
    application.application().setAdditionalProfiles(PROFILE);
    return application.sources(Application.class).properties(getProperties());
}

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

//For Loading config from server 
static Properties getProperties() {
    Properties props = new Properties();
    props.put("spring.config.location", CONFIG_LOCATION);
    return props;
}

}

web.xml

<context-param>
 <param-name>spring.profiles.active</param-name>
 <param-value>dev</param-value>
</context-param>

<context-param>
 <param-name>spring.config.path</param-name>    
 <param-value>classpath:app.config/</param-value>
</context-param>
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.