Spring Boot ELK
Spring Boot ELK
RAGHU
We can integrate this stack to any back-end application to achieve centralized logging. We can use
ELK to analyze the logs more efficiently and also using more complex search scenarios. It provides log
aggregation and efficient searching.
Page 1 of 5
Mr. RAGHU
Controller code:
package com.app.raghu.controller;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ElkDemoController {
@GetMapping(path = "/welcome")
public ResponseEntity<String> welcome() {
logger.debug("Welcome to ELK demo service");
return ResponseEntity.ok("Hello ELK Integration!!!");
}
@GetMapping(path = "/users/{name}")
public ResponseEntity<String> getUserByName(@PathVariable String name) {
if (name.equals("ADMIN")) {
logger.debug("Access by ADMIN triggered");
return ResponseEntity.ok("Access Granted to " + name);
} else {
logger.error("Access denied for: " + name);
return new ResponseEntity<>("Access Denied for " + name,
HttpStatus.BAD_REQUEST);
}
}
log4j2.xml: -
Page 2 of 5
Mr. RAGHU
pom.xml: -
<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-log4j2</artifactId>
</dependency>
Logging Configuration:
Create a log4j2.xml file inside resources folder. This includes log patterns, how log files should be
created, how console logs should be shown and many more information.
RollingFile is the type of log file. It means we have a constraint on the log file to rotate log file
creation. I have added size based constraint: when a log file is greater than 10MB, new file is created.
File name format will be elkdemo-<date>.log.
Execution order:
Page 3 of 5
Mr. RAGHU
http://localhost:8080/welcome
http://localhost:8080/users/sam
2. Start Elasticsearch
D:\ELK\elasticsearch-7.14.1\bin> elasticsearch.bat
3. Start kibana
elasticsearch.hosts: ["http://localhost:9200"]
D:\ELK\kibana-7.14.1-windows-x86_64\bin> kibana.bat
http://localhost:5601/
input {
file {
path => "D:/elk/logs/elkdemo.log"
start_position => "beginning"
}
}
output {
stdout {
codec => rubydebug
}
elasticsearch {
hosts => ["localhost:9200"]
index => "elkdemo"
}
}
Go to Management > Stack Management > Kibana > Index Patterns page from left side bar in Kibana
UI
Page 4 of 5
Mr. RAGHU
Go to “Discover” page from menu. In the left side, you will get a drop down to select the pattern.
Select elkdemo pattern from there
Page 5 of 5