4

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?

jagamot
  • 5,348
  • 18
  • 59
  • 96

3 Answers3

0

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 &quot;%r&quot; %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 !

Ashwani Tiwari
  • 1,497
  • 18
  • 28
0

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

MyTwoCents
  • 7,284
  • 3
  • 24
  • 52
0
  1. 1.add logging.config=classpath:logback.xml logging.path=${your log path}in your "application.properties".
  2. create a "logback.xml" config file in your classpath(please search the contents of the configuration file by yourself).
  3. You can already create a log file by the above two steps,If you launch your application via the "java -jar" command, you may also need the "-Djava.io.tmpdir=${your log path}"(same as "logging.path" config) parameter to specify the log storage path.
Shuai Lee
  • 3
  • 1