Search in sources :

Example 1 with AppenderRef

use of org.apache.logging.log4j.core.config.AppenderRef 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 2 with AppenderRef

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

the class AsyncAppender method start.

@Override
public void start() {
    final Map<String, Appender> map = config.getAppenders();
    final List<AppenderControl> appenders = new ArrayList<>();
    for (final AppenderRef appenderRef : appenderRefs) {
        final Appender appender = map.get(appenderRef.getRef());
        if (appender != null) {
            appenders.add(new AppenderControl(appender, appenderRef.getLevel(), appenderRef.getFilter()));
        } else {
            LOGGER.error("No appender named {} was configured", appenderRef);
        }
    }
    if (errorRef != null) {
        final Appender appender = map.get(errorRef);
        if (appender != null) {
            errorAppender = new AppenderControl(appender, null, null);
        } else {
            LOGGER.error("Unable to set up error Appender. No appender named {} was configured", errorRef);
        }
    }
    if (appenders.size() > 0) {
        thread = new AsyncThread(appenders, queue);
        thread.setName("AsyncAppender-" + getName());
    } else if (errorRef == null) {
        throw new ConfigurationException("No appenders are available for AsyncAppender " + getName());
    }
    asyncQueueFullPolicy = AsyncQueueFullPolicyFactory.create();
    thread.start();
    super.start();
}
Also used : Appender(org.apache.logging.log4j.core.Appender) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) ArrayList(java.util.ArrayList) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef)

Example 3 with AppenderRef

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

the class RewriteAppender method start.

@Override
public void start() {
    for (final AppenderRef ref : appenderRefs) {
        final String name = ref.getRef();
        final Appender appender = config.getAppender(name);
        if (appender != null) {
            final Filter filter = appender instanceof AbstractAppender ? ((AbstractAppender) appender).getFilter() : null;
            appenders.put(name, new AppenderControl(appender, ref.getLevel(), filter));
        } else {
            LOGGER.error("Appender " + ref + " cannot be located. Reference ignored");
        }
    }
    super.start();
}
Also used : AbstractAppender(org.apache.logging.log4j.core.appender.AbstractAppender) Appender(org.apache.logging.log4j.core.Appender) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) Filter(org.apache.logging.log4j.core.Filter) AbstractAppender(org.apache.logging.log4j.core.appender.AbstractAppender) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef)

Example 4 with AppenderRef

use of org.apache.logging.log4j.core.config.AppenderRef in project Anserini by castorini.

the class TrainingDataGenerator method createNewLoggerConfig.

/**
   * Dynamically creates a logger configuration with an appender that writes to a file.
   * This logger is used to write training data.
   *
   * @param loggerName     the name of the logger to create
   * @param outputFilePath the file path to write logs to
   * @param patternLayout  layout for the logger, if null just display
   *                       the message using DEFAULT_CONVERSION_PATTERN
   */
private static void createNewLoggerConfig(String loggerName, String outputFilePath, String patternLayout) {
    // Ignore null output files
    if (outputFilePath == null)
        return;
    // Create a logger to write the training data
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    Layout layout = PatternLayout.createLayout(patternLayout == null ? PatternLayout.DEFAULT_CONVERSION_PATTERN : patternLayout, config, null, StandardCharsets.UTF_8, false, false, null, null);
    Appender appender = FileAppender.createAppender(outputFilePath, "false", "false", loggerName, "true", "false", "false", "2000", layout, null, "false", null, config);
    appender.start();
    config.addAppender(appender);
    // Adding reference to the appender
    AppenderRef ref = AppenderRef.createAppenderRef(loggerName, null, null);
    AppenderRef[] refs = new AppenderRef[] { ref };
    LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.TRACE, loggerName, "true", refs, null, config, null);
    // Adding appender to logger, and adding logger to context
    loggerConfig.addAppender(appender, null, null);
    config.addLogger(loggerName, loggerConfig);
    ctx.updateLoggers();
}
Also used : Appender(org.apache.logging.log4j.core.Appender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) Layout(org.apache.logging.log4j.core.Layout) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 5 with AppenderRef

use of org.apache.logging.log4j.core.config.AppenderRef in project oxCore by GluuFederation.

the class LoggingHelper method configureConsoleAppender.

public static void configureConsoleAppender() {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();
    ConsoleAppender appender = ConsoleAppender.createDefaultAppenderForLayout(PatternLayout.createDefaultLayout());
    appender.start();
    config.addAppender(appender);
    AppenderRef[] refs = new AppenderRef[] { AppenderRef.createAppenderRef(appender.getName(), null, null) };
    LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.ALL, LogManager.ROOT_LOGGER_NAME, "true", refs, null, config, null);
    loggerConfig.addAppender(appender, null, null);
    config.addLogger(LogManager.ROOT_LOGGER_NAME, loggerConfig);
    ctx.updateLoggers();
}
Also used : AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Aggregations

AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)9 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)6 LoggerContext (org.apache.logging.log4j.core.LoggerContext)5 Configuration (org.apache.logging.log4j.core.config.Configuration)4 Appender (org.apache.logging.log4j.core.Appender)3 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)2 AppenderControl (org.apache.logging.log4j.core.config.AppenderControl)2 Property (org.apache.logging.log4j.core.config.Property)2 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)2 ArrayList (java.util.ArrayList)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 Level (org.apache.logging.log4j.Level)1 Filter (org.apache.logging.log4j.core.Filter)1 Layout (org.apache.logging.log4j.core.Layout)1 Logger (org.apache.logging.log4j.core.Logger)1 AbstractAppender (org.apache.logging.log4j.core.appender.AbstractAppender)1 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)1 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)1 ConfigurationException (org.apache.logging.log4j.core.config.ConfigurationException)1 PluginFactory (org.apache.logging.log4j.core.config.plugins.PluginFactory)1