http://jira.mfd.msk:8088/browse/BKI-3215 Changed log lvl to warn for error checking; Added SetAndClearMdc annotation for packageName logging
This commit is contained in:
parent
2c3b2822c2
commit
061c24b89c
6 changed files with 88 additions and 5 deletions
|
|
@ -0,0 +1,19 @@
|
||||||
|
package ru.nbch.credit_tracker.config.mdc;
|
||||||
|
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnClass(Aspect.class)
|
||||||
|
@EnableAspectJAutoProxy(proxyTargetClass = true)
|
||||||
|
public class AopMdcConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MdcAspect mdcAspect() {
|
||||||
|
return new MdcAspect();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
package ru.nbch.credit_tracker.config.mdc;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.slf4j.MDC;
|
||||||
|
import ru.nbch.credit_tracker.entities.dto.UserPackage;
|
||||||
|
import ru.nbch.credit_tracker.entities.user_request.monitoring_request.MonitoringRequest;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Aspect
|
||||||
|
public class MdcAspect {
|
||||||
|
|
||||||
|
// For methods that set an MDC value (from a method argument) and then clear it at the end
|
||||||
|
@Around("@annotation(setAndClearMdc)")
|
||||||
|
public Object setAndClearMdcAdvice(ProceedingJoinPoint pjp, SetAndClearMdc setAndClearMdc) throws Throwable {
|
||||||
|
Object[] args = pjp.getArgs();
|
||||||
|
int index = setAndClearMdc.argIndex();
|
||||||
|
Object obj = (args != null && args.length > index) ? args[index] : null;
|
||||||
|
if (obj instanceof MonitoringRequest request) {
|
||||||
|
if (request.getPackageName() != null) {
|
||||||
|
putMdcValue(request.getPackageName(), setAndClearMdc.key());
|
||||||
|
}
|
||||||
|
} else if (obj instanceof UserPackage userPackage) {
|
||||||
|
if (userPackage.getPackageName() != null) {
|
||||||
|
putMdcValue(userPackage.getPackageName(), setAndClearMdc.key());
|
||||||
|
}
|
||||||
|
} else if (obj instanceof String packageName) {
|
||||||
|
putMdcValue(setAndClearMdc.key(), packageName);
|
||||||
|
} else {
|
||||||
|
log.debug("no packageName found for for {}", pjp.toShortString());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return pjp.proceed();
|
||||||
|
} finally {
|
||||||
|
MDC.remove(setAndClearMdc.key());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void putMdcValue(String value, String key) {
|
||||||
|
try {
|
||||||
|
MDC.put(key, "[" + value + "] ");
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error("{}", ExceptionUtils.getStackTrace(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
package ru.nbch.credit_tracker.config.mdc;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
// Annotation to set an MDC key based on a method argument and clear it after method execution
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
public @interface SetAndClearMdc {
|
||||||
|
String key() default "ru.nbch.credit_tracker.log.mdc.key";
|
||||||
|
// The index of the method argument to use for the MDC value (defaults to the first argument)
|
||||||
|
int argIndex() default 0;
|
||||||
|
}
|
||||||
|
|
@ -285,7 +285,7 @@ public class SignalService {
|
||||||
request -> {
|
request -> {
|
||||||
if (!request.getPersons().isEmpty()) {
|
if (!request.getPersons().isEmpty()) {
|
||||||
List<PhoneFidMap> subjects = request.getPersons().stream().map(p -> {
|
List<PhoneFidMap> subjects = request.getPersons().stream().map(p -> {
|
||||||
log.error("Ошибка при формировании субъекта. package_id = {}, id = {}. Описание: {}", packageId, p.getUid(), p.getValue());
|
log.warn("Ошибка при формировании субъекта. package_id = {}, id = {}. Описание: {}", packageId, p.getUid(), p.getValue());
|
||||||
return PhoneFidMap.builder()
|
return PhoneFidMap.builder()
|
||||||
.id(p.getUid())
|
.id(p.getUid())
|
||||||
.errorText(p.getValue())
|
.errorText(p.getValue())
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
<encoder>
|
<encoder>
|
||||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} %X{ru.nbch.credit_tracker.log.mdc.key} - %msg%n</pattern>
|
||||||
<charset>utf-8</charset>
|
<charset>utf-8</charset>
|
||||||
</encoder>
|
</encoder>
|
||||||
</appender>
|
</appender>
|
||||||
|
|
@ -22,7 +22,7 @@
|
||||||
<maxFileSize>100MB</maxFileSize>
|
<maxFileSize>100MB</maxFileSize>
|
||||||
</triggeringPolicy>
|
</triggeringPolicy>
|
||||||
<encoder>
|
<encoder>
|
||||||
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} %X{ru.nbch.credit_tracker.log.mdc.key} - %msg%n</pattern>
|
||||||
<charset>utf-8</charset>
|
<charset>utf-8</charset>
|
||||||
</encoder>
|
</encoder>
|
||||||
</appender>
|
</appender>
|
||||||
|
|
|
||||||
|
|
@ -69,12 +69,12 @@ public class ValidationUtilTest {
|
||||||
Arguments.of(
|
Arguments.of(
|
||||||
"/monitoring_request/invalid_phone_request.zip",
|
"/monitoring_request/invalid_phone_request.zip",
|
||||||
SignalError.ERROR_003,
|
SignalError.ERROR_003,
|
||||||
Collections.emptyList().toArray(new Object[0])
|
new Object[]{"abc123"}
|
||||||
),
|
),
|
||||||
Arguments.of(
|
Arguments.of(
|
||||||
"/monitoring_request/invalid_id_request.zip",
|
"/monitoring_request/invalid_id_request.zip",
|
||||||
SignalError.ERROR_010,
|
SignalError.ERROR_010,
|
||||||
Collections.emptyList().toArray(new Object[0])
|
new Object[]{"#####"}
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue