Search in sources :

Example 66 with LoggerContext

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

use of org.apache.logging.log4j.core.LoggerContext in project oxTrust by GluuFederation.

the class LoggerService method setExternalLoggerConfig.

private boolean setExternalLoggerConfig() {
    GluuAppliance updateAppliance = applianceService.getAppliance();
    if (StringUtils.isEmpty(updateAppliance.getOxLogConfigLocation())) {
        return false;
    }
    File log4jFile = new File(updateAppliance.getOxLogConfigLocation());
    if (!log4jFile.exists())
        return false;
    LoggerContext loggerContext = LoggerContext.getContext(false);
    loggerContext.setConfigLocation(log4jFile.toURI());
    loggerContext.reconfigure();
    configurationUpdateEvent.select(ConfigurationUpdate.Literal.INSTANCE).fire(this.appConfiguration);
    return true;
}
Also used : GluuAppliance(org.gluu.oxtrust.model.GluuAppliance) File(java.io.File) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 68 with LoggerContext

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

the class Log4J2Logger method setNodeId.

/**
 * {@inheritDoc}
 */
@Override
public void setNodeId(UUID nodeId) {
    A.notNull(nodeId, "nodeId");
    this.nodeId = nodeId;
    // Set nodeId as system variable to be used at configuration.
    System.setProperty(NODE_ID, U.id8(nodeId));
    if (inited) {
        final LoggerContext ctx = impl.getContext();
        synchronized (mux) {
            inited = false;
        }
        addConsoleAppenderIfNeeded(new C1<Boolean, Logger>() {

            @Override
            public Logger apply(Boolean init) {
                if (init)
                    ctx.reconfigure();
                return (Logger) LogManager.getRootLogger();
            }
        });
    }
}
Also used : IgniteLogger(org.apache.ignite.IgniteLogger) Logger(org.apache.logging.log4j.core.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 69 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 Logger createConsoleLogger() {
    // from http://logging.apache.org/log4j/2.x/manual/customconfig.html
    final LoggerContext ctx = impl.getContext();
    final Configuration cfg = ctx.getConfiguration();
    PatternLayout.Builder builder = PatternLayout.newBuilder().withPattern("%d{ISO8601}][%-5p][%t][%c{1}] %m%n").withCharset(Charset.defaultCharset()).withAlwaysWriteExceptions(false).withNoConsoleNoAnsi(false);
    PatternLayout layout = builder.build();
    ConsoleAppender.Builder consoleAppenderBuilder = ConsoleAppender.newBuilder().withName(CONSOLE_APPENDER).withLayout(layout);
    ConsoleAppender consoleApp = consoleAppenderBuilder.build();
    consoleApp.start();
    cfg.addAppender(consoleApp);
    cfg.getRootLogger().addAppender(consoleApp, Level.TRACE, null);
    ctx.updateLoggers(cfg);
    return ctx.getRootLogger();
}
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) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 70 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project hive by apache.

the class TestOperationLoggingLayout method checkAppenderState.

/**
 * assert that the appender for the given queryId is in the expected state.
 * @param msg a diagnostic
 * @param routingAppenderName name of the RoutingAppender
 * @param queryId the query id to use as a key
 * @param expectedStopped the expected stop state
 */
private void checkAppenderState(String msg, String routingAppenderName, String queryId, boolean expectedStopped) throws NoSuchFieldException, IllegalAccessException {
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    Configuration configuration = context.getConfiguration();
    LoggerConfig loggerConfig = configuration.getRootLogger();
    Map<String, Appender> appendersMap = loggerConfig.getAppenders();
    RoutingAppender routingAppender = (RoutingAppender) appendersMap.get(routingAppenderName);
    Assert.assertNotNull(msg + "could not find routingAppender " + routingAppenderName, routingAppender);
    Field defaultsField = RoutingAppender.class.getDeclaredField("appenders");
    defaultsField.setAccessible(true);
    ConcurrentHashMap appenders = (ConcurrentHashMap) defaultsField.get(routingAppender);
    AppenderControl appenderControl = (AppenderControl) appenders.get(queryId);
    if (!expectedStopped) {
        Assert.assertNotNull(msg + "Could not find AppenderControl for query id " + queryId, appenderControl);
        Appender appender = appenderControl.getAppender();
        Assert.assertNotNull(msg + "could not find Appender for query id " + queryId + " from AppenderControl " + appenderControl, appender);
        Assert.assertEquals(msg + "Appender for query is in unexpected state", expectedStopped, appender.isStopped());
    } else {
        Assert.assertNull(msg + "AppenderControl for query id is not removed" + queryId, appenderControl);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) LogDivertAppender(org.apache.hadoop.hive.ql.log.LogDivertAppender) RoutingAppender(org.apache.logging.log4j.core.appender.routing.RoutingAppender) HushableRandomAccessFileAppender(org.apache.hadoop.hive.ql.log.HushableRandomAccessFileAppender) Field(java.lang.reflect.Field) RoutingAppender(org.apache.logging.log4j.core.appender.routing.RoutingAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Aggregations

LoggerContext (org.apache.logging.log4j.core.LoggerContext)163 Configuration (org.apache.logging.log4j.core.config.Configuration)57 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)36 Test (org.junit.Test)33 Appender (org.apache.logging.log4j.core.Appender)18 File (java.io.File)12 IOException (java.io.IOException)12 Logger (org.apache.logging.log4j.Logger)11 BeforeClass (org.junit.BeforeClass)11 Map (java.util.Map)10 Level (org.apache.logging.log4j.Level)8 LogEvent (org.apache.logging.log4j.core.LogEvent)8 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)7 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)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 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)4 RoutingAppender (org.apache.logging.log4j.core.appender.routing.RoutingAppender)4 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)4