mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-25 16:22:35 +03:00
improve logging
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
SPRING_ACTIVE_PROFILE=container
|
||||
SPRING_LOG_LEVEL=INFO
|
||||
|
||||
DB_URL=localhost:5432
|
||||
DB_NAME=demo
|
||||
DB_SCHEMA=public
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<Product> = productRepository.findAll(pageable)
|
||||
|
||||
|
||||
24
src/main/kotlin/com/example/demo/utils/Logger.kt
Normal file
24
src/main/kotlin/com/example/demo/utils/Logger.kt
Normal file
@@ -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<in R : Any> : ReadOnlyProperty<R, Logger> {
|
||||
override fun getValue(thisRef: R, property: KProperty<*>)
|
||||
= getLogger(getClassForLogging(thisRef.javaClass))
|
||||
|
||||
private fun <T : Any> getClassForLogging(javaClass: Class<T>): Class<*> {
|
||||
return javaClass.enclosingClass?.takeIf {
|
||||
it.kotlin.companionObject?.java == javaClass
|
||||
} ?: javaClass
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun getLogger(forClass: Class<*>): Logger = LoggerFactory.getLogger(forClass)
|
||||
@@ -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}
|
||||
|
||||
15
src/main/resources/logback-spring.xml
Normal file
15
src/main/resources/logback-spring.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<configuration>
|
||||
<springProfile name="default">
|
||||
<include resource="org/springframework/boot/logging/logback/base.xml" />
|
||||
</springProfile>
|
||||
|
||||
<springProfile name="container">
|
||||
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder class="net.logstash.logback.encoder.LogstashEncoder" />
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="jsonConsoleAppender" />
|
||||
</root>
|
||||
</springProfile>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user