-
Notifications
You must be signed in to change notification settings - Fork 54k
Adding "Streaming Real-time log to Splunk from Spring boot" | BAEL-8434 #17648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
81c6475
Adding SplunkDemo with log4j2
programenth 80bffb5
Merge branch 'eugenp:master' into spring-web-test1
programenth 53ab8cb
Delete logging-modules/splunk-with-log4j2 directory
programenth 89c9057
adding Splunk demo with Spring boot - Maven
programenth aadb664
Delete logging-modules/splunk-with-log4j2 directory
programenth 0881f70
Adding Splunk with log4j2 code
programenth 30e364c
Update pom.xml
programenth a8f6033
Merge branch 'master' into spring-web-test1
programenth 794433c
Delete logging-modules/splunk-with-log4j2 directory
programenth 51fe70b
Adding Splunk with log4j2 updated code.
programenth 4221342
Update pom.xml
programenth 9b13da1
Update pom.xml
programenth c478f44
Merge branch 'eugenp:master' into spring-web-test1
programenth 10836fd
Update pom.xml
programenth 2421dfe
updating imports order
programenth 6a46b66
Merge branch 'eugenp:master' into spring-web-test1
programenth 27a7a19
Delete logging-modules/splunk-with-log4j2 directory
programenth aefdc55
Add files via upload
programenth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| <?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> | ||
| <parent> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-parent</artifactId> | ||
| <version>3.3.4</version> | ||
| <relativePath/> | ||
| </parent> | ||
| <groupId>com.splunk</groupId> | ||
| <artifactId>log4j</artifactId> | ||
| <version>0.0.1-SNAPSHOT</version> | ||
| <name>log4j</name> | ||
| <description>Demo project for Splunk with Spring Boot</description> | ||
| <properties> | ||
| <java.version>17</java.version> | ||
| <splunk-logging.version>1.8.0</splunk-logging.version> | ||
| </properties> | ||
|
|
||
| <repositories> | ||
| <repository> | ||
| <id>splunk-artifactory</id> | ||
| <name>Splunk Releases</name> | ||
| <url>https://splunk.jfrog.io/splunk/ext-releases-local</url> | ||
| </repository> | ||
| <repository> | ||
| <id>central</id> | ||
| <name>Central Repository</name> | ||
| <url>https://repo1.maven.org/maven2/</url> | ||
| </repository> | ||
| </repositories> | ||
|
|
||
| <dependencies> | ||
| <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-starter-test</artifactId> | ||
| <scope>test</scope> | ||
| <exclusions> | ||
| <exclusion> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-logging</artifactId> | ||
| </exclusion> | ||
| </exclusions> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.splunk.logging</groupId> | ||
| <artifactId>splunk-library-javalogging</artifactId> | ||
| <version>${splunk-logging.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-log4j2</artifactId> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-maven-plugin</artifactId> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> | ||
13 changes: 13 additions & 0 deletions
13
logging-modules/splunk-with-log4j2/src/main/java/com/splunk/log4j/Log4jApplication.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.splunk.log4j; | ||
|
|
||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
|
||
| @SpringBootApplication | ||
| public class Log4jApplication { | ||
|
|
||
| public static void main(String[] args) { | ||
| SpringApplication.run(Log4jApplication.class, args); | ||
| } | ||
|
|
||
| } |
37 changes: 37 additions & 0 deletions
37
...dules/splunk-with-log4j2/src/main/java/com/splunk/log4j/controller/StudentController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.splunk.log4j.controller; | ||
|
|
||
| import java.util.List; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import com.splunk.log4j.dto.Student; | ||
| import com.splunk.log4j.service.StudentService; | ||
|
|
||
| @RestController | ||
| @RequestMapping("students") | ||
| public class StudentController { | ||
|
|
||
| private final StudentService studentService; | ||
|
|
||
| public StudentController(StudentService studentService) { | ||
| this.studentService = studentService; | ||
| } | ||
|
|
||
| @PostMapping | ||
| public Student addStudent(@RequestBody Student student) { | ||
| return studentService.addStudent(student); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<Student> getStudents() { | ||
| return studentService.getStudents(); | ||
| } | ||
|
|
||
| @GetMapping("{rollNumber}") | ||
| public Student getStudent(@PathVariable("rollNumber") int rollNumber) { | ||
| return studentService.getStudent(rollNumber); | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
logging-modules/splunk-with-log4j2/src/main/java/com/splunk/log4j/dto/Student.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package com.splunk.log4j.dto; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Student { | ||
|
|
||
| private String name; | ||
|
|
||
| private int rollNumber; | ||
|
|
||
| public Student() { | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public int getRollNumber() { | ||
| return rollNumber; | ||
| } | ||
|
|
||
| public void setRollNumber(int rollNumber) { | ||
| this.rollNumber = rollNumber; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Student{" + "name='" + name + '\'' + ", rollNumber=" + rollNumber + '}'; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
|
|
||
| Student student = (Student) o; | ||
|
|
||
| if (rollNumber != student.rollNumber) { | ||
| return false; | ||
| } | ||
| return Objects.equals(name, student.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result = name != null ? name.hashCode() : 0; | ||
| result = 31 * result + rollNumber; | ||
| return result; | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
...ing-modules/splunk-with-log4j2/src/main/java/com/splunk/log4j/service/StudentService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.splunk.log4j.service; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.springframework.stereotype.Service; | ||
| import com.splunk.log4j.dto.Student; | ||
|
|
||
| @Service | ||
| public class StudentService { | ||
|
|
||
| private static final Logger logger = LogManager.getLogger(StudentService.class); | ||
|
|
||
| private final List<Student> students = new ArrayList<>(); | ||
|
|
||
| public Student addStudent(Student student) { | ||
| logger.info("addStudent: adding Student"); | ||
| logger.info("addStudent: Request: {}", student); | ||
| students.add(student); | ||
| logger.info("addStudent: added Student"); | ||
| logger.info("addStudent: Response: {}", student); | ||
| return student; | ||
| } | ||
|
|
||
| public List<Student> getStudents() { | ||
| logger.info("getStudents: getting Students"); | ||
| List<Student> studentsList = students; | ||
| logger.info("getStudents: got Students"); | ||
| logger.info("getStudents: Response: {}", studentsList); | ||
| return studentsList; | ||
| } | ||
|
|
||
| public Student getStudent(int rollNumber) { | ||
| logger.info("getStudent: getting Student"); | ||
| logger.info("getStudent: Request: {}", rollNumber); | ||
| Student student = students.stream() | ||
| .filter(stu -> stu.getRollNumber() == rollNumber) | ||
| .findAny() | ||
| .orElseThrow(() -> new RuntimeException("Student not found")); | ||
| logger.info("getStudent: got Student"); | ||
| logger.info("getStudent: Response: {}", student); | ||
| return student; | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
logging-modules/splunk-with-log4j2/src/main/resources/application.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| spring.application.name=log4j |
29 changes: 29 additions & 0 deletions
29
logging-modules/splunk-with-log4j2/src/main/resources/log4j2-spring.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <Configuration> | ||
| <Appenders> | ||
| <Console name="console" target="SYSTEM_OUT"> | ||
| <PatternLayout | ||
| pattern="%style{%d{ISO8601}} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{10}}{bright,yellow}: %msg%n%throwable"/> | ||
| </Console> | ||
| <SplunkHttp | ||
| name="splunkhttp" | ||
| url="http://localhost:8088" | ||
| token="11806291-7e0e-422a-a083-abfdd4b2eb74" | ||
| host="localhost" | ||
| index="student_api_dev" | ||
| type="raw" | ||
| source="student-http-events" | ||
| sourcetype="log4j" | ||
| messageFormat="text" | ||
| disableCertificateValidation="true"> | ||
| <PatternLayout pattern="%m"/> | ||
| </SplunkHttp> | ||
| </Appenders> | ||
|
|
||
| <Loggers> | ||
| <Root level="info"> | ||
| <AppenderRef ref="console"/> | ||
| <AppenderRef ref="splunkhttp"/> | ||
| </Root> | ||
| </Loggers> | ||
| </Configuration> |
75 changes: 75 additions & 0 deletions
75
...lunk-with-log4j2/src/test/java/com/splunk/log4j/controller/StudentControllerUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package com.splunk.log4j.controller; | ||
|
|
||
| import static org.mockito.Mockito.when; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
| import org.springframework.boot.test.mock.mockito.MockBean; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.splunk.log4j.dto.Student; | ||
| import com.splunk.log4j.service.StudentService; | ||
|
|
||
| @WebMvcTest(StudentController.class) | ||
| class StudentControllerUnitTest { | ||
|
|
||
| @Autowired | ||
| private MockMvc mockMvc; | ||
|
|
||
| @MockBean | ||
| private StudentService studentService; | ||
|
|
||
| @Autowired | ||
| private ObjectMapper objectMapper; | ||
|
|
||
| @Test | ||
| void whenAddStudentCalled_thenReturnSuccessAndAddedStudent() throws Exception { | ||
| Student student = new Student(); | ||
| student.setName("Ram"); | ||
| student.setRollNumber(3); | ||
|
|
||
| when(studentService.addStudent(student)).thenReturn(student); | ||
|
|
||
| mockMvc.perform(post("/students").contentType(MediaType.APPLICATION_JSON) | ||
| .content(objectMapper.writeValueAsString(student))) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(content().json(objectMapper.writeValueAsString(student))) | ||
| .andReturn(); | ||
| } | ||
|
|
||
| @Test | ||
| void whenGetStudentCalled_thenReturnStudentByIndex() throws Exception { | ||
| Student student = new Student(); | ||
| student.setName("Ram"); | ||
| student.setRollNumber(4); | ||
|
|
||
| when(studentService.getStudent(0)).thenReturn(student); | ||
|
|
||
| mockMvc.perform(get("/students/0")) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(content().json(objectMapper.writeValueAsString(student))); | ||
| } | ||
|
|
||
| @Test | ||
| void whenGetStudentsCalled_thenReturnListOfStudent() throws Exception { | ||
| Student student = new Student(); | ||
| student.setName("Ram"); | ||
| student.setRollNumber(4); | ||
|
|
||
| Student student2 = new Student(); | ||
| student.setName("Sham"); | ||
| student.setRollNumber(3); | ||
|
|
||
| when(studentService.getStudents()).thenReturn(List.of(student, student2)); | ||
|
|
||
| mockMvc.perform(get("/students")) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(content().json(objectMapper.writeValueAsString(List.of(student, student2)))); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @JavierPeris ,
I was getting conflicts in the build due to other loggers because there are other submodules in which they have other loggers already (other than log4j2).
Error for reference:
SLF4J(W): Class path contains multiple SLF4J providers. SLF4J(W): Found provider [org.apache.logging.slf4j.SLF4JServiceProvider@64711bf2] SLF4J(W): Found provider [ch.qos.logback.classic.spi.LogbackServiceProvider@169da7f2] SLF4J(W): See https://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J(I): Actual provider is of type [org.apache.logging.slf4j.SLF4JServiceProvider@64711bf2]