Spring Boot Logbcak到文件在Tomcat上不起作用

时间:2019-05-28 18:58:55

标签: spring-boot tomcat logback

我正在tomcat上以Spring War的身份运行为War,并登录到控制台和文件。

只要我以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>

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>

答案 2 :(得分:0)

我可以通过将日志写入到应用程序没有写访问路径的其他文件夹中来解决此问题,但是我需要对springboot主类进行一些更改以加载基于应用程序props的配置文件,请在下面的类中找到。不知道其他人是否也必须这样做。

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

公共类应用程序扩展了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>