Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

로그남길수있게하는 커스텀 어노테이션을 만들었습니다 #18

Merged
merged 3 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.msg.gauth.global.annotation.logger;

import java.lang.reflect.Field;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class InjectionLogger implements ApplicationListener<ApplicationStartedEvent> {

@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
ConfigurableApplicationContext ac = event.getApplicationContext();
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
for (String name : beanDefinitionNames) {
Object bean = ac.getBean(name);
String classPath = bean.getClass().getName();
Logger log = LoggerFactory.getLogger(classPath);
try {
if(!classPath.contains("gauth")){
continue;
}
Class<?> beanClass = Class.forName(classPath);
Field[] fields = beanClass.getDeclaredFields();
for (Field field : fields) {
log4k log4k = field.getDeclaredAnnotation(log4k.class);
if(log4k != null){
field.setAccessible(true);
field.set(bean, log);
}
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/msg/gauth/global/annotation/logger/log4k.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.msg.gauth.global.annotation.logger;


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface log4k {
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.msg.gauth.global.exception.handler;

import com.msg.gauth.global.annotation.logger.log4k;
import com.msg.gauth.global.exception.ErrorResponse;
import com.msg.gauth.global.exception.exceptions.BasicException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@log4k
Logger log;

@ExceptionHandler(BasicException.class)
public ResponseEntity<ErrorResponse> BasicExceptionHandler(HttpServletRequest request, BasicException ex){
Expand Down