Skip to content

Commit

Permalink
feat: file logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ShanePark committed Mar 6, 2025
1 parent 5ecf7f3 commit b4752fc
Show file tree
Hide file tree
Showing 31 changed files with 879 additions and 763 deletions.
2 changes: 1 addition & 1 deletion dutypark_secret
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.tistory.shanepark.dutypark.common.advice

import com.tistory.shanepark.dutypark.common.exceptions.DutyparkAuthException
import jakarta.servlet.http.HttpServletRequest
import org.slf4j.LoggerFactory
import org.springframework.core.Ordered
import org.springframework.core.annotation.Order
import org.springframework.stereotype.Controller
Expand All @@ -13,7 +14,7 @@ import java.net.URLEncoder
@ControllerAdvice(annotations = [Controller::class])
@Order(Ordered.HIGHEST_PRECEDENCE)
class ViewExceptionControllerAdvice {
val log: org.slf4j.Logger = org.slf4j.LoggerFactory.getLogger(this.javaClass)
val log: org.slf4j.Logger = LoggerFactory.getLogger(this.javaClass)

@ExceptionHandler
fun notAuthorizedHandler(e: DutyparkAuthException, request: HttpServletRequest): ModelAndView {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.tistory.shanepark.dutypark.common.config

import ch.qos.logback.classic.Level
import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.rolling.RollingFileAppender
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy
import jakarta.annotation.PostConstruct
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration

@Configuration
class LogbackConfig {

@Value("\${dutypark.log.path}")
private lateinit var logPath: String

val log: Logger = LoggerFactory.getLogger(LogbackConfig::class.java)

@PostConstruct
fun configureLogging() {
val loggerContext = LoggerFactory.getILoggerFactory() as LoggerContext

val encoder = PatternLayoutEncoder().apply {
context = loggerContext
pattern = "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
start()
}

val fileAppender = RollingFileAppender<ILoggingEvent>().apply {
context = loggerContext
name = "FILE"
this.encoder = encoder
isAppend = true
}

val rollingPolicy = TimeBasedRollingPolicy<ILoggingEvent>().apply {
context = loggerContext
fileNamePattern = "${logPath}/dutypark-%d{yyyy-MM-dd}.log"
maxHistory = 365
setParent(fileAppender)
start()
}

fileAppender.rollingPolicy = rollingPolicy
fileAppender.start()

val rootLogger = loggerContext.getLogger("ROOT")
rootLogger.level = Level.INFO
rootLogger.addAppender(fileAppender)

log.info("log file path: $logPath")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.tistory.shanepark.dutypark.common.slack.notifier.SlackNotifierSender
import net.gpedro.integrations.slack.SlackApi
import net.gpedro.integrations.slack.SlackMessage
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
Expand All @@ -19,7 +20,7 @@ class SlackApiConfiguration(
val slackToken: String
) {

private val log: Logger = org.slf4j.LoggerFactory.getLogger(this.javaClass)
private val log: Logger = LoggerFactory.getLogger(this.javaClass)

@Bean("slackTaskExecutor")
fun threadPoolTaskExecutor(): TaskExecutor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import net.gpedro.integrations.slack.SlackMessage
import org.apache.catalina.connector.ClientAbortException
import org.apache.coyote.CloseNowException
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
import org.springframework.web.HttpRequestMethodNotSupportedException
import org.springframework.web.bind.annotation.ControllerAdvice
Expand All @@ -21,7 +22,7 @@ import java.util.*
class ErrorDetectAdvisor(
private val slackNotifier: SlackNotifier,
) {
val log: Logger = org.slf4j.LoggerFactory.getLogger(this.javaClass)
val log: Logger = LoggerFactory.getLogger(this.javaClass)

@ExceptionHandler(HttpRequestMethodNotSupportedException::class)
@ResponseBody
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.tistory.shanepark.dutypark.common.slack.notifier

import net.gpedro.integrations.slack.SlackMessage
import org.slf4j.LoggerFactory

class SlackNotifierLogger : SlackNotifier {

private val log: org.slf4j.Logger = org.slf4j.LoggerFactory.getLogger(this.javaClass)
private val log: org.slf4j.Logger = LoggerFactory.getLogger(this.javaClass)

override fun call(slackMessage: SlackMessage) {
log.info("SlackNotifierLogger: $slackMessage")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tistory.shanepark.dutypark.department.controller

import com.tistory.shanepark.dutypark.common.domain.dto.PageResponse
import com.tistory.shanepark.dutypark.common.exceptions.DutyparkAuthException
import com.tistory.shanepark.dutypark.dashboard.domain.DashboardDepartment
import com.tistory.shanepark.dutypark.department.domain.dto.DepartmentCreateDto
import com.tistory.shanepark.dutypark.department.domain.dto.DepartmentDto
Expand All @@ -13,8 +14,12 @@ import com.tistory.shanepark.dutypark.duty.batch.domain.DutyBatchTeamResult
import com.tistory.shanepark.dutypark.duty.batch.domain.DutyBatchTemplate
import com.tistory.shanepark.dutypark.duty.batch.exceptions.DutyBatchException
import com.tistory.shanepark.dutypark.duty.batch.service.DutyBatchService
import com.tistory.shanepark.dutypark.member.domain.annotation.Login
import com.tistory.shanepark.dutypark.member.repository.MemberRepository
import com.tistory.shanepark.dutypark.security.domain.dto.LoginMember
import jakarta.validation.Valid
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationContext
import org.springframework.data.domain.Pageable
import org.springframework.data.web.PageableDefault
Expand All @@ -32,6 +37,8 @@ class DepartmentAdminController(
private val applicationContext: ApplicationContext,
) {

val log: Logger = LoggerFactory.getLogger(DepartmentAdminController::class.java)

@GetMapping
fun findAll(@PageableDefault(page = 0, size = 10) page: Pageable): PageResponse<SimpleDepartmentDto> {
val result = departmentService.findAllWithMemberCount(page)
Expand Down Expand Up @@ -90,22 +97,27 @@ class DepartmentAdminController(

@PostMapping("/{id}/duty")
fun uploadBatchTemplate(
@Login loginMember: LoginMember,
@PathVariable id: Long,
@RequestParam(name = "file") file: MultipartFile,
@RequestParam(name = "year") year: Int,
@RequestParam(name = "month") month: Int
): DutyBatchTeamResult {
if (!loginMember.isAdmin) {
throw DutyparkAuthException("$loginMember is not admin")
}
val department = departmentRepository.findById(id).orElseThrow()
val batchTemplate = department.dutyBatchTemplate ?: throw IllegalArgumentException("templateName is required")
val dutyBatchService = applicationContext.getBean(batchTemplate.batchServiceClass) as DutyBatchService
try {
return dutyBatchService.batchUploadDepartment(
return try {
log.info("batch duty upload by $loginMember for department ${department.name}(${department.id}) year=$year, month=$month")
dutyBatchService.batchUploadDepartment(
departmentId = id,
file = file,
yearMonth = YearMonth.of(year, month)
)
} catch (e: DutyBatchException) {
return DutyBatchTeamResult.fail(e.message ?: "알 수 없는 원인으로 시간표 업로드 실패.")
DutyBatchTeamResult.fail(e.message ?: "알 수 없는 원인으로 시간표 업로드 실패.")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.tistory.shanepark.dutypark.member.domain.annotation.Login
import com.tistory.shanepark.dutypark.member.service.MemberService
import com.tistory.shanepark.dutypark.security.domain.dto.LoginMember
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationContext
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
Expand All @@ -23,7 +24,7 @@ class DutyBatchController(
private val memberService: MemberService,
private val applicationContext: ApplicationContext,
) {
val log: Logger = org.slf4j.LoggerFactory.getLogger(DutyBatchController::class.java)
val log: Logger = LoggerFactory.getLogger(DutyBatchController::class.java)

@GetMapping("/templates")
fun getTemplates(
Expand All @@ -48,6 +49,7 @@ class DutyBatchController(

val dutyBatchService = applicationContext.getBean(dutyBatchTemplate.batchServiceClass) as DutyBatchService
return try {
log.info("batch duty upload by $loginMember for member $memberId. year=$year, month=$month")
dutyBatchService.batchUploadMember(memberId = memberId, file = file, yearMonth = YearMonth.of(year, month))
} catch (e: DutyBatchException) {
DutyBatchResult.fail(e.batchErrorMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.tistory.shanepark.dutypark.duty.service.DutyService
import com.tistory.shanepark.dutypark.member.domain.annotation.Login
import com.tistory.shanepark.dutypark.security.domain.dto.LoginMember
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.time.YearMonth
Expand All @@ -17,7 +18,7 @@ import java.time.YearMonth
class DutyController(
private val dutyService: DutyService,
) {
val log: Logger = org.slf4j.LoggerFactory.getLogger(DutyController::class.java)
val log: Logger = LoggerFactory.getLogger(DutyController::class.java)

@GetMapping
fun getDuties(
Expand All @@ -40,6 +41,7 @@ class DutyController(
): ResponseEntity<Boolean> {
checkAuthentication(loginMember, dutyUpdateDto.memberId)
dutyService.update(dutyUpdateDto)
log.info("$loginMember update duty: $dutyUpdateDto")
return ResponseEntity.ok(true)
}

Expand All @@ -50,6 +52,7 @@ class DutyController(
): ResponseEntity<Boolean> {
checkAuthentication(loginMember, dutyBatchUpdateDto.memberId)
dutyService.update(dutyBatchUpdateDto)
log.info("$loginMember update duty batch: $dutyBatchUpdateDto")
return ResponseEntity.ok(true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.tistory.shanepark.dutypark.member.service.FriendService
import com.tistory.shanepark.dutypark.member.service.MemberService
import com.tistory.shanepark.dutypark.security.domain.dto.LoginMember
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
Expand All @@ -18,7 +19,7 @@ class DutyViewController(
val memberService: MemberService,
val friendService: FriendService,
) : ViewController() {
val log: Logger = org.slf4j.LoggerFactory.getLogger(this.javaClass)
val log: Logger = LoggerFactory.getLogger(this.javaClass)

@GetMapping("/duty/{id}")
fun retrieveMemberDuty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.tistory.shanepark.dutypark.member.domain.dto.DDaySaveDto
import com.tistory.shanepark.dutypark.member.service.DDayService
import com.tistory.shanepark.dutypark.security.domain.dto.LoginMember
import jakarta.validation.Valid
import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
Expand All @@ -15,7 +16,7 @@ import org.springframework.web.bind.annotation.*
class DDayController(
private val dDayService: DDayService
) {
private val log: org.slf4j.Logger = org.slf4j.LoggerFactory.getLogger(this.javaClass)
private val log = LoggerFactory.getLogger(this.javaClass)

@PostMapping
@SlackNotification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.tistory.shanepark.dutypark.member.domain.entity.DDayEvent
import com.tistory.shanepark.dutypark.member.repository.DDayRepository
import com.tistory.shanepark.dutypark.member.repository.MemberRepository
import com.tistory.shanepark.dutypark.security.domain.dto.LoginMember
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDate
Expand All @@ -18,7 +20,7 @@ class DDayService(
private val dDayRepository: DDayRepository
) {

val log: org.slf4j.Logger = org.slf4j.LoggerFactory.getLogger(this.javaClass)
val log: Logger = LoggerFactory.getLogger(this.javaClass)

fun createDDay(loginMember: LoginMember, dDaySaveDto: DDaySaveDto): DDayEvent {
val member = memberRepository.findById(loginMember.id).orElseThrow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.tistory.shanepark.dutypark.security.domain.dto.LoginMember
import com.tistory.shanepark.dutypark.security.domain.dto.RefreshTokenDto
import com.tistory.shanepark.dutypark.security.domain.entity.RefreshToken
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import java.time.LocalDateTime
Expand All @@ -19,7 +20,7 @@ class RefreshTokenService(
private val refreshTokenRepository: RefreshTokenRepository,
private val jwtConfig: JwtConfig,
) {
private val log: Logger = org.slf4j.LoggerFactory.getLogger(this.javaClass)
private val log: Logger = LoggerFactory.getLogger(this.javaClass)

@Scheduled(cron = "0 0 0 * * *")
fun revokeExpiredRefreshTokens() {
Expand All @@ -39,7 +40,7 @@ class RefreshTokenService(
fun deleteRefreshToken(loginMember: LoginMember, id: Long) {
val refreshToken = refreshTokenRepository.findById(id).orElseThrow()
if (!loginMember.isAdmin && refreshToken.member.id != loginMember.id) {
log.warn("No authority to delete refresh token. loginMemberId:${loginMember.id}, refreshTokenId:$id")
log.warn("No authority to delete refresh token. loginMemberId:$loginMember, refreshTokenId:$id")
throw DutyparkAuthException("No authority to delete refresh token.")
}
refreshTokenRepository.delete(refreshToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.tistory.shanepark.dutypark.schedule.domain.enums.ParsingTimeStatus
import com.tistory.shanepark.dutypark.schedule.repository.ScheduleRepository
import com.tistory.shanepark.dutypark.schedule.timeparsing.service.ScheduleTimeParsingQueueManager
import com.tistory.shanepark.dutypark.security.domain.dto.LoginMember
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime
Expand All @@ -26,7 +27,7 @@ class ScheduleService(
private val friendService: FriendService,
private val scheduleTimeParsingQueueManager: ScheduleTimeParsingQueueManager,
) {
private val log = org.slf4j.LoggerFactory.getLogger(this.javaClass)
private val log = LoggerFactory.getLogger(this.javaClass)

@Transactional(readOnly = true)
fun findSchedulesByYearAndMonth(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.ObjectMapper
import com.tistory.shanepark.dutypark.schedule.timeparsing.domain.ScheduleTimeParsingRequest
import com.tistory.shanepark.dutypark.schedule.timeparsing.domain.ScheduleTimeParsingResponse
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.ai.chat.client.ChatClient
import org.springframework.ai.chat.model.ChatModel
import org.springframework.stereotype.Service
Expand All @@ -14,7 +16,7 @@ class ScheduleTimeParsingService(
private val objectMapper: ObjectMapper,
) {
private val chatClient = ChatClient.builder(chatModel).build()
private val log: org.slf4j.Logger = org.slf4j.LoggerFactory.getLogger(ScheduleTimeParsingService::class.java)
private val log: Logger = LoggerFactory.getLogger(ScheduleTimeParsingService::class.java)

fun parseScheduleTime(request: ScheduleTimeParsingRequest): ScheduleTimeParsingResponse {
val prompt = generatePrompt(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.tistory.shanepark.dutypark.security.domain.dto.LoginMember
import com.tistory.shanepark.dutypark.security.domain.dto.PasswordChangeDto
import com.tistory.shanepark.dutypark.security.service.AuthService
import jakarta.servlet.http.HttpServletRequest
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.*
Expand All @@ -16,7 +17,7 @@ import org.springframework.web.bind.annotation.*
class AuthController(
private val authService: AuthService,
) {
private val log = org.slf4j.LoggerFactory.getLogger(AuthController::class.java)
private val log = LoggerFactory.getLogger(AuthController::class.java)

@PostMapping("login")
fun login(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ data class LoginMember(
const val ATTR_NAME: String = "loginMember"
}

override fun toString(): String {
return "${name}(${id})"
}


}
Loading

0 comments on commit b4752fc

Please sign in to comment.