snowdrop / tomcat-logging-example

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Purpose

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.

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:

link:https://raw.githubusercontent.com/snowdrop/tomcat-logging-example/master/src/main/java/me/snowdrop/logging/TomcatLoggingApplication.java[]
  • 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[]
  • 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 example

  • 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 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