Purpose

The main goal of this booster is to send Tomcat’s access logging to stdout, which, in the context of an application running on OpenShift, means appending access logs to the OpenShift console, thus enabling administrators to use the central logging facility to monitor Tomcat’s activity.

Technical details

In order to redirect Spring Boot’s embedded Tomcat [4] logging to the OpenShift console, some work needs to be done.

  • You need to set server.use-forward-headers to true in your application.yml file:

    server:
      use-forward-headers: true
  • You will need to configure your embedded Tomcat to add the logback-access valve by defining a provider method:

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        // configure embedded Tomcat container
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();

        // Default configuration file: logback-access.xml
        LogbackValve logbackValve = new LogbackValve();

        logbackValve.setFilename("logback-access.xml");

        // Add logback valve to embedded Tomcat
        tomcat.addContextValves(logbackValve);

        // return the configured factory
        return tomcat;
    }
  • Optional: It is possible to register a ServletContextListener to log information when the servlet context is initialized / destroyed:

    @Bean
    protected ServletContextListener listener() {
        return new ServletContextListener() {

            @Override
            public void contextInitialized(ServletContextEvent sce) {
                logger.info("---> ServletContext initialized");
            }

            @Override
            public void contextDestroyed(ServletContextEvent sce) {
                logger.info("---> ServletContext destroyed");
            }

        };
    }
  • Optional: You can also specify logging levels (e.g. DEBUG) for different packages (e.g. org.apache.tomcat) by adding level.<package name> stanzas followed by the level name in application.yml. The name of the logging file can also be specified using the file property:

logging:
  level.org.apache.tomcat: "DEBUG"
  file: "application.log"

Deploying and interacting with the booster

  • Create a new project tomcat-logging (or whatever you want to call it):

$ oc new-project tomcat-logging
  • Build and deploy the Spring Boot application using the Fabric8 Maven Plugin [1]

$ mvn clean fabric8:deploy -Popenshift
  • Retrieve the route for the application:

$ oc get route spring-boot-tomcat-logging-booster -o jsonpath='http://{.spec.host}/greet/world'
  • Interact with the application by navigating to the route URL in your browser and changing the last path part (world in the example above) and try accessing different URLs.

  • Open your OpenShift console (if you use minishift [5]: minishift console should do it), navigate to the application deployment: <openshift host>/console/project/tomcat-logging/browse/dc/spring-boot-tomcat-logging-booster and click on View Log. You should see access entries similar to:

192.168.64.1 - - 22/Nov/2017:15:18:44 +0000 "GET /greet/world HTTP/1.1" 200 -
192.168.64.1 - - 22/Nov/2017:15:18:45 +0000 "GET /greet/ HTTP/1.1" 404 -

References