use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class Rfc5424LayoutTest method testLoggerFields.
@Test
public void testLoggerFields() {
final String[] fields = new String[] { "[BAZ@32473 baz=\"org.apache.logging.log4j.core.layout.Rfc5424LayoutTest.testLoggerFields\"]", "[RequestContext@3692 bar=\"org.apache.logging.log4j.core.layout.Rfc5424LayoutTest.testLoggerFields\"]", "[SD-ID@32473 source=\"org.apache.logging.log4j.core.layout.Rfc5424LayoutTest.testLoggerFields\"]" };
final List<String> expectedToContain = Arrays.asList(fields);
for (final Appender appender : root.getAppenders().values()) {
root.removeAppender(appender);
}
final LoggerFields[] loggerFields = new LoggerFields[] { LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("source", "%C.%M") }, "SD-ID", "32473", false), LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("baz", "%C.%M"), new KeyValuePair("baz", "%C.%M") }, "BAZ", "32473", false), LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("bar", "%C.%M") }, null, null, false) };
final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, "RequestContext", null, null, true, null, "ATM", null, "key1, key2, locale", null, null, null, false, loggerFields, null);
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
root.addAppender(appender);
root.setLevel(Level.DEBUG);
root.info("starting logger fields test");
try {
final List<String> list = appender.getMessages();
assertTrue("Not enough list entries", list.size() > 0);
final String message = list.get(0);
assertTrue("No class/method", message.contains("Rfc5424LayoutTest.testLoggerFields"));
for (final String value : expectedToContain) {
Assert.assertTrue("Message expected to contain " + value + " but did not", message.contains(value));
}
appender.clear();
} finally {
root.removeAppender(appender);
appender.stop();
}
}
use of org.apache.logging.log4j.core.Appender 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.Appender in project hive by apache.
the class LogDivertAppender method getDefaultLayout.
public Layout<? extends Serializable> getDefaultLayout() {
// There should be a ConsoleAppender. Copy its Layout.
Logger root = LogManager.getRootLogger();
Layout layout = null;
for (Appender ap : ((org.apache.logging.log4j.core.Logger) root).getAppenders().values()) {
if (ap.getClass().equals(ConsoleAppender.class)) {
layout = ap.getLayout();
break;
}
}
return layout;
}
use of org.apache.logging.log4j.core.Appender in project hive by apache.
the class OperationManager method initOperationLogCapture.
private void initOperationLogCapture(String loggingMode) {
// Register another Appender (with the same layout) that talks to us.
Appender ap = LogDivertAppender.createInstance(this, OperationLog.getLoggingLevel(loggingMode));
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configuration configuration = context.getConfiguration();
LoggerConfig loggerConfig = configuration.getLoggerConfig(LoggerFactory.getLogger(getClass()).getName());
loggerConfig.addAppender(ap, null, null);
context.updateLoggers();
ap.start();
}
use of org.apache.logging.log4j.core.Appender in project hive by apache.
the class HadoopJobExecHelper method localJobDebugger.
public void localJobDebugger(int exitVal, String taskId) {
StringBuilder sb = new StringBuilder();
sb.append("\n");
sb.append("Task failed!\n");
sb.append("Task ID:\n " + taskId + "\n\n");
sb.append("Logs:\n");
console.printError(sb.toString());
for (Appender appender : ((Logger) LogManager.getRootLogger()).getAppenders().values()) {
if (appender instanceof FileAppender) {
console.printError(((FileAppender) appender).getFileName());
} else if (appender instanceof RollingFileAppender) {
console.printError(((RollingFileAppender) appender).getFileName());
}
}
}
Aggregations