package io.github.mfulton26.ctrl4testng;

import com.google.common.base.Function;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.ITestResult;
import org.testng.Reporter;

/**
 * This {@link Logger} simply loads {@link Logger}s associated with {@link ITestResult}s using a {@link CacheLoader} and
 * then delegates calls to the loaded {@link Logger}.
 *
 * @author Mark Fulton
 */
class CurrentTestResultLogger extends ForwardingLogger {

    private final LoadingCache<ITestResult, Logger> loadingCache = CacheBuilder.newBuilder()
        .build(CacheLoader.from(new Function<ITestResult, Logger>() {
            private final TestResultToLoggerNameFunction testResultToLoggerNameFunction =
                new TestResultToLoggerNameFunction();

            @Override
            public Logger apply(ITestResult input) {
                return LoggerFactory.getLogger(testResultToLoggerNameFunction.apply(input));
            }
        }));

    @Override
    protected Logger getLogger() {
        return loadingCache.getUnchecked(Reporter.getCurrentTestResult());
    }

}

@@ -39,182 +39,8 @@
 g;%0A%0A
-import com.google.common.base.Function;%0Aimport com.google.common.cache.CacheBuilder;%0Aimport com.google.common.cache.CacheLoader;%0Aimport com.google.common.cache.LoadingCache;%0A
 impo
@@ -249,42 +249,11 @@
 t%7Ds 
-using a %7B@link CacheLoader%7D and%0A *
+and
  the
@@ -276,16 +276,19 @@
 s to the
+%0A *
  loaded 
@@ -400,174 +400,81 @@
    p
-rivate final LoadingCache%3CITestResult, Logger%3E loadingCache = CacheBuilder.newBuilder()%0A        .build(CacheLoader.from(new Function%3CITestResult, Logger%3E() %7B%0A        
+ublic static final String LOGGER_ATTRIBUTE_NAME = %22ctrl4testng.logger%22;%0A%0A
     
@@ -551,24 +551,16 @@
 ction =%0A
-        
         
@@ -598,24 +598,16 @@
 ion();%0A%0A
-        
     @Ove
@@ -620,30 +620,54 @@
     
- 
+protected Logger getLogger() %7B%0A
        
-public
  Logger 
 appl
@@ -666,57 +666,301 @@
 ger 
-apply(ITestResult input) %7B%0A                return
+logger;%0A        ITestResult testResult = Reporter.getCurrentTestResult();%0A        Object attributeValue = testResult.getAttribute(LOGGER_ATTRIBUTE_NAME);%0A        if (attributeValue instanceof Logger) %7B%0A            logger = (Logger) attributeValue;%0A        %7D%0A        else %7B%0A            logger =
  Log
@@ -1021,12 +1021,17 @@
 ply(
-inpu
+testResul
 t));
@@ -1047,146 +1047,95 @@
     
-%7D%0A        %7D));%0A%0A    @Override%0A    protected Logger getLogger() %7B%0A        return loadingCache.getUnchecked(Reporter.getCurrentTestResult())
+testResult.setAttribute(LOGGER_ATTRIBUTE_NAME, logger);%0A        %7D%0A        return logger
 ;%0A  

Use ITestResult attributes instead of a LoadingCache