diff --git a/.env.example b/.env.example index e5111d2..37c170c 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,6 @@ +SPRING_ACTIVE_PROFILE=container +SPRING_LOG_LEVEL=INFO + DB_URL=localhost:5432 DB_NAME=demo DB_SCHEMA=public diff --git a/build.gradle.kts b/build.gradle.kts index b3e833f..ae4c913 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -27,6 +27,7 @@ dependencies { implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.15.4") implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.4") implementation("io.github.optimumcode:json-schema-validator:0.2.3") + implementation("net.logstash.logback:logstash-logback-encoder:8.0") implementation("org.flywaydb:flyway-core:9.22.3") implementation("org.jetbrains.kotlin:kotlin-reflect:2.0.20") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3") diff --git a/docker-compose.yml b/docker-compose.yml index 89c0dea..24d44d4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,8 @@ services: dockerfile: Dockerfile context: . environment: + SPRING_LOG_LEVEL: $SPRING_LOG_LEVEL + SPRING_ACTIVE_PROFILE: $SPRING_ACTIVE_PROFILE DB_URL: $DB_URL DB_NAME: $DB_NAME DB_SCHEMA: $DB_SCHEMA diff --git a/src/main/kotlin/com/example/demo/services/database/product/ProductServiceImpl.kt b/src/main/kotlin/com/example/demo/services/database/product/ProductServiceImpl.kt index 853ede2..0d49377 100644 --- a/src/main/kotlin/com/example/demo/services/database/product/ProductServiceImpl.kt +++ b/src/main/kotlin/com/example/demo/services/database/product/ProductServiceImpl.kt @@ -5,6 +5,8 @@ import com.example.demo.providers.ProductRepository import com.example.demo.services.database.exceptions.AlreadyDeletedException import com.example.demo.services.database.product.exceptions.ProductNotFoundException import com.example.demo.services.kafka.Producer +import com.example.demo.utils.LoggerDelegate +import net.logstash.logback.marker.Markers import org.springframework.data.domain.Page import org.springframework.data.domain.Pageable import java.time.OffsetDateTime @@ -15,7 +17,15 @@ class ProductServiceImpl( private val productRepository: ProductRepository, private val producer: Producer, ): ProductService { + private val logger by LoggerDelegate() + override fun findByGuid(guid: UUID): Product? = productRepository.findByGuid(guid) + .also { + logger.debug( + Markers.appendEntries(mapOf("guid" to guid, "idResult" to it?.id)), + "find product by guid", + ) + } override fun findAll(pageable: Pageable): Page = productRepository.findAll(pageable) diff --git a/src/main/kotlin/com/example/demo/utils/Logger.kt b/src/main/kotlin/com/example/demo/utils/Logger.kt new file mode 100644 index 0000000..8c0a0ac --- /dev/null +++ b/src/main/kotlin/com/example/demo/utils/Logger.kt @@ -0,0 +1,24 @@ +package com.example.demo.utils + +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import kotlin.properties.ReadOnlyProperty +import kotlin.reflect.KProperty +import kotlin.reflect.full.companionObject + +/** + * usage: `private val logger by LoggerDelegate()` + */ +class LoggerDelegate : ReadOnlyProperty { + override fun getValue(thisRef: R, property: KProperty<*>) + = getLogger(getClassForLogging(thisRef.javaClass)) + + private fun getClassForLogging(javaClass: Class): Class<*> { + return javaClass.enclosingClass?.takeIf { + it.kotlin.companionObject?.java == javaClass + } ?: javaClass + } +} + + +fun getLogger(forClass: Class<*>): Logger = LoggerFactory.getLogger(forClass) \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 0950bbd..32bce49 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -14,6 +14,12 @@ spring: locations: classpath:db/migration/structure, classpath:db/migration/data # the location where flyway should look for migration scripts validate-on-migrate: true default-schema: ${DB_SCHEMA:public} + profiles: + active: ${SPRING_ACTIVE_PROFILE:default} + +logging: + level: + root: ${SPRING_LOG_LEVEL:INFO} kafka: bootstrap-servers: ${KAFKA_SERVERS:localhost:9095} diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..7ceef5e --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file