陷入了一個(gè)奇怪的要求.我需要在log4j消息上附加唯一的錯(cuò)誤ID并將該消息ID返回到接口.因此,盡管如此,我還是創(chuàng)建了一個(gè)spring服務(wù),如下所示
public class LoggingService {
protected static Logger logger = LoggerFactory.getLogger(LoggingService.class);
public String debug(String debug_msg)
{
String uniqueMsgId = generateUniqueId();
logger.debug(concatIdWithMsg(uniqueMsgId, debug_msg));
return uniqueMsgId;
}
}
并將其自動(dòng)連線到我需要的任何地方.
public class LoginLogoutController {
@Autowired
LoggingService logger;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String getLoginPage()
{
logger.debug("Login page requested");
}
}
盡管它工作正常,但記錄器msg中的源類是LoggingService,這很明顯.我想要的是傳遞LoggingService自動(dòng)連接的類,以便記錄器消息顯示問題的原始來源.我試圖以某種方式更改服務(wù) 但不知道如何傳遞源類
public class LoggingService<T> {
protected static Logger logger = null;
Class<T> sourceClass;
public void construct(Class<T> sourceClass)
{
this.sourceClass = sourceClass;
logger = LoggerFactory.getLogger(sourceClass);
}
public String debug(String debug_msg)
{
String uniqueMsgId = generateUniqueId();
logger.debug(concatIdWithMsg(uniqueMsgId, debug_msg));
return null;
}
}
解決方法: 我使用這種機(jī)制來注入記錄器.
創(chuàng)建此注釋.
/**
* Indicates InjectLogger of appropriate type to
* be supplied at runtime to the annotated field.
*
* The injected logger is an appropriate implementation
* of org.slf4j.Logger.
*/
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(FIELD)
@Documented
public @interface InjectLogger {
}
現(xiàn)在讓我們定義一個(gè)實(shí)際上完成注入記錄器實(shí)現(xiàn)工作的類.
/**
* Auto injects the underlying implementation of logger into the bean with field
* having annotation <code>InjectLogger</code>.
*
*/
import java.lang.reflect.Field;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.ReflectionUtils;
import static org.springframework.util.ReflectionUtils.FieldCallback;
public class LoggerInjector implements BeanPostProcessor {
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
public Object postProcessBeforeInitialization(final Object bean,
String beanName) throws BeansException {
ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {
public void doWith(Field field) throws IllegalArgumentException,
IllegalAccessException {
// make the field accessible if defined private
ReflectionUtils.makeAccessible(field);
if (field.getAnnotation(InjectLogger.class) != null) {
Logger log = LoggerFactory.getLogger(bean.getClass());
field.set(bean, log);
}
}
});
return bean;
}
}
使用它甚至更簡單.只需將上面創(chuàng)建的Logger批注添加到所需類的Log字段中即可.
import org.slf4j.Logger;
public class Demo {
@InjectLogger
private Logger log;
public void doSomething() {
log.info("message");
log.error("Lets see how the error message looks...");
}
}
來源:https://www./content-4-532051.html
|