This is a logging service for a Spring Boot application. It uses Apache Kafka for message consumption and MongoDB for data storage. The service is designed to consume error logs from Kafka, store them in MongoDB, and provide an API for retrieving the logs.
The project is structured into four main packages:
-
com.serhatacarr.loggingservice.controller
: This package contains theErrorLogController
class which exposes a REST API for retrieving the error logs. -
com.serhatacarr.loggingservice.repository
: This package contains theErrorLogRepository
interface which extendsMongoRepository
. This interface is used for CRUD operations on theErrorLog
documents in MongoDB. -
com.serhatacarr.loggingservice.service
: This package contains theKafkaConsumerService
class which consumes messages from a Kafka topic and saves them asErrorLog
documents in MongoDB. -
com.serhatacarr.loggingservice.entity
: This package contains theErrorLog
class which represents the error log documents in MongoDB.
The KafkaConsumerService
class is annotated with @Service
and @Slf4j
(for logging). It has a method consume
annotated with @KafkaListener
, which consumes messages from the Kafka topic "errorLog". The consumed message is saved as an ErrorLog
document in MongoDB.
The ErrorLogRepository
interface extends MongoRepository<ErrorLog, String>
, which provides methods for CRUD operations on ErrorLog
documents in MongoDB.
The ErrorLogController
class is annotated with @RestController
and @RequestMapping("/api/v1/error-logs")
. It has a method findAll
annotated with @GetMapping
, which retrieves all ErrorLog
documents from MongoDB and returns them as a list.
The ErrorLog
class is annotated with @Document(collection = "errorlogdoc")
, @Getter
, @Setter
, @AllArgsConstructor
, @NoArgsConstructor
, and @Builder
. It represents the error log documents in MongoDB.
To use this service, you need to have a running instance of Apache Kafka and MongoDB. You also need to configure the Kafka topic and MongoDB connection details in the application's properties file.
You can run the application using the following command:
mvn spring-boot:run
Once the application is running, you can access the API at http://localhost:8080/api/v1/error-logs
.