简体   繁体   中英

Spring-Boot Embedded Tomcat - generate localhost.log and cataline.out files

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 &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 !

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

  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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM