feature #10: first implementation of logger
This commit is contained in:
@@ -18,7 +18,10 @@ dependencies {
|
|||||||
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.31"
|
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.31"
|
||||||
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.9"
|
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.9"
|
||||||
implementation "com.github.jasync-sql:jasync-postgresql:0.9.53"
|
implementation "com.github.jasync-sql:jasync-postgresql:0.9.53"
|
||||||
|
implementation "org.slf4j:slf4j-api:1.7.26"
|
||||||
|
|
||||||
|
testImplementation "ch.qos.logback:logback-classic:1.2.3"
|
||||||
|
testImplementation "ch.qos.logback:logback-core:1.2.3"
|
||||||
testImplementation "io.mockk:mockk:1.9"
|
testImplementation "io.mockk:mockk:1.9"
|
||||||
testImplementation "org.junit.jupiter:junit-jupiter:5.4.2"
|
testImplementation "org.junit.jupiter:junit-jupiter:5.4.2"
|
||||||
testImplementation 'org.amshove.kluent:kluent:1.47'
|
testImplementation 'org.amshove.kluent:kluent:1.47'
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import com.github.jasync.sql.db.postgresql.PostgreSQLConnectionBuilder
|
|||||||
import com.github.jasync.sql.db.util.length
|
import com.github.jasync.sql.db.util.length
|
||||||
import fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.EntityI
|
||||||
import fr.postgresjson.serializer.Serializer
|
import fr.postgresjson.serializer.Serializer
|
||||||
|
import fr.postgresjson.utils.LoggerDelegate
|
||||||
|
import org.slf4j.Logger
|
||||||
import java.util.concurrent.CompletableFuture
|
import java.util.concurrent.CompletableFuture
|
||||||
|
|
||||||
|
|
||||||
@@ -31,6 +33,7 @@ class Connection(
|
|||||||
): Executable {
|
): Executable {
|
||||||
private lateinit var connection: ConnectionPool<PostgreSQLConnection>
|
private lateinit var connection: ConnectionPool<PostgreSQLConnection>
|
||||||
private val serializer = Serializer()
|
private val serializer = Serializer()
|
||||||
|
private val logger: Logger? by LoggerDelegate()
|
||||||
|
|
||||||
fun connect(): ConnectionPool<PostgreSQLConnection> {
|
fun connect(): ConnectionPool<PostgreSQLConnection> {
|
||||||
if (!::connection.isInitialized || !connection.isConnected()) {
|
if (!::connection.isInitialized || !connection.isConnected()) {
|
||||||
@@ -132,6 +135,7 @@ class Connection(
|
|||||||
select(sql, object: TypeReference<List<R>>() {}, values)
|
select(sql, object: TypeReference<List<R>>() {}, values)
|
||||||
|
|
||||||
override fun exec(sql: String, values: List<Any?>): CompletableFuture<QueryResult> {
|
override fun exec(sql: String, values: List<Any?>): CompletableFuture<QueryResult> {
|
||||||
|
logger?.debug(sql, values)
|
||||||
return connect().sendPreparedStatement(sql, compileArgs(values))
|
return connect().sendPreparedStatement(sql, compileArgs(values))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
11
src/main/kotlin/fr/postgresjson/utils/LoggerDelegate.kt
Normal file
11
src/main/kotlin/fr/postgresjson/utils/LoggerDelegate.kt
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package fr.postgresjson.utils
|
||||||
|
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import kotlin.properties.ReadOnlyProperty
|
||||||
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
|
internal class LoggerDelegate<in R : Any> : ReadOnlyProperty<R, Logger> {
|
||||||
|
override fun getValue(thisRef: R, property: KProperty<*>)
|
||||||
|
= LoggerFactory.getLogger(thisRef.javaClass.packageName)
|
||||||
|
}
|
||||||
34
src/test/resources/logback.xml
Normal file
34
src/test/resources/logback.xml
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
<property name="LOG_DIR" value="./postgresjson" />
|
||||||
|
<appender name="query" class="ch.qos.logback.core.FileAppender">
|
||||||
|
<file>${LOG_DIR}/query.log</file>
|
||||||
|
<append>true</append>
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
<appender name="other" class="ch.qos.logback.core.FileAppender">
|
||||||
|
<file>${LOG_DIR}/other.log</file>
|
||||||
|
<append>true</append>
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="trace">
|
||||||
|
<appender-ref ref="STDOUT"/>
|
||||||
|
<appender-ref ref="other" />
|
||||||
|
</root>
|
||||||
|
|
||||||
|
<logger name="org.eclipse.jetty" level="INFO"/>
|
||||||
|
<logger name="io.netty" level="INFO"/>
|
||||||
|
|
||||||
|
<logger name="fr.postgresjson.connexion" level="trace">
|
||||||
|
<appender-ref ref="query" />
|
||||||
|
</logger>
|
||||||
|
</configuration>
|
||||||
Reference in New Issue
Block a user