Skip to content

Commit

Permalink
Merge pull request #1 from bhuwanupadhyay/develop
Browse files Browse the repository at this point in the history
Add logstash-logback-spring-boot-starter
  • Loading branch information
semusings authored Feb 17, 2023
2 parents bab8d24 + f452d36 commit df93c27
Show file tree
Hide file tree
Showing 20 changed files with 563 additions and 5 deletions.
Binary file removed Feature.png
Binary file not shown.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ This is collection for spring boot starters project for popular tools.
1. **[Drools Engine Starter](drools-engine-starter/README.md)** [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.bhuwanupadhyay/drools-engine-starter/badge.svg?style=flat)](https://github.com/bhuwanupadhyay/spring-boot-artifacts/tree/master/drools-engine-starter)
2. **[AWS DynamoDB Enhanced Client Repository Starter](aws-dynamodb-enhanced-client-repository-starter/README.md)** [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.bhuwanupadhyay/aws-dynamodb-enhanced-client-repository-starter/badge.svg?style=flat)](https://github.com/bhuwanupadhyay/spring-boot-artifacts/tree/master/aws-dynamodb-enhanced-client-repository-starter)
3. **[Transactional Outbox Pattern JPA Starter](transactional-outbox-pattern-jpa-starter)** [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.bhuwanupadhyay/transactional-outbox-pattern-jpa-starter/badge.svg?style=flat)](https://github.com/bhuwanupadhyay/spring-boot-artifacts/tree/master/transactional-outbox-pattern-jpa-starter)
4. **[Logstash Logback Spring Boot Starter](logstash-logback-spring-boot-starter)** [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.bhuwanupadhyay/logstash-logback-spring-boot-starter/badge.svg?style=flat)](https://github.com/bhuwanupadhyay/spring-boot-artifacts/tree/master/logstash-logback-spring-boot-starter)
1 change: 1 addition & 0 deletions artifacts.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
transactional-outbox-pattern-jpa-starter,1.0.0
drools-engine-starter,1.0.0
aws-dynamodb-enhanced-client-repository-starter,1.0.0
logstash-logback-spring-boot-starter,1.0.4
66 changes: 66 additions & 0 deletions logstash-logback-spring-boot-starter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Logstash Logback Spring Boot Starter

Library for Logstash logback for ELK with zero configuration

## Usage

### Add dependency in your spring boot project

- Maven

```xml
<dependency>
<groupId>io.github.bhuwanupadhyay</groupId>
<artifactId>logstash-logback-spring-boot-starter</artifactId>
<version>[version]</version>
</dependency>
```

- Gradle

```
implementation 'io.github.bhuwanupadhyay:logstash-logback-spring-boot-starter:[version]'
```

### Customize configurations

Autoconfiguration enable logstash logging automatically, and connected with logstash server url `localhost:5044`.

In case if you have to change logstash server url, override following properties:

```yaml
boot:
logstash:
destination: localhost:5044
```
By default custom fields are:
```json
{ "appname" : "<spring.application.name>" }
```

To disable logstash logging, override following properties:

```yaml
boot:
logstash:
enabled: false
```
All configuration properties:
```yaml
boot:
logstash:
destination: localhost:5044
enabled: true
key-store-location: keystore/trust.pk
key-store-password: 12345
custom-fields: |-
{"appname":"${spring.application.name}", "env": "${spring.profiles.active}"}
queue-size: 512
```
## Demo
[Greeting Service](demo)
78 changes: 78 additions & 0 deletions logstash-logback-spring-boot-starter/demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Getting Started

### ELK Stack

Before running your application you should start ELK Stack on your machine.

The best way to do that is through Docker containers.

- Create a Docker network to enable communication between containers via container name.

```shell script
docker network create elk
```

- Run elasticsearch docker container.

```shell script
docker run -d --name elasticsearch --net elk -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.9.2
```

- Create logstash configuration file `logstash.conf`.

```shell script
cat <<EOF> ~/logstash.conf
input {
tcp {
port => 5044
codec => json_lines
}
}
output {
elasticsearch {
hosts => ["http://elasticsearch:9200"]
index => "example-%{appname}-%{env}"
}
}
EOF
```
- Run logstash docker container.
```shell script
docker run -d --name logstash --net elk -p 5044:5044 -v ~/logstash.conf:/usr/share/logstash/pipeline/logstash.conf logstash:7.9.2
```
- Run kibana docker container.
```shell script
docker run -d --name kibana --net elk -e "ELASTICSEARCH_URL=http://elasticsearch:9200" -p 5601:5601 kibana:7.9.2
```
### Greeting Service
- Run the service and then check url [http://localhost:9200/_cat/indices](http://localhost:9200/_cat/indices) and look your index:
```
example-greeting-service-dev
```
- Open kibana dashboard [http://localhost:5601](http://localhost:5601/) and create an index:
```
example-greeting*
```
- Run discover and add filter message, then you will see logging:
```
10 hits
Documents
message
Started DemoApplication in 1.251 seconds (JVM running for 1.714)
Tomcat started on port(s): 8080 (http) with context path ''
Initializing Servlet 'dispatcherServlet'
Initializing Spring DispatcherServlet 'dispatcherServlet'
Completed initialization in 3 ms
Greeting Initialized at 2902820304765
Greeting Initialized at 2904734799104
Greeting Initialized at 2905670902001
Greeting Initialized at 2906859407908
Greeting Initialized at 2907493282685
```
Thank you for reading!
58 changes: 58 additions & 0 deletions logstash-logback-spring-boot-starter/demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.github.bhuwanupadhyay</groupId>
<artifactId>logstash-logback-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</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>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.demo;

public class Greeting {

private String message;

public Greeting() {
}

public Greeting(String message) {
this.message = message;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

private static final Logger LOG = LoggerFactory.getLogger(GreetingController.class);

@GetMapping("/greetings")
public ResponseEntity<Greeting> getGreetings() {
long currentTime = System.nanoTime();
LOG.info("Greeting Initialized at {}", currentTime);
if (currentTime % 2 == 0) {
return ResponseEntity.ok(new Greeting("Good Morning!"));
} else {
return ResponseEntity.ok(new Greeting("Good Afternoon!"));
}
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
spring:
application:
name: greeting-service
profiles:
active: dev
boot:
logstash:
destination: localhost:5044
custom-fields: |-
{"appname":"${spring.application.name}", "env": "${spring.profiles.active}"}
queue-size: 512
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {

@Test
void contextLoads() {
}

}
36 changes: 36 additions & 0 deletions logstash-logback-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.github.bhuwanupadhyay</groupId>
<artifactId>spring-boot-artifacts-parent</artifactId>
<version>0.0.2</version>
</parent>

<artifactId>logstash-logback-spring-boot-starter</artifactId>
<version>1.0.4</version>
<name>Logstash Logback Spring Boot Starter</name>
<description>Library for Logstash logback for ELK with zero configuration</description>

<properties>
<logstash-logback-encoder.version>6.4</logstash-logback-encoder.version>
</properties>

<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>${logstash-logback-encoder.version}</version>
</dependency>
</dependencies>


</project>
Loading

0 comments on commit df93c27

Please sign in to comment.