The main goal of this example 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.
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
totrue
in yourapplication.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:
link:https://raw.githubusercontent.com/snowdrop/tomcat-logging-example/master/src/main/java/me/snowdrop/logging/TomcatLoggingApplication.java[role=include]
-
Optional: It is possible to register a
ServletContextListener
to log information when the servlet context is initialized / destroyed:
link:https://raw.githubusercontent.com/snowdrop/tomcat-logging-example/master/src/main/java/me/snowdrop/logging/TomcatLoggingApplication.java[role=include]
-
Optional: You can also specify logging levels (e.g.
DEBUG
) for different packages (e.g.org.apache.tomcat
) by addinglevel.<package name>
stanzas followed by the level name inapplication.yml
. The name of the logging file can also be specified using thefile
property:
logging:
level.org.apache.tomcat: "DEBUG"
file: "application.log"
-
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 tomcat-logging-example -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/tomcat-logging-example
and click onView 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 -
-
[1] Maven Fabric8 plugin reference: https://maven.fabric8.io
-
[2] For more details on
logback-access
, please look at: https://logback.qos.ch/access.html -
[3] An interesting third-party project with deeper
logback-access
integration with Spring Boot can be found at: https://github.com/akihyro/logback-access-spring-boot-starter -
[4] More details on embedded servlet containers in Spring Boot: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-servlet-containers.html
-
[5] Minishift: https://docs.openshift.org/latest/minishift/index.html