com.cinchapi.common.logging.Logging Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of accent4j Show documentation
Show all versions of accent4j Show documentation
Accent4J is a suite of libraries, helpers and data structures that make Java programming idioms more fluent.
/*
* 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 {
/**
* Disable ALL logging from {@code clazz}
*
* @param clazz the {@link Class} for which logging should be disabled
*/
public static void disable(Class> clazz) {
disable(clazz.getName());
}
/**
* Disable ALL logging from the {@code name}d parameter.
*
* @param clazz the name of the Logger to disable
*/
public static void disable(String name) {
Logger logger = LoggerFactory.getLogger(name);
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);
}
}
/**
* A cache of the object that is passed to the {@code setLevel} method of
* the appropriate Logger.
*/
static Map levelCache = Maps.newHashMap();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy