Search in sources :

Example 1 with Log4jLoggerAdapter

use of org.slf4j.impl.Log4jLoggerAdapter in project alluxio by Alluxio.

the class LogUtils method setLogLevel.

/**
 * Gets a logger's level with specify name, if the level argument is not null, it will set to
 * specify level first.
 * @param logName logger's name
 * @param level logger's level
 * @return an entity object about the log info
 * @throws IOException if an I/O error occurs
 */
public static LogInfo setLogLevel(String logName, String level) throws IOException {
    LogInfo result = new LogInfo();
    if (StringUtils.isNotBlank(logName)) {
        result.setLogName(logName);
        Log log = LogFactory.getLog(logName);
        Logger logger = LoggerFactory.getLogger(logName);
        if (log instanceof Log4JLogger) {
            process(((Log4JLogger) log).getLogger(), level, result);
        } else if (log instanceof Jdk14Logger) {
            process(((Jdk14Logger) log).getLogger(), level, result);
        } else if (logger instanceof Log4jLoggerAdapter) {
            try {
                Field field = Log4jLoggerAdapter.class.getDeclaredField("logger");
                field.setAccessible(true);
                org.apache.log4j.Logger log4jLogger = (org.apache.log4j.Logger) field.get(logger);
                process(log4jLogger, level, result);
            } catch (NoSuchFieldException | IllegalAccessException e) {
                result.setMessage(e.getMessage());
            }
        } else {
            result.setMessage("Sorry, " + log.getClass() + " not supported.");
        }
    } else {
        result.setMessage("Please specify a correct logName.");
    }
    return result;
}
Also used : LogInfo(alluxio.wire.LogInfo) Jdk14Logger(org.apache.commons.logging.impl.Jdk14Logger) Log(org.apache.commons.logging.Log) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) Logger(org.slf4j.Logger) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) Jdk14Logger(org.apache.commons.logging.impl.Jdk14Logger) Log4jLoggerAdapter(org.slf4j.impl.Log4jLoggerAdapter) Field(java.lang.reflect.Field)

Aggregations

LogInfo (alluxio.wire.LogInfo)1 Field (java.lang.reflect.Field)1 Log (org.apache.commons.logging.Log)1 Jdk14Logger (org.apache.commons.logging.impl.Jdk14Logger)1 Log4JLogger (org.apache.commons.logging.impl.Log4JLogger)1 Logger (org.slf4j.Logger)1 Log4jLoggerAdapter (org.slf4j.impl.Log4jLoggerAdapter)1