Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,11 @@ description about how to apply this feature can be found
Stacktraces can be logged within one log message. Further details can be found
[here](https://github.com/SAP/cf-java-logging-support/wiki/Logging-Stack-Traces).

## Sample Application
## Sample Applications

In order to illustrate how the different features are used, this repository includes a simple application in the [./sample folder](./sample).
In order to illustrate how the different features are used, this repository includes two sample applications:
* a Jersey implementation in the [./sample folder](./sample)
* a Spring Boot implementaiton in the [./sample-spring-boot folder](./sample-spring-boot)

## Documentation

Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
<module>cf-java-logging-support-jersey</module>
<module>cf-java-monitoring-custom-metrics-clients</module>
<module>sample</module>
<module>sample-spring-boot</module>
</modules>

<dependencies>
Expand Down
241 changes: 241 additions & 0 deletions sample-spring-boot/README.md

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions sample-spring-boot/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
applications:
#
# You may want/need to change these to avoid naming conflicts
#
- name: logging-sample-app
instances: 1
path: target/sample-app-spring-boot-3.2.1.jar
env:
# Set LOG_*: true to activate logging of respective field
LOG_SENSITIVE_CONNECTION_DATA: false
LOG_REMOTE_USER: false
LOG_REFERER: false
207 changes: 207 additions & 0 deletions sample-spring-boot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>sample-app-spring-boot</artifactId>
<name>sample-app-spring-boot</name>
<description>Logging Sample App for Spring Boot</description>
<parent>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-parent</artifactId>
<version>3.3.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<spring.boot.version>2.3.4.RELEASE</spring.boot.version>
<keytool.plugin.version>1.5</keytool.plugin.version>
<keystore.token.store_password>0bzhBRNUXBR5
</keystore.token.store_password>
<keystore.token.key_password>0bzhBRNUXBR5
</keystore.token.key_password>
<keystore.token.key_alias>jwt-token</keystore.token.key_alias>
<keystore.token.dname>CN=cf-java-logging-support, OU=None, O=SAP,
L=Unknown, ST=Unknown, C=Unknown
</keystore.token.dname>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

<!-- We're using the Servlet Filter instrumentation -->
<dependency>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-servlet</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>${basedir}/target/generated-resources/keystore</directory>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>initialze-keystore</id>
<activation>
<file>
<missing>${basedir}/target/generated-resources/keystore/token_keystore.jks</missing>
</file>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>keytool-maven-plugin</artifactId>
<version>${keytool.plugin.version}</version>
<executions>
<execution>
<id>create-key-pair</id>
<goals>
<goal>generateKeyPair</goal>
</goals>
<phase>generate-resources</phase>
</execution>
</executions>
<configuration>
<keystore>${basedir}/target/generated-resources/keystore/token_keystore.jks</keystore>
<storepass>${keystore.token.store_password}</storepass>
<keypass>${keystore.token.key_password}</keypass>
<alias>${keystore.token.key_alias}</alias>
<dname>${keystore.token.dname}</dname>
<sigalg>SHA256withRSA</sigalg>
<ext></ext>
<validity>100</validity>
<keyalg>RSA</keyalg>
<keysize>2048</keysize>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>logback</id>
<activation>
<property>
<name>!log4j2</name>
</property>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-logback</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>log4j2</id>
<activation>
<property>
<name>log4j2</name>
</property>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-log4j2</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.sap.hcp.cf.logging.sample.springboot;

import java.time.Clock;

import javax.servlet.DispatcherType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.sap.hcp.cf.logging.sample.springboot.keystore.KeyStoreDynLogConfiguration;
import com.sap.hcp.cf.logging.servlet.dynlog.DynamicLogLevelConfiguration;
import com.sap.hcp.cf.logging.servlet.filter.AddHttpHeadersToLogContextFilter;
import com.sap.hcp.cf.logging.servlet.filter.AddVcapEnvironmentToLogContextFilter;
import com.sap.hcp.cf.logging.servlet.filter.CompositeFilter;
import com.sap.hcp.cf.logging.servlet.filter.CorrelationIdFilter;
import com.sap.hcp.cf.logging.servlet.filter.DynamicLogLevelFilter;
import com.sap.hcp.cf.logging.servlet.filter.GenerateRequestLogFilter;
import com.sap.hcp.cf.logging.servlet.filter.RequestLoggingFilter;

@SpringBootApplication
@EnableWebMvc
public class SampleAppSpringBootApplication {

public static void main(String[] args) {
SpringApplication.run(SampleAppSpringBootApplication.class, args);
}

/**
* Registers a customized {@link RequestLoggingFilter} with the servlet. We
* inject our own dynamic logging configuration, that contains the public RSA
* key from our keystore.
*
* @param dynLogConfig autowired with {@link KeyStoreDynLogConfiguration}
* @return a registration of the {@link RequestLoggingFilter}
*/
@Bean
public FilterRegistrationBean<MyLoggingFilter> loggingFilter(@Autowired DynamicLogLevelConfiguration dynLogConfig) {
FilterRegistrationBean<MyLoggingFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new MyLoggingFilter(dynLogConfig));
registrationBean.setName("request-logging");
registrationBean.addUrlPatterns("/*");
registrationBean.setDispatcherTypes(DispatcherType.REQUEST);
return registrationBean;
}

/**
* Provides a global {@link Clock} instance. Useful for testing.
*
* @return the global clock
*/
@Bean
public Clock clock() {
return Clock.systemUTC();
}

private class MyLoggingFilter extends CompositeFilter {

private MyLoggingFilter(DynamicLogLevelConfiguration dynLogConfig) {
super(new AddVcapEnvironmentToLogContextFilter(), new AddHttpHeadersToLogContextFilter(),
new CorrelationIdFilter(), new DynamicLogLevelFilter(() -> dynLogConfig),
new GenerateRequestLogFilter());
}
}

/**
* Provides a {@link BCryptPasswordEncoder} for Basic-Auth.
*
* @return the encoder
*/
@Bean
public BCryptPasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.sap.hcp.cf.logging.sample.springboot.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "auth.basic")
public class BasicAuthenticationConfiguration {

private String username;
private String password;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}
Loading