Search in sources :

Example 16 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project graylog2-server by Graylog2.

the class LoggersResource method setLoggerLevel.

@VisibleForTesting
protected void setLoggerLevel(final String loggerName, final Level level) {
    final LoggerContext context = (LoggerContext) LogManager.getContext(false);
    final Configuration config = context.getConfiguration();
    final LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
    if (loggerName.equals(loggerConfig.getName())) {
        loggerConfig.setLevel(level);
    } else {
        final LoggerConfig newLoggerConfig = new LoggerConfig(loggerName, level, loggerConfig.isAdditive());
        newLoggerConfig.setLevel(level);
        config.addLogger(loggerName, newLoggerConfig);
    }
    context.updateLoggers();
}
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) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 17 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project graylog2-server by Graylog2.

the class CmdLineTool method addInstrumentedAppender.

private void addInstrumentedAppender(final MetricRegistry metrics, final Level level) {
    final InstrumentedAppender appender = new InstrumentedAppender(metrics, null, null, false);
    appender.start();
    final LoggerContext context = (LoggerContext) LogManager.getContext(false);
    final org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration();
    config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).addAppender(appender, level, null);
    context.updateLoggers(config);
}
Also used : InstrumentedAppender(com.codahale.metrics.log4j2.InstrumentedAppender) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 18 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext 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 19 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext 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 20 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext 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)

Aggregations

LoggerContext (org.apache.logging.log4j.core.LoggerContext)156 Configuration (org.apache.logging.log4j.core.config.Configuration)53 Test (org.junit.Test)33 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)32 Appender (org.apache.logging.log4j.core.Appender)15 File (java.io.File)12 IOException (java.io.IOException)11 Logger (org.apache.logging.log4j.Logger)11 BeforeClass (org.junit.BeforeClass)11 Map (java.util.Map)9 Level (org.apache.logging.log4j.Level)8 LogEvent (org.apache.logging.log4j.core.LogEvent)8 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)7 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)7 Logger (org.apache.logging.log4j.core.Logger)6 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)5 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)5 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)5 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)4 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)4