I have a spring-boot application running with an embedded tomcat. We are using logback and slf4j for logging.
I am trying to figure ways to generate catalina.out and localhost.log files through spring-boot application. It looks like catalina.out is generated by the start-up script that initiates the tomcat container in a stand-alone mode, catalina.out file is not generated within spring-boot application that is using embedded tomcat.
How about localhost.log file? Does the same apply for localhost.log file?
Also how can I change the log levels for embedded tomcat through logback/slf4j binding in my spring-boot application.
Any advice?
You have to configure the tomcat container in spring boot manually like this
You have to create the bean of EmbeddedServletContainerFactory and configure the log in tomcat container , below are the sample code (I am not tested it , but it may be run).
The tomcat now search the logback-access.xml file in classpath automatically the configure the logging
For Spring boot version < 2.0.0
@SpringBootApplication
public class ABCApplication {
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
LogbackValve logbackValve = new LogbackValve();
logbackValve.setFilename("logback-access.xml");
tomcat.addContextValves(logbackValve);
return tomcat;
}
public static void main(String[] args) {
SpringApplication.run(ABCApplication.class, args);
}
}
The EmbeddedServletContainerFactory is replace by TomcatServletWebServerFactory is spring boot version 2.0.0 , so use required factory to configure the tomacat.
Now you can provide your logback-access.xml like this
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%h %l %u %t "%r" %s %b</pattern>
</encoder>
</appender>
<appender-ref ref="STDOUT" />
</configuration>
You can add the appender to xml to log the tomcat logging in file.
You have to look about LogbackValve
You have to add the following dependency for the LogbackValve
<dependency>
<groupId>net.rakugakibox.spring.boot</groupId>
<artifactId>logback-access-spring-boot-starter</artifactId>
</dependency>
Hope it may be help jagamot !
To define Embedded Tomcat Log Path add these line in application.properties
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be absolute or relative to the Tomcat base dir.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in the log file name.
for configuring log levels for embedded tomcat through logback
use this link
https://dzone.com/articles/configuring-logback-with-spring-boot
logging.config=classpath:logback.xml logging.path=${your log path}
in your "application.properties".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.