package com.github.schnitker.logmgr.logback;

import java.text.MessageFormat;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;

public class JulLoggerWrapper extends java.util.logging.Logger {

    private final Logger logger;

    /**
     * Constructs LoggerWrapper from Log4j logger.
     * 
     * @param logger
     *            the Logback logger
     */
    protected JulLoggerWrapper(final Logger logger) {
        super(logger.getName(), null);
        this.logger = logger;

        super.setLevel(JulLevels.toJavaLevel(this.logger.getEffectiveLevel()));
    }

    @Override
    public void log(final java.util.logging.LogRecord record) {
        if (record == null || record.getMessage() == null) {
            return;
        }
        final Level level = JulLevels.toLogbackLevel(record.getLevel());
        if (this.logger.isEnabledFor(level)) {
            String message = record.getMessage();
            try {
                Object parameters[] = record.getParameters();
                if (parameters != null && parameters.length != 0) {
                    message = MessageFormat.format(message, parameters);
                }
            } catch (Exception ex) {
                // ignore parameter error
            }
            this.logger.log(null, Logger.FQCN, level.levelInt, message, null, record.getThrown());
        }
    }

    @Override
    public void setLevel(final java.util.logging.Level newLevel) throws SecurityException {

        logger.setLevel(JulLevels.toLogbackLevel(newLevel));
        super.setLevel(newLevel);
    }

    @Override
    public java.util.logging.Level getLevel() {
        return JulLevels.toJavaLevel(this.logger.getLevel());
    }

    @Override
    public boolean isLoggable(final java.util.logging.Level level) {
        return this.logger.isEnabledFor(JulLevels.toLogbackLevel(level));
    }

    @Override
    public String getName() {
        return this.logger.getName();
    }

    // ================================================================
    // all other method should check the logback level
    // ================================================================

    @Override
    public void log(java.util.logging.Level level, String msg) {
        this.logger.log(null, Logger.FQCN, JulLevels.toLogbackLevel(level).levelInt, msg, null, null);
    }

    @Override
    public void log(java.util.logging.Level level, String msg, Object param1) {
        Level logbackLevel = JulLevels.toLogbackLevel(level);

        if (this.logger.isEnabledFor(logbackLevel)) {
            String fmtMsg = MessageFormat.format(msg, param1);
            this.logger.log(null, Logger.FQCN, JulLevels.toLogbackLevel(level).levelInt, fmtMsg, null, null);
        }
    }

    @Override
    public void log(java.util.logging.Level level, String msg, Object params[]) {
        Level log4jLevel = JulLevels.toLogbackLevel(level);

        if (this.logger.isEnabledFor(log4jLevel)) {
            String fmtMsg = MessageFormat.format(msg, params);
            this.logger.log(null, Logger.FQCN, JulLevels.toLogbackLevel(level).levelInt, fmtMsg, null, null);
        }
    }

    @Override
    public void log(java.util.logging.Level level, String msg, Throwable thrown) {
        this.logger.log(null, Logger.FQCN, JulLevels.toLogbackLevel(level).levelInt, msg, null, thrown);
    }

    @Override
    public void throwing(String sourceClass, String sourceMethod, Throwable thrown) {
        this.logger.error(sourceClass + '.' + sourceMethod, thrown);
    }

    @Override
    public void severe(String msg) {
        this.logger.error(msg);
    }

    @Override
    public void warning(String msg) {
        this.logger.warn(msg);
    }

    @Override
    public void info(String msg) {
        this.logger.info(msg);
    }

    @Override
    public void config(String msg) {
        this.logger.debug(msg);
    }

    @Override
    public void fine(String msg) {
        this.logger.debug(msg);
    }

    @Override
    public void finer(String msg) {
        this.logger.trace(msg);
    }

    @Override
    public void finest(String msg) {
        this.logger.trace(msg);
    }
}
@@ -1314,22 +1314,49 @@
 CN, 
-l
+L
 evel.
+toLocationAwareLoggerInteger(
 level
-Int
+)
 , me
@@ -2270,32 +2270,94 @@
 , String msg) %7B%0A
+        Level logbackLevel = JulLevels.toLogbackLevel(level);%0A
         this.log
@@ -2387,48 +2387,56 @@
 CN, 
-Jul
 Level
-s
 .toLo
-gbackLevel(level).levelInt
+cationAwareLoggerInteger(logbackLevel)
 , ms
@@ -2606,32 +2606,40 @@
 ckLevel(level);%0A
+        
 %0A        if (thi
@@ -2791,48 +2791,56 @@
 CN, 
-Jul
 Level
-s
 .toLo
-gbackLevel(level).levelInt
+cationAwareLoggerInteger(logbackLevel)
 , fm
@@ -2988,18 +2988,20 @@
 evel log
-4j
+back
 Level = 
@@ -3074,18 +3074,20 @@
 dFor(log
-4j
+back
 Level)) 
@@ -3202,48 +3202,56 @@
 CN, 
-Jul
 Level
-s
 .toLo
-gbackLevel(level).levelInt
+cationAwareLoggerInteger(logbackLevel)
 , fm
@@ -3375,32 +3375,103 @@
 wable thrown) %7B%0A
+        Level logbackLevel = JulLevels.toLogbackLevel(level);%0A        %0A
         this.log
@@ -3501,48 +3501,56 @@
 CN, 
-Jul
 Level
-s
 .toLo
-gbackLevel(level).levelInt
+cationAwareLoggerInteger(logbackLevel)
 , ms

fix logback