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();
}
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;
}
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();
}
});
}
}
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();
}
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);
}
}
Aggregations