Configure the collection of log and trace messages generated by a Java application in SAP BTP, Cloud Foundry.
-
You have bound an application to the SAP Application Logging service. See: Logging and Tracing
-
You have installed and configured Maven.
-
For SAP Java Buildpack 1 (deprecated!): You use SAP JVM 8 or SapMachine 11 or 17, and your <JAVA_HOME> environment variable points to this location.
-
For SAP Java Buildpack 2: You use SapMachine 17, 21 or 25, and your <JAVA_HOME> environment variable points to this location.
-
You do not have any SLF4J and logback JAR files in the application.
The JARs for the SLF4J and logback are included as part of the Tomcat and TomEE runtimes provided by SAP Java Buildpack. Packing them in the application can cause problems during class loading.
The recommended framework for logging is Simple Logging Facade for Java (SLF4J). To use this framework, you can create an instance of the org.slf4j.Logger and org.slf4j.LoggerFactory classes. You can use SLF4J to configure your Java application to generate logs and traces, and if appropriate – set the logging and tracing levels.
It is the application's responsibility to ensure that the logback is configured in a secure way in the case when the application overrides the default logback configuration included in SAP Java Buildpack. See step 2 below.
-
Instruct Maven that the application should not package the SLF4J dependency, since it's already provided by the runtime.
To do that, go to the
pom.xmlfile and set scope provided for the following dependency:-
For SAP Java Buildpack 1:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.36</version> <scope>provided</scope> </dependency> -
For SAP Java Buildpack 2:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>2.0.13</version> <scope>provided</scope> </dependency>
-
-
(Optional) Configure the logback in case the application overrides the default logback configuration. To do that, you need to perform the following steps:
-
Under path
META-INF/sap_java_buildpack/resources/tomcat/conf, create a customlogback.xmlfile. For example:<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false" scan="false"> <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="${encoder}"> ${pattern} </encoder> </appender> <root level="INFO"> <appender-ref ref="STDOUT"/> </root> </configuration> -
Create a
resource_configuration.ymlfile or add the following entries to your existingresource_configuration.ymlfile (if using Tomcat):--- tomcat/conf/logback.xml: pattern: '' encoder: ''NOTE: You can also add default values for
patternandencoder. -
Add the following property in your
manifest.ymlfile. For example (if using Tomcat):--- applications: - name: <APP_NAME> ... env: JBP_CONFIG_RESOURCE_CONFIGURATION: "['tomcat/conf/logback.xml': {'encoder': 'com.sap.hcp.cf.logback.encoder.JsonEncoder', 'pattern': ''}]"
-
Related Information