Search in sources :

Example 1 with Log4jLogger

use of org.apache.logging.slf4j.Log4jLogger in project HikariCP by brettwooldridge.

the class TestElf method setSlf4jTargetStream.

static void setSlf4jTargetStream(Class<?> clazz, PrintStream stream) {
    try {
        Log4jLogger log4Jlogger = (Log4jLogger) LoggerFactory.getLogger(clazz);
        Field field = clazz.getClassLoader().loadClass("org.apache.logging.slf4j.Log4jLogger").getDeclaredField("logger");
        field.setAccessible(true);
        Logger logger = (Logger) field.get(log4Jlogger);
        if (logger.getAppenders().containsKey("string")) {
            Appender appender = logger.getAppenders().get("string");
            logger.removeAppender(appender);
        }
        logger.addAppender(new StringAppender("string", stream));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : AbstractAppender(org.apache.logging.log4j.core.appender.AbstractAppender) Appender(org.apache.logging.log4j.core.Appender) Field(java.lang.reflect.Field) Log4jLogger(org.apache.logging.slf4j.Log4jLogger) Logger(org.apache.logging.log4j.core.Logger) Log4jLogger(org.apache.logging.slf4j.Log4jLogger)

Example 2 with Log4jLogger

use of org.apache.logging.slf4j.Log4jLogger in project HikariCP by brettwooldridge.

the class TestElf method setSlf4jLogLevel.

static void setSlf4jLogLevel(Class<?> clazz, Level logLevel) {
    try {
        Log4jLogger log4Jlogger = (Log4jLogger) LoggerFactory.getLogger(clazz);
        Field field = clazz.getClassLoader().loadClass("org.apache.logging.slf4j.Log4jLogger").getDeclaredField("logger");
        field.setAccessible(true);
        Logger logger = (Logger) field.get(log4Jlogger);
        logger.setLevel(logLevel);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Field(java.lang.reflect.Field) Log4jLogger(org.apache.logging.slf4j.Log4jLogger) Logger(org.apache.logging.log4j.core.Logger) Log4jLogger(org.apache.logging.slf4j.Log4jLogger)

Aggregations

Field (java.lang.reflect.Field)2 Logger (org.apache.logging.log4j.core.Logger)2 Log4jLogger (org.apache.logging.slf4j.Log4jLogger)2 Appender (org.apache.logging.log4j.core.Appender)1 AbstractAppender (org.apache.logging.log4j.core.appender.AbstractAppender)1