package atg.tools.dynunit.nucleus.logging;

import atg.nucleus.logging.DebugLogEvent;
import atg.nucleus.logging.ErrorLogEvent;
import atg.nucleus.logging.InfoLogEvent;
import atg.nucleus.logging.LogEvent;
import atg.nucleus.logging.LogListener;
import atg.nucleus.logging.TraceLogEvent;
import atg.nucleus.logging.WarningLogEvent;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * Simple LogListener that translates log events to Log4j2.
 *
 * @author msicker
 * @version 1.0.0
 */
public class ApacheLogListener
        implements LogListener {

    @Override
    public void logEvent(final LogEvent logEvent) {
        if (logEvent == null) {
            return;
        }
        log(
                getLoggerForEvent(logEvent),
                getLoggingLevelForEvent(logEvent),
                getLogMessageForEvent(logEvent),
                getExceptionForEvent(logEvent)
        );
    }

    private static Logger getLoggerForEvent(final LogEvent logEvent) {
        final String originator = logEvent.getOriginator();
        return originator == null ? LogManager.getRootLogger() : LogManager.getLogger(originator);
    }

    private static Level getLoggingLevelForEvent(final LogEvent logEvent) {
        if (logEvent instanceof TraceLogEvent) {
            return Level.TRACE;
        }
        if (logEvent instanceof DebugLogEvent) {
            return Level.DEBUG;
        }
        if (logEvent instanceof InfoLogEvent) {
            return Level.INFO;
        }
        if (logEvent instanceof WarningLogEvent) {
            return Level.WARN;
        }
        if (logEvent instanceof ErrorLogEvent) {
            return Level.ERROR;
        }
        return Level.FATAL;
    }

    private static String getLogMessageForEvent(final LogEvent logEvent) {
        return logEvent.getMessage();
    }

    private static Throwable getExceptionForEvent(final LogEvent logEvent) {
        return logEvent.getThrowable();
    }

    private static void log(final Logger logger,
                            final Level logLevel,
                            final String logMessage,
                            final Throwable exception) {
        if (exception == null) {
            logger.log(logLevel, logMessage);
        }
        else {
            logger.log(logLevel, logMessage, exception);
        }
    }
}

@@ -113,32 +113,89 @@
 .ErrorLogEvent;%0A
+import atg.nucleus.logging.ExternalLogSystemLogListener;%0A
 import atg.nucle
@@ -653,16 +653,53 @@
         
+extends ExternalLogSystemLogListener%0A
 implemen
@@ -856,28 +856,29 @@
     
-log(%0A               
+final Logger logger =
  get
@@ -893,33 +893,33 @@
 rEvent(logEvent)
-,
+;
 %0A               
@@ -907,31 +907,43 @@
 t);%0A        
-       
+final Level level =
  getLoggingL
@@ -968,114 +968,169 @@
 ent)
-,%0A                getLogMessageForEvent(logEvent),%0A                getExceptionForEvent(logEvent)%0A        
+;%0A        final String message = logEvent.getMessage();%0A        final Throwable exception = logEvent.getThrowable();%0A        logger.log(level, message, exception
 );%0A 
@@ -1147,23 +1147,16 @@
 private 
-static 
 Logger g
@@ -1233,16 +1233,49 @@
 nator = 
+getPseudoClassNameForNucleusPath(
 logEvent
@@ -1290,16 +1290,17 @@
 inator()
+)
 ;%0A      
@@ -1311,58 +1311,8 @@
 turn
- originator == null ? LogManager.getRootLogger() :
  Log
@@ -1361,23 +1361,16 @@
 private 
-static 
 Level ge
@@ -1481,24 +1481,59 @@
       return
+ isUseInfoForDebug() ? Level.INFO :
  Level.TRACE
@@ -1607,24 +1607,59 @@
       return
+ isUseInfoForDebug() ? Level.INFO :
  Level.DEBUG
@@ -1967,645 +1967,13 @@
 vel.
-FATAL;%0A    %7D%0A%0A    private static String getLogMessageForEvent(final LogEvent logEvent) %7B%0A        return logEvent.getMessage();%0A    %7D%0A%0A    private static Throwable getExceptionForEvent(final LogEvent logEvent) %7B%0A        return logEvent.getThrowable();%0A    %7D%0A%0A    private static void log(final Logger logger,%0A                            final Level logLevel,%0A                            final String logMessage,%0A                            final Throwable exception) %7B%0A        if (exception == null) %7B%0A            logger.log(logLevel, logMessage);%0A        %7D%0A        else %7B%0A            logger.log(logLevel, logMessage, exception);%0A        %7D
+INFO;
 %0A   

Simplify ApacheLogListener.