package lb.examples.karaf.jpa.eclipselink;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.persistence.logging.AbstractSessionLog;
import org.eclipse.persistence.logging.SessionLog;
import org.eclipse.persistence.logging.SessionLogEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 *
 */
public class EclipseLinkSessionLogger extends AbstractSessionLog implements SessionLog
{
    private static final Logger LOGGER  = LoggerFactory.getLogger("ECLIPSELINK");
    private static final Map<String,Logger> LOGGERS = new HashMap<String,Logger>();

    /**
     * c-tor
     */
    public EclipseLinkSessionLogger()
    {
        this(1);
    }

    /**
     * c-tor
     *
     * @param level
     */
    public EclipseLinkSessionLogger(
        int level)
    {
        setLevel(1);
    }

    /**
     *
     */
    @Override
    public void log(
        SessionLogEntry sle)
    {
        Logger lg  = getLogger(sle);
        String msg = getMessage(sle);

        if(lg != null && StringUtils.isNotBlank(msg)) {
            switch(sle.getLevel()) {
                case SessionLog.SEVERE  : lg.error(msg); break;
                case SessionLog.WARNING : lg.warn (msg); break;
                case SessionLog.INFO    : lg.info (msg); break;
                case SessionLog.FINE    : lg.debug(msg); break;
                case SessionLog.FINER   : lg.trace(msg); break;
                case SessionLog.FINEST  : lg.trace(msg); break;
                case SessionLog.ALL     : lg.trace(msg); break;
                default                 : lg.debug(msg); break;
            }
        }
    }

    /**
     *
     * @param sle
     * @return
     */
    private String getMessage(
        SessionLogEntry sle)
    {
        StringBuilder msg = new StringBuilder();
        //msg.append(getSupplementDetailString(sle))
        msg.append(formatMessage(sle));

        return msg.toString();
    }

    /**
     *
     * @param sle
     * @return
     */
    private Logger getLogger(
        SessionLogEntry sle)
    {
        String ns = sle.getNameSpace();
        if(StringUtils.isNotBlank(ns)) {
            ns = ns.toUpperCase();

            if(!LOGGERS.containsKey(ns)) {
                LOGGERS.put(ns,LoggerFactory.getLogger("JPA-" + ns));
            }

            return LOGGERS.get(ns);
        }

        return LOGGER;
    }
}

@@ -688,23 +688,19 @@
 Logger()
-%0A   
  %7B%0A
+
         
@@ -807,25 +807,16 @@
 nLogger(
-%0A        
 int leve
@@ -817,20 +817,16 @@
 t level)
-%0A   
  %7B%0A     
@@ -905,25 +905,16 @@
 oid log(
-%0A        
 SessionL
@@ -917,36 +917,32 @@
 ionLogEntry sle)
-%0A   
  %7B%0A        Logge
@@ -1726,25 +1726,16 @@
 Message(
-%0A        
 SessionL
@@ -1738,36 +1738,32 @@
 ionLogEntry sle)
-%0A   
  %7B%0A        Strin
@@ -2015,25 +2015,16 @@
 tLogger(
-%0A        
 SessionL
@@ -2035,20 +2035,16 @@
 try sle)
-%0A   
  %7B%0A     

add data service