generated from GSM-MSG/MSG-Repository-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from GSM-MSG/43-feat/http-logging-aspect
#44 HttpLoggingAspect 클래스 작성
- Loading branch information
Showing
2 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
bitgouel-api/src/main/kotlin/team/msg/common/aop/HttpLoggingAspect.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package team.msg.common.aop | ||
|
||
import org.aspectj.lang.JoinPoint | ||
import org.aspectj.lang.ProceedingJoinPoint | ||
import org.aspectj.lang.annotation.Around | ||
import org.aspectj.lang.annotation.Aspect | ||
import org.aspectj.lang.annotation.Pointcut | ||
import org.aspectj.lang.reflect.CodeSignature | ||
import org.aspectj.lang.reflect.MethodSignature | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.context.request.RequestContextHolder | ||
import org.springframework.web.context.request.ServletRequestAttributes | ||
import team.msg.common.logger.LoggerDelegator | ||
import java.util.* | ||
import kotlin.collections.HashMap | ||
import kotlin.collections.HashSet | ||
|
||
@Aspect | ||
@Component | ||
class HttpLoggingAspect { | ||
|
||
private val log by LoggerDelegator() | ||
|
||
@Pointcut("within(@org.springframework.web.bind.annotation.RestController *)") | ||
fun onRequest() {} | ||
|
||
@Around("onRequest()") | ||
@Throws(Throwable::class) | ||
fun logging(proceedingJoinPoint: ProceedingJoinPoint): Any? { | ||
val request = (RequestContextHolder.currentRequestAttributes() as ServletRequestAttributes).request | ||
val ip = request.remoteAddr | ||
val method = request.method | ||
val uri = request.requestURI | ||
val sessionId = request.requestedSessionId | ||
val params = request.queryString | ||
val contentType = request.contentType | ||
val userAgent = request.getHeader("User-Agent") | ||
val signature: MethodSignature = proceedingJoinPoint.signature as MethodSignature | ||
val className = signature.declaringType.simpleName | ||
val methodName = signature.name | ||
val headerNames = request.headerNames | ||
val headerSet: MutableSet<String> = HashSet() | ||
|
||
while (headerNames.hasMoreElements()) { | ||
val headerName = headerNames.nextElement() | ||
headerSet.add(headerName) | ||
} | ||
val code = UUID.randomUUID() | ||
log.info( | ||
"At {}#{} [Request:{}] IP: {}, Session-ID: {}, URI: {}, Params: {}, Content-Type: {}, User-Agent: {}, Headers: {}, Parameters: {}, Code: {}", | ||
className, methodName, method, ip, sessionId, uri, params, contentType, userAgent, headerSet, params(proceedingJoinPoint), code | ||
) | ||
val result = proceedingJoinPoint.proceed() | ||
when (result) { | ||
is ResponseEntity<*> -> { | ||
log.info( | ||
"At {}#{} [Response:{}] IP: {}, Session-ID: {}, Headers: {}, Response: {}, Status-Code: {}, Code: {}", | ||
className, methodName, method, ip,sessionId, result.headers, result.body, result.statusCode, code | ||
) | ||
} | ||
|
||
null -> { | ||
log.info( | ||
"At {}#{} [Response: null] IP: {}, Session-ID: {}, Code: {}", | ||
className, methodName, ip, sessionId, code | ||
) | ||
} | ||
|
||
else -> { | ||
throw RuntimeException("유효하지 않은 Controller 반환 타입입니다.") | ||
} | ||
} | ||
return result | ||
} | ||
|
||
private fun params(joinPoint: JoinPoint): Map<*,*>? { | ||
val codeSignature = joinPoint.signature as CodeSignature | ||
val parameterNames = codeSignature.parameterNames | ||
val args = joinPoint.args | ||
val params: MutableMap<String,Any> = HashMap() | ||
|
||
for (i in parameterNames.indices) { | ||
params[parameterNames[i]] = args[i] | ||
} | ||
return params | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...kotlin/team/msg/common/DataInitializer.kt → ...n/team/msg/common/init/DataInitializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters