Spring boot embedded tomcat logs
Asked Answered
O

5

18

i'm using spring boot embedded tomcat with spring boot 1.5.9 , im also using Log4j2.

recently i exerience problems during load, so i want to understand better the tomcat logs [Not the access Logs] , i tried (in application.properties) :

logging.level.org.apache.tomcat: INFO
logging.level.org.apache.catalina: INFO

but none of the above worked. is there any other way to achieve it ?

Occupational answered 18/1, 2018 at 2:18 Comment(0)
O
11

Found it !! You are now able to see the internal Logs of Embedded Tomcat in your App's Log4j log file with 3 easy steps:

1] add to your pom:

 <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-jul</artifactId>
     </dependency>

2] add to your running arg a new JVM param , e.g:

java -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager -jar target/demo-0.0.1-SNAPSHOT.jar

3] add to your application.properties:

logging.level.org.apache=DEBUG

Enjoy Life ! :)

Explaination: the problem is because Log4j log levels is not propagated into JUL (which is the actual Logging way Embedded tomcat use) so the above achieves this connection with JUL and Log4j log levels.

Reference: After reading the Spring boot 1.5.10 release notes (which is not required for the solution) i saw the new documentation that shed light how to achive it and explaination about it:

https://github.com/spring-projects/spring-boot/issues/2923#issuecomment-358451260

Occupational answered 3/2, 2018 at 20:25 Comment(3)
what if you use default logbackPit
@Kalpesh Soni not possible due the fact i needed to do it in a very large scale code base that is already working with log4j2 ,changing to logback is not an option...Occupational
In newer versions of Spring Boot (e.g. 2.1.9) you don't need to add the dependency on org.apache.logging.log4j:log4j-jul as it will get added by org.springframework.boot:spring-boot-starter-log4j2.Variometer
C
1

I struggled a lot,and didnt find anything of my help.Utlimately I had build "WAR" out of my spring boot application.Deploy it to tomcat instance and followed below steps,which redirected all the internal tomcat logs(JULI) logs to my application log file.

  1. Delete existing JULI library (CATALINA_HOME/bin/tomcat-juli.jar file) and the existing Tomcat Java Logging configuration file (CATALINA_HOME/conf/logging.properties).

  2. Download JULI Log4j Tomcat library (tomcat-juli.jar) from the Tomcat downloads’ Extras section (http://tomcat.apache.org/download-70.cgi). Place the downloaded file to CATALINA_HOME/bin directory.

  3. Download Tomcat JULI adapters library (tomcat-juli-adapters.jar) from the Tomcat downloads’ Extras section. Place this file in the CATALINA_HOME/lib directory.

  4. Download Log4j (version 1.2 or later), and place the downloaded library file to CATALINA_HOME/lib directory.

  5. Create the Log4j configuration file at the following location: CATALINA_HOME/lib/log4j.properties. Check below log4j configuration matching the default Java Logging configuration.

  6. Restart Tomcat.

Log4j configuration File Matching the Default Tomcat Logging Settings:

log4j.rootLogger=INFO, CATALINA
//Define all the appenders log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.
log4j.appender.CATALINA.Append=true log4j.appender.CATALINA.Encoding=UTF-8

//Roll-over the log once per day
log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout

log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.LOCALHOST=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.File=${catalina.base}/logs/localhost.
log4j.appender.LOCALHOST.Append=true log4j.appender.LOCALHOST.Encoding=UTF-8
log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.File=${catalina.base}/logs/manager.
log4j.appender.MANAGER.Append=true log4j.appender.MANAGER.Encoding=UTF-8
log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.HOST-MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.File=${catalina.base}/logs/host-manager.
log4j.appender.HOST-MANAGER.Append=true log4j.appender.HOST-MANAGER.Encoding=UTF-8
log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Encoding=UTF-8
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

//Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].
[localhost]=INFO,
 LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].
[localhost].[/manager]=INFO,MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].
[localhost].[/host-manager]=
INFO, HOST-
MANAGER

You can also check a adapter avaiable on GIT @ link

In your spring boot application,you can make changes like adding and removing jars,folder from embedded Tomcat server Or even adding custom config files to it using TomcatEmbeddedServletContainerFactory.class ,of spring boot.

Cambrian answered 22/1, 2018 at 11:7 Comment(1)
Thanks for this detailed information ! but i do need to stay with embedded tomcat , do you have a way to get Tomcat logs into App's Log when working with Embedded tomcat ?Occupational
I
1

For slf4j and Spring Boot 2 hide exceptions from Tomcat and handle them by yourself:

  • Add to pom:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    
  • Add to config:

    @PostConstruct
    void postConstruct() {
        SLF4JBridgeHandler.install();
    }
    
  • Add to application.yaml

    logging:
        level:
          org.apache.catalina: off
    
  • Handle exception in ErrorController

    @Controller
    @Slf4j
    public class ErrorController implements 
                  org.springframework.boot.web.servlet.error.ErrorController {
        private static final String ERROR_PATH = "/error";
    
       @Autowired
       private ErrorAttributes errorAttributes;
    
       @Override
       public String getErrorPath() {
          return ERROR_PATH;
       }
    
       @RequestMapping(ERROR_PATH)
       public ModelAndView error(HttpServletRequest request) {
           return processException(errorAttributes.getError(new ServletWebRequest(request)));
       }
    }
    
Imbalance answered 16/7, 2019 at 11:57 Comment(0)
C
-1

Default configurations are provided for Java Util Logging, Log4J, Log4J2 and Logback. In each case loggers are pre-configured to use console output with optional file output also available

refer this link : https://mcmap.net/q/742080/-spring-boot-default-log-location/31939886

The embedded tomcat in spring boot internally echoes logs to console. The default log configuration will echo messages to the console as they are written. So until you explicitly specify a file as you described, it stays in the Console.

From the spring boot logging doc.

You can custmize the logging as per your need.

Cockrell answered 22/1, 2018 at 9:51 Comment(2)
Hi Ramesh as declared in the question i use Log4j2 , so my app 's logs is not written to console.Occupational
Default configurations are provided for Java Util Logging, Log4J, Log4J2 and Logback. In each case loggers are pre-configured to use console output with optional file output also availableCockrell
L
-1

A log file, generated by org.apache.catalina.valves.AccessLogValve, usually named something like localhost_access_log can be configured like this:

@Configuration
public class EmbeddedTomcatConfig {
    @Bean
    public TomcatEmbeddedServletContainerFactory containerFactory() {
        TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory = new TomcatEmbeddedServletContainerFactory();
        AccessLogValve accessLogValve = new AccessLogValve();
        // set desired properties like
        accessLogValve.setDirectory(...);
        tomcatEmbeddedServletContainerFactory.addEngineValves(accessLogValve);
        return tomcatEmbeddedServletContainerFactory;
    }
}

Or, much better with Spring Boot 2:

   @Bean
   public WebServerFactoryCustomizer<TomcatServletWebServerFactory> customizer() {
        return container -> {
            AccessLogValve accessLogValve = new AccessLogValve();
            // set desired properties like
            accessLogValve.setDirectory("...");
            container.addEngineValves(accessLogValve);
        };
    }
Loralorain answered 13/9, 2019 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.