/*
 * Copyright 2014 Red Hat, Inc, and individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.projectodd.wunderboss;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import org.slf4j.Logger;

public class LogbackUtil {

    public static void setLogLevel(Logger logger, String level) {
        ((ch.qos.logback.classic.Logger)logger)
                .setLevel(Level.toLevel(level));
    }

    public static void configureLogback(Object rawContext) {
        if (!(rawContext instanceof LoggerContext)) {
            return;
        }

        LoggerContext context = (LoggerContext)rawContext;
        ClassLoader cl = LogbackUtil.class.getClassLoader();
        //TODO: see if there is a better way to determine if logback will self-configure
        if (System.getProperty("logback.configurationFile") == null &&
                cl.getResource("logback.xml") == null &&
                cl.getResource("logback.groovy") == null) {
            context.reset();
            JoranConfigurator configurator = new JoranConfigurator();
            configurator.setContext(context);
            try {
                configurator.doConfigure(cl.getResource("logback-default.xml"));
            } catch (JoranException e) {
                throw new RuntimeException("Failed to load default logging configuration", e);
            }
        }
    }
}

@@ -768,30 +768,30 @@
 sic.
-joran.JoranConfigurato
+util.ContextInitialize
 r;%0Ai
@@ -868,16 +868,38 @@
 ogger;%0A%0A
+import java.net.URL;%0A%0A
 public c
@@ -1164,10 +1164,8 @@
  if 
-(!
 (raw
@@ -1201,43 +1201,15 @@
 ext)
-)
  %7B%0A    
-        return;%0A        %7D%0A%0A
     
@@ -1275,191 +1275,123 @@
     
-ClassLoader cl = LogbackUtil.class.getClassLoader();%0A        //TODO: see if there is a better way to determine if logback will self-configure%0A        if (System.getProperty(%22logback.c
+    ContextInitializer initializer = new ContextInitializer(context);%0A            if (initializer.findURLOfDefaultC
 onfi
@@ -1402,17 +1402,22 @@
 tionFile
-%22
+(false
 ) == nul
@@ -1409,35 +1409,35 @@
 e(false) == null
- &&
+) %7B
 %0A               
@@ -1442,253 +1442,136 @@
    c
-l.getResource(%22logback.xml%22) == null &&%0A                cl.getResource(%22logback.groovy%22) == null) %7B%0A            context.reset();%0A            JoranConfigurator configurator = new JoranConfigurator();%0A            configurator.setContext(context);%0A
+ontext.reset();%0A                URL defaultConfig = context.getClass().getClassLoader().getResource(%22logback-default.xml%22);%0A    
     
@@ -1604,32 +1604,33 @@
     
-configurator.doC
+    initializer.c
 onfigure
 (cl.
@@ -1629,15 +1629,10 @@
 gure
-(cl.get
+By
 Reso
@@ -1640,33 +1640,28 @@
 rce(
-%22logback-default.xml%22));%0A
+defaultConfig);%0A    
     
@@ -1693,24 +1693,28 @@
 eption e) %7B%0A
+    
             
@@ -1796,16 +1796,34 @@
 n%22, e);%0A
+                %7D%0A
         

Simplify LogBack configuration.