mirror of https://github.com/usbharu/Hideout.git
Merge pull request #96 from usbharu/feature/logging-2
Feature/logging 2
This commit is contained in:
commit
31a06a3e9e
|
@ -137,6 +137,7 @@ dependencies {
|
|||
implementation("software.amazon.awssdk:s3:2.20.157")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.7.3")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-slf4j:1.7.3")
|
||||
implementation("dev.usbharu:http-signature:1.0.0")
|
||||
|
||||
implementation("io.ktor:ktor-client-logging-jvm:$ktor_version")
|
||||
|
|
|
@ -10,13 +10,24 @@ import org.springframework.web.bind.annotation.RestController
|
|||
@RestController
|
||||
class InboxControllerImpl(private val apService: APService) : InboxController {
|
||||
override suspend fun inbox(@RequestBody string: String): ResponseEntity<Unit> {
|
||||
val parseActivity = apService.parseActivity(string)
|
||||
val parseActivity = try {
|
||||
apService.parseActivity(string)
|
||||
} catch (e: Exception) {
|
||||
LOGGER.warn("FAILED Parse Activity", e)
|
||||
return ResponseEntity.accepted().build()
|
||||
}
|
||||
LOGGER.info("INBOX Processing Activity Type: {}", parseActivity)
|
||||
apService.processActivity(string, parseActivity)
|
||||
try {
|
||||
apService.processActivity(string, parseActivity)
|
||||
} catch (e: Exception) {
|
||||
LOGGER.warn("FAILED Process Activity $parseActivity", e)
|
||||
return ResponseEntity(HttpStatus.ACCEPTED)
|
||||
}
|
||||
LOGGER.info("SUCCESS Processing Activity Type: {}", parseActivity)
|
||||
return ResponseEntity(HttpStatus.ACCEPTED)
|
||||
}
|
||||
|
||||
companion object {
|
||||
val LOGGER = LoggerFactory.getLogger(InboxControllerImpl::class.java)
|
||||
private val LOGGER = LoggerFactory.getLogger(InboxControllerImpl::class.java)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import dev.usbharu.hideout.domain.model.wellknown.WebFinger
|
|||
import dev.usbharu.hideout.service.api.WebFingerApiService
|
||||
import dev.usbharu.hideout.util.AcctUtil
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.stereotype.Controller
|
||||
|
@ -18,7 +19,13 @@ class WebFingerController(
|
|||
) {
|
||||
@GetMapping("/.well-known/webfinger")
|
||||
fun webfinger(@RequestParam("resource") resource: String): ResponseEntity<WebFinger> = runBlocking {
|
||||
val acct = AcctUtil.parse(resource.replace("acct:", ""))
|
||||
logger.info("WEBFINGER Lookup webfinger resource: {}", resource)
|
||||
val acct = try {
|
||||
AcctUtil.parse(resource.replace("acct:", ""))
|
||||
} catch (e: IllegalArgumentException) {
|
||||
logger.warn("FAILED Parse acct.", e)
|
||||
return@runBlocking ResponseEntity.badRequest().build()
|
||||
}
|
||||
val user =
|
||||
webFingerApiService.findByNameAndDomain(acct.username, acct.domain ?: applicationConfig.url.host)
|
||||
val webFinger = WebFinger(
|
||||
|
@ -31,6 +38,11 @@ class WebFingerController(
|
|||
)
|
||||
)
|
||||
)
|
||||
logger.info("SUCCESS Lookup webfinger resource: {} acct: {}", resource, acct)
|
||||
ResponseEntity(webFinger, HttpStatus.OK)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(WebFingerController::class.java)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@ import kotlinx.coroutines.CoroutineScope
|
|||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.slf4j.MDCContext
|
||||
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.beans.factory.annotation.Qualifier
|
||||
|
@ -43,8 +44,8 @@ interface APNoteService {
|
|||
|
||||
@Cacheable("fetchNote")
|
||||
fun fetchNoteAsync(url: String, targetActor: String? = null): Deferred<Note> {
|
||||
return CoroutineScope(Dispatchers.IO).async {
|
||||
newSuspendedTransaction {
|
||||
return CoroutineScope(Dispatchers.IO + MDCContext()).async {
|
||||
newSuspendedTransaction(MDCContext()) {
|
||||
fetchNote(url, targetActor)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -188,11 +188,21 @@ class APServiceImpl(
|
|||
val logger: Logger = LoggerFactory.getLogger(APServiceImpl::class.java)
|
||||
override fun parseActivity(json: String): ActivityType {
|
||||
val readTree = objectMapper.readTree(json)
|
||||
logger.trace("readTree: {}", readTree)
|
||||
logger.trace(
|
||||
"""
|
||||
|
|
||||
|***** Trace Begin Activity *****
|
||||
|
|
||||
|{}
|
||||
|
|
||||
|***** Trace End Activity *****
|
||||
|
|
||||
""".trimMargin(), readTree.toPrettyString()
|
||||
)
|
||||
if (readTree.isObject.not()) {
|
||||
throw JsonParseException("Json is not object.")
|
||||
}
|
||||
val type = readTree["type"]
|
||||
val type = readTree["type"] ?: throw JsonParseException("Type is null")
|
||||
if (type.isArray) {
|
||||
return type.firstNotNullOf { jsonNode: JsonNode ->
|
||||
ActivityType.values().firstOrNull { it.name.equals(jsonNode.asText(), true) }
|
||||
|
|
|
@ -1,18 +1,19 @@
|
|||
package dev.usbharu.hideout.service.core
|
||||
|
||||
import kotlinx.coroutines.slf4j.MDCContext
|
||||
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class ExposedTransaction : Transaction {
|
||||
override suspend fun <T> transaction(block: suspend () -> T): T {
|
||||
return newSuspendedTransaction {
|
||||
return newSuspendedTransaction(MDCContext()) {
|
||||
block()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun <T> transaction(transactionLevel: Int, block: suspend () -> T): T {
|
||||
return newSuspendedTransaction(transactionIsolation = transactionLevel) {
|
||||
return newSuspendedTransaction(MDCContext(), transactionIsolation = transactionLevel) {
|
||||
block()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,10 +5,13 @@ import jakarta.servlet.FilterChain
|
|||
import jakarta.servlet.ServletRequest
|
||||
import jakarta.servlet.ServletResponse
|
||||
import org.slf4j.MDC
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.boot.autoconfigure.security.SecurityProperties
|
||||
import org.springframework.core.annotation.Order
|
||||
import org.springframework.stereotype.Component
|
||||
import java.util.*
|
||||
|
||||
@Service
|
||||
@Component
|
||||
@Order(SecurityProperties.DEFAULT_FILTER_ORDER - 1)
|
||||
class MdcXrequestIdFilter : Filter {
|
||||
override fun doFilter(request: ServletRequest?, response: ServletResponse?, chain: FilterChain) {
|
||||
val uuid = UUID.randomUUID()
|
||||
|
|
|
@ -7,6 +7,7 @@ import dev.usbharu.hideout.query.PostQueryService
|
|||
import dev.usbharu.hideout.repository.PostRepository
|
||||
import dev.usbharu.hideout.repository.UserRepository
|
||||
import org.jetbrains.exposed.exceptions.ExposedSQLException
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.stereotype.Service
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
|
@ -22,12 +23,19 @@ class PostServiceImpl(
|
|||
private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>())
|
||||
|
||||
override suspend fun createLocal(post: PostCreateDto): Post {
|
||||
logger.info("START Create Local Post user: {}, media: {}", post.userId, post.mediaIds.size)
|
||||
val create = internalCreate(post, true)
|
||||
interceptors.forEach { it.run(create) }
|
||||
logger.info("SUCCESS Create Local Post url: {}", create.url)
|
||||
return create
|
||||
}
|
||||
|
||||
override suspend fun createRemote(post: Post): Post = internalCreate(post, false)
|
||||
override suspend fun createRemote(post: Post): Post {
|
||||
logger.info("START Create Remote Post user: {}, remote url: {}", post.userId, post.apId)
|
||||
val createdPost = internalCreate(post, false)
|
||||
logger.info("SUCCESS Create Remote Post url: {}", createdPost.url)
|
||||
return createdPost
|
||||
}
|
||||
|
||||
override fun addInterceptor(postCreateInterceptor: PostCreateInterceptor) {
|
||||
interceptors.add(postCreateInterceptor)
|
||||
|
@ -58,4 +66,8 @@ class PostServiceImpl(
|
|||
)
|
||||
return internalCreate(createPost, isLocal)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(PostServiceImpl::class.java)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,9 +20,7 @@ class ReactionServiceImpl(
|
|||
reactionRepository.save(
|
||||
Reaction(reactionRepository.generateId(), 0, postId, userId)
|
||||
)
|
||||
} catch (e: ExposedSQLException) {
|
||||
LOGGER.warn("FAILED Failure to persist reaction information.")
|
||||
LOGGER.debug("FAILED", e)
|
||||
} catch (_: ExposedSQLException) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,4 +14,5 @@
|
|||
<logger name="io.ktor.server.plugins.contentnegotiation" level="INFO"/>
|
||||
<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter" level="INFO"/>
|
||||
<logger name="org.mongodb.driver.protocol.command" level="INFO"/>
|
||||
<logger name="dev.usbharu" level="TRACE"/>
|
||||
</configuration>
|
||||
|
|
Loading…
Reference in New Issue