All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.smilego.tenant.logging.redis.RedisLogAspect Maven / Gradle / Ivy

package io.smilego.tenant.logging.redis;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import io.smilego.tenant.TenantContext;
import io.smilego.tenant.util.LogBuilder;
import io.smilego.tenant.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Iterator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Aspect
@Component
public class RedisLogAspect implements Ordered {

    /*
        Interceptar os Caches e gerar LOG
    */
    @Around("@annotation(org.springframework.cache.annotation.Cacheable) || @annotation(org.springframework.cache.annotation.CacheEvict)")
    public Object interceptRedisListener(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Object proceed = null;
        ObjectMapper objectMapper = new ObjectMapper()
                .registerModule(new ParameterNamesModule())
                .registerModule(new Jdk8Module())
                .registerModule(new JavaTimeModule()); // new module, NOT JSR310Module
        LogBuilder logBuilder = LogBuilder.of()
                .header("========== Redis Listener ==========")
                .row("Tenant Context: ", TenantContext.getTenantId())
                .row("Method: ", methodSignature.getMethod().getName());
        try {
            proceed = joinPoint.proceed();
            Iterator it = Arrays.stream(methodSignature.getMethod().getAnnotations()).iterator();
            String cacheName = "";
            String cacheKey  = "";
            while(it.hasNext()) {
                Annotation annotation = it.next();
                if (annotation instanceof Cacheable) {
                    Cacheable cacheable = (Cacheable) annotation;
                    cacheName = Stream.of(String.join(",", cacheable.value())).collect(Collectors.joining(","));
                    cacheKey  = cacheable.key();
                } else if (annotation instanceof CacheEvict) {
                    CacheEvict cacheable = (CacheEvict) annotation;
                    cacheName = Stream.of(String.join(",", cacheable.value())).collect(Collectors.joining(","));
                    cacheKey  = cacheable.key();
                }
            }

            String cacheKeyValue = "";
            try {
                if(cacheKey.contains(".") && !cacheKey.contains("concat")){
                    String[] arrCacheKey = cacheKey.replace(".","-").split("-");
                    String nameFieldObject = arrCacheKey[arrCacheKey.length-1];
                    Object obj = joinPoint.getArgs()[0];
                    Field field = obj.getClass().getDeclaredField(nameFieldObject);
                    field.setAccessible(true);
                    Object result = field.get(obj);
                    cacheKeyValue = StringUtil.objectToString(result);
                } else {
                    cacheKeyValue = Arrays.stream(joinPoint.getArgs()).map(m->StringUtil.objectToString(m)).reduce("", String::concat);
                }
                log.debug(logBuilder
                        .row("Cache Name: ", cacheName)
                        .row("Cache Key: ", cacheKeyValue)
                        .build());
            } catch (Throwable e) {
                log.error(logBuilder
                        .row("Cache Name: ", cacheName)
                        .row("Error: ", e.getLocalizedMessage())
                        .build());
            }
        } catch (Throwable err) {
            log.error(logBuilder
                    .row("Error: ", err.getLocalizedMessage())
                    .build());
            throw err;
        }
        return proceed;
    }

    @Override
    public int getOrder() {
        return 5;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy