/*
 * Copyright (c) 2013-2016 Cinchapi Inc.
 * 
 * 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 com.cinchapi.common.logging;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cinchapi.common.reflect.Reflection;
import com.google.common.collect.Maps;

/**
 * A collection of utility methods for programmatically configuring logging
 * preferences.
 * 
 * @author Jeff Nelson
 */
public class Logging {

    /**
     * A cache of the object that is passed to the {@code setLevel} method of
     * the appropriate Logger.
     */
    static Map<String, Object> levelCache = Maps.newHashMap();

    /**
     * Disable ALL logging from {@code clazz}
     * 
     * @param clazz the {@link Class} for which logging should be disabled
     */
    public static void disable(Class<?> clazz) {
        Logger logger = LoggerFactory.getLogger(clazz);
        if(logger instanceof ch.qos.logback.classic.Logger) {
            ((ch.qos.logback.classic.Logger) logger)
                    .setLevel(ch.qos.logback.classic.Level.OFF);
        }
        else if(logger instanceof java.util.logging.Logger) {
            ((java.util.logging.Logger) logger)
                    .setLevel(java.util.logging.Level.OFF);
        }
        else {
            // This code is very brittle because it calls a lot of stuff using
            // reflection. If internal APIs change, this could go bad very
            // quickly. The moral of the story is that everyone should just use
            // Logback, but since that isn't the reality, we have to live with
            // this hack. In actuality, the real moral is that client side code
            // should never have logging statements, otherwise we wouldn't have
            // to disable them in the first place.
            String type = logger.getClass().getName();
            Object theLogger = Reflection.get("logger", logger);
            Object level = levelCache.get(type);
            if(level == null) {
                switch (type) {
                case "org.slf4j.impl.Log4jLoggerAdapter":
                default:
                    level = Reflection
                            .getStatic("OFF", Reflection
                                    .getClassCasted("org.apache.log4j.Level"));
                    break;
                }
                levelCache.put(type, level);
            }
            Reflection.call(theLogger, "setLevel", level);
        }
    }
}

@@ -974,180 +974,226 @@
   * 
-A cache of the object that is passed to the %7B@code setLevel%7D method of%0A     * the appropriate Logger.%0A     */%0A    static Map%3CString, Object%3E levelCache = Maps.newHashMap();
+Disable ALL logging from %7B@code clazz%7D%0A     * %0A     * @param clazz the %7B@link Class%7D for which logging should be disabled%0A     */%0A    public static void disable(Class%3C?%3E clazz) %7B%0A        disable(clazz.getName());%0A    %7D
 %0A%0A  
@@ -1226,37 +1226,52 @@
 ogging from 
+the 
 %7B@code 
-clazz%7D
+name%7Dd parameter.
 %0A     * %0A   
@@ -1295,57 +1295,37 @@
 the 
-%7B@link Class%7D for which logging should be
+name of the Logger to
  disable
 d%0A  
@@ -1312,33 +1312,32 @@
 ogger to disable
-d
 %0A     */%0A    pub
@@ -1356,38 +1356,35 @@
 oid disable(
-Class%3C?%3E clazz
+String name
 ) %7B%0A        
@@ -1423,21 +1423,20 @@
 tLogger(
-clazz
+name
 );%0A     
@@ -2697,37 +2697,8 @@
 tion
-%0A                            
 .get
@@ -2722,24 +2722,16 @@
 lection%0A
-        
         
@@ -2972,11 +2972,200 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * A cache of the object that is passed to the %7B@code setLevel%7D method of%0A     * the appropriate Logger.%0A     */%0A    static Map%3CString, Object%3E levelCache = Maps.newHashMap();
 %0A%7D%0A

add method to disable logging for named Loggers