refactor: ログを追加

This commit is contained in:
usbharu 2023-10-24 12:52:15 +09:00
parent 8347bd7d0f
commit 63c15b35d1
6 changed files with 54 additions and 10 deletions

View File

@ -10,13 +10,24 @@ import org.springframework.web.bind.annotation.RestController
@RestController @RestController
class InboxControllerImpl(private val apService: APService) : InboxController { class InboxControllerImpl(private val apService: APService) : InboxController {
override suspend fun inbox(@RequestBody string: String): ResponseEntity<Unit> { 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) LOGGER.info("INBOX Processing Activity Type: {}", parseActivity)
try {
apService.processActivity(string, parseActivity) 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) return ResponseEntity(HttpStatus.ACCEPTED)
} }
companion object { companion object {
val LOGGER = LoggerFactory.getLogger(InboxControllerImpl::class.java) private val LOGGER = LoggerFactory.getLogger(InboxControllerImpl::class.java)
} }
} }

View File

@ -5,6 +5,7 @@ import dev.usbharu.hideout.domain.model.wellknown.WebFinger
import dev.usbharu.hideout.service.api.WebFingerApiService import dev.usbharu.hideout.service.api.WebFingerApiService
import dev.usbharu.hideout.util.AcctUtil import dev.usbharu.hideout.util.AcctUtil
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller import org.springframework.stereotype.Controller
@ -18,7 +19,13 @@ class WebFingerController(
) { ) {
@GetMapping("/.well-known/webfinger") @GetMapping("/.well-known/webfinger")
fun webfinger(@RequestParam("resource") resource: String): ResponseEntity<WebFinger> = runBlocking { 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 = val user =
webFingerApiService.findByNameAndDomain(acct.username, acct.domain ?: applicationConfig.url.host) webFingerApiService.findByNameAndDomain(acct.username, acct.domain ?: applicationConfig.url.host)
val webFinger = WebFinger( val webFinger = WebFinger(
@ -31,6 +38,11 @@ class WebFingerController(
) )
) )
) )
logger.info("SUCCESS Lookup webfinger resource: {} acct: {}", resource, acct)
ResponseEntity(webFinger, HttpStatus.OK) ResponseEntity(webFinger, HttpStatus.OK)
} }
companion object {
private val logger = LoggerFactory.getLogger(WebFingerController::class.java)
}
} }

View File

@ -188,11 +188,21 @@ class APServiceImpl(
val logger: Logger = LoggerFactory.getLogger(APServiceImpl::class.java) val logger: Logger = LoggerFactory.getLogger(APServiceImpl::class.java)
override fun parseActivity(json: String): ActivityType { override fun parseActivity(json: String): ActivityType {
val readTree = objectMapper.readTree(json) val readTree = objectMapper.readTree(json)
logger.trace("readTree: {}", readTree) logger.trace(
"""
|
|***** Trace Begin Activity *****
|
|{}
|
|***** Trace End Activity *****
|
""".trimMargin(), readTree.toPrettyString()
)
if (readTree.isObject.not()) { if (readTree.isObject.not()) {
throw JsonParseException("Json is not object.") throw JsonParseException("Json is not object.")
} }
val type = readTree["type"] val type = readTree["type"] ?: throw JsonParseException("Type is null")
if (type.isArray) { if (type.isArray) {
return type.firstNotNullOf { jsonNode: JsonNode -> return type.firstNotNullOf { jsonNode: JsonNode ->
ActivityType.values().firstOrNull { it.name.equals(jsonNode.asText(), true) } ActivityType.values().firstOrNull { it.name.equals(jsonNode.asText(), true) }

View File

@ -7,6 +7,7 @@ import dev.usbharu.hideout.query.PostQueryService
import dev.usbharu.hideout.repository.PostRepository import dev.usbharu.hideout.repository.PostRepository
import dev.usbharu.hideout.repository.UserRepository import dev.usbharu.hideout.repository.UserRepository
import org.jetbrains.exposed.exceptions.ExposedSQLException import org.jetbrains.exposed.exceptions.ExposedSQLException
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.time.Instant import java.time.Instant
import java.util.* import java.util.*
@ -22,12 +23,19 @@ class PostServiceImpl(
private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>()) private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>())
override suspend fun createLocal(post: PostCreateDto): Post { 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) val create = internalCreate(post, true)
interceptors.forEach { it.run(create) } interceptors.forEach { it.run(create) }
logger.info("SUCCESS Create Local Post url: {}", create.url)
return create 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) { override fun addInterceptor(postCreateInterceptor: PostCreateInterceptor) {
interceptors.add(postCreateInterceptor) interceptors.add(postCreateInterceptor)
@ -58,4 +66,8 @@ class PostServiceImpl(
) )
return internalCreate(createPost, isLocal) return internalCreate(createPost, isLocal)
} }
companion object {
private val logger = LoggerFactory.getLogger(PostServiceImpl::class.java)
}
} }

View File

@ -20,9 +20,7 @@ class ReactionServiceImpl(
reactionRepository.save( reactionRepository.save(
Reaction(reactionRepository.generateId(), 0, postId, userId) Reaction(reactionRepository.generateId(), 0, postId, userId)
) )
} catch (e: ExposedSQLException) { } catch (_: ExposedSQLException) {
LOGGER.warn("FAILED Failure to persist reaction information.")
LOGGER.debug("FAILED", e)
} }
} }
} }

View File

@ -14,4 +14,5 @@
<logger name="io.ktor.server.plugins.contentnegotiation" level="INFO"/> <logger name="io.ktor.server.plugins.contentnegotiation" level="INFO"/>
<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter" level="INFO"/> <logger name="org.springframework.web.filter.CommonsRequestLoggingFilter" level="INFO"/>
<logger name="org.mongodb.driver.protocol.command" level="INFO"/> <logger name="org.mongodb.driver.protocol.command" level="INFO"/>
<logger name="dev.usbharu" level="TRACE"/>
</configuration> </configuration>