SAP Java Buildpack integrates the Cloud Foundry Java Logging Support libraries, allowing applications to produce logs in JSON format that can be parsed by the SAP Application Logging service for SAP BTP.
-
Bind your application to an SAP Application Logging service instance. This way, you can produce application logs and forward them to a central application logging stack.
-
If you want to generate request metrics for applications based on Tomcat or TomEE, add com.sap.hcp.cf.logging.servlet.filter.RequestLoggingFilter to the
web.xmlfile.<filter-name>request-logging</filter-name> <filter-class>com.sap.hcp.cf.logging.servlet.filter.RequestLoggingFilter</filter-class> </filter> <filter-mapping> <filter-name>request-logging</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>The default log level of com.sap.hcp.cf.logging.servlet.filter.RequestLoggingFilter is set to INFO.
-
Check the logs of the application.
If the binding of your application to the SAP Application Logging service is successful, the application logs can be found on
https://logs.cf.<SAP_BTP_region>. To request the logs of your application, run:cf logs <app_name> --recent -
(Optional) Change the log level of a logging location. You can do this in two ways - before and during application deploy.
-
Change the log level before deploy, by using a file. You need to restart your application after that. Use cases:
-
Standard applications based on Tomcat or TomEE – configure the SET_LOGGING_LEVEL environment variable in the
manifest.ymlfile. For example:env: SET_LOGGING_LEVEL: '{com.sap.sample.java.LogInfo: INFO, com.sap.sample.java.LogWarn: WARN}' -
Standard applications based on Java Main – create a
logback.xmlfile in directorysrc/main/resources. For example:<configuration> <!-- Console appender --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- Set root log level --> <root level="DEBUG"> <appender-ref ref="STDOUT" /> </root> <!-- Optional: package-specific logging --> <logger name="org.example.Main" level="ERROR"/> </configuration> -
Spring Boot applications based on Java Main. Their default log configuration provides messages of levels ERROR, WARN, and INFO. To enable other log levels, for example DEBUG, create a file
application.propertiesin directory/src/main/resources/and add the following line:logging.level.root=DEBUGTo enable DEBUG logging for a particular class only, you can do so by specifying the fully qualified class name:
logging.level.com.mypackage.MySpecificClass=DEBUGIf you use a Spring Boot generator (for example Spring Initializr), the file directory
/src/main/resources/application.propertiesis automatically created for you.
-
-
Change the log level for any Java application in real time (during deploy), by using command:
cf set-logging-level <app-name> <java-class> <level>The changes will be applied immediately – no need to restart your application. For example:
cf set-logging-level myapp123 com.sap.sample.java.LogWarn WARN
-
-
(Optional) Check the audit logs.
Changing the logging level of your application (by using the
set-logging-levelcommand) can be tracked in the audit log server. To enable audit logging, bind your Java application to an Audit Log service instance, with plan oauth2.NOTE: For SpringBoot applications based on Java Main, you also need to add the following dependencies in the
pom.xmlfile:<dependency> <groupId>com.sap.cp.auditlog</groupId> <artifactId>audit-java-client-api</artifactId> <version>${com.sap.cp.auditlog.audit-java-client.version}</version> </dependency> <dependency> <groupId>com.sap.cp.auditlog</groupId> <artifactId>audit-java-client-impl</artifactId> <version>${com.sap.cp.auditlog.audit-java-client.version}</version> </dependency> <dependency> <groupId>com.sap.cloud.sjb</groupId> <artifactId>xs-env</artifactId> <version>${com.sap.cloud.sjb.xs-env.version}</version> <scope>runtime</scope> </dependency>
Related Information
Configure a Java Application for Logs and Traces