Search in sources :

Example 16 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project geode by apache.

the class LogService method restoreConsoleAppender.

/**
   * Restores STDOUT ConsoleAppender to ROOT logger. Only called when using the log4j2-default.xml
   * configuration. This is done when the LogWriterAppender for log-file is destroyed. The Appender
   * instance stored in stdoutAppender is used.
   */
public static synchronized void restoreConsoleAppender() {
    if (stdoutAppender == null) {
        return;
    }
    final AppenderContext appenderContext = LogService.getAppenderContext(LogService.ROOT_LOGGER_NAME);
    final LoggerConfig config = appenderContext.getLoggerConfig();
    Appender stdout = config.getAppenders().get(STDOUT);
    if (stdout == null) {
        config.addAppender(stdoutAppender, Level.ALL, null);
        appenderContext.getLoggerContext().updateLoggers();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) AppenderContext(org.apache.geode.internal.logging.log4j.AppenderContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 17 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project geode by apache.

the class Configurator method getLoggerConfig.

public static LoggerConfig getLoggerConfig(final String name) {
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    Configuration config = context.getConfiguration();
    LoggerConfig logConfig = config.getLoggerConfig(name);
    if (!logConfig.getName().equals(name)) {
        throw new IllegalStateException("LoggerConfig does not exist for " + name);
    }
    return logConfig;
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 18 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project geode by apache.

the class Configurator method setLevel.

public static void setLevel(String name, Level level) {
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    LoggerConfig logConfig = getLoggerConfig(name);
    logConfig.setLevel(level);
    context.updateLoggers();
    if (level.isLessSpecificThan(Level.DEBUG)) {
        LogService.configureFastLoggerDelegating();
    }
}
Also used : LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 19 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project ignite by apache.

the class Log4J2Logger method createConsoleLogger.

/**
     * Creates console appender with some reasonable default logging settings.
     *
     * @return Logger with auto configured console appender.
     */
public static Logger createConsoleLogger() {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(true);
    Configuration cfg = ctx.getConfiguration();
    PatternLayout.Builder builder = PatternLayout.newBuilder();
    builder.withPattern("%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n").withCharset(Charset.defaultCharset()).withAlwaysWriteExceptions(false).withNoConsoleNoAnsi(false);
    PatternLayout layout = builder.build();
    ConsoleAppender.Builder consoleAppenderBuilder = ConsoleAppender.newBuilder();
    consoleAppenderBuilder.withName(CONSOLE_APPENDER).withLayout(layout);
    ConsoleAppender consoleApp = consoleAppenderBuilder.build();
    consoleApp.start();
    AppenderRef ref = AppenderRef.createAppenderRef(CONSOLE_APPENDER, Level.TRACE, null);
    AppenderRef[] refs = { ref };
    LoggerConfig logCfg = LoggerConfig.createLogger(false, Level.INFO, LogManager.ROOT_LOGGER_NAME, "", refs, null, null, null);
    logCfg.addAppender(consoleApp, null, null);
    cfg.addAppender(consoleApp);
    cfg.addLogger(LogManager.ROOT_LOGGER_NAME, logCfg);
    ctx.updateLoggers(cfg);
    return (Logger) LogManager.getContext().getLogger(LogManager.ROOT_LOGGER_NAME);
}
Also used : ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef) IgniteLogger(org.apache.ignite.IgniteLogger) Logger(org.apache.logging.log4j.core.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 20 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project logging-log4j2 by apache.

the class ConfigurationAssemblerTest method validate.

private void validate(final Configuration config) {
    assertNotNull(config);
    assertNotNull(config.getName());
    assertFalse(config.getName().isEmpty());
    assertNotNull("No configuration created", config);
    assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED);
    final Map<String, Appender> appenders = config.getAppenders();
    assertNotNull(appenders);
    assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 2);
    final KafkaAppender kafkaAppender = (KafkaAppender) appenders.get("Kafka");
    final GelfLayout gelfLayout = (GelfLayout) kafkaAppender.getLayout();
    final Map<String, LoggerConfig> loggers = config.getLoggers();
    assertNotNull(loggers);
    assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2);
    final LoggerConfig rootLoggerConfig = loggers.get("");
    assertEquals(Level.ERROR, rootLoggerConfig.getLevel());
    assertFalse(rootLoggerConfig.isIncludeLocation());
    final LoggerConfig loggerConfig = loggers.get("org.apache.logging.log4j");
    assertEquals(Level.DEBUG, loggerConfig.getLevel());
    assertTrue(loggerConfig.isIncludeLocation());
    final Filter filter = config.getFilter();
    assertNotNull("No Filter", filter);
    assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter);
    final List<CustomLevelConfig> customLevels = config.getCustomLevels();
    assertNotNull("No CustomLevels", filter);
    assertEquals(1, customLevels.size());
    final CustomLevelConfig customLevel = customLevels.get(0);
    assertEquals("Panic", customLevel.getLevelName());
    assertEquals(17, customLevel.getIntLevel());
    final Logger logger = LogManager.getLogger(getClass());
    logger.info("Welcome to Log4j!");
}
Also used : KafkaAppender(org.apache.logging.log4j.core.appender.mom.kafka.KafkaAppender) Appender(org.apache.logging.log4j.core.Appender) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) Filter(org.apache.logging.log4j.core.Filter) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) KafkaAppender(org.apache.logging.log4j.core.appender.mom.kafka.KafkaAppender) GelfLayout(org.apache.logging.log4j.core.layout.GelfLayout) Logger(org.apache.logging.log4j.Logger) CustomLevelConfig(org.apache.logging.log4j.core.config.CustomLevelConfig) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Aggregations

LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)63 Configuration (org.apache.logging.log4j.core.config.Configuration)38 LoggerContext (org.apache.logging.log4j.core.LoggerContext)32 Appender (org.apache.logging.log4j.core.Appender)11 Level (org.apache.logging.log4j.Level)9 Test (org.junit.Test)7 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)6 Logger (org.apache.logging.log4j.Logger)5 Filter (org.apache.logging.log4j.core.Filter)5 Configuration.getConfiguration (javax.security.auth.login.Configuration.getConfiguration)4 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)4 ThresholdFilter (org.apache.logging.log4j.core.filter.ThresholdFilter)4 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)3 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)3 PropertyChangeListener (java.beans.PropertyChangeListener)2 File (java.io.File)2 Path (java.nio.file.Path)2 Map (java.util.Map)2 AppenderContext (org.apache.geode.internal.logging.log4j.AppenderContext)2