use of org.apache.logging.log4j.core.LogEvent in project graylog2-server by Graylog2.
the class MemoryAppenderTest method appenderIsThreadSafe.
@Test
public void appenderIsThreadSafe() throws Exception {
final int bufferSize = 1;
final MemoryAppender appender = MemoryAppender.createAppender(null, null, "memory", String.valueOf(bufferSize), "false");
assertThat(appender).isNotNull();
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLevel(Level.INFO).setLoggerName("test").setLoggerFqcn("com.example.test").setMessage(new SimpleMessage("Message")).build();
final int threadCount = 48;
final Thread[] threads = new Thread[threadCount];
final TestAwareThreadGroup threadGroup = new TestAwareThreadGroup("memory-appender-test");
final CountDownLatch latch = new CountDownLatch(1);
for (int i = 0; i < threadCount; i++) {
final Runnable runner = () -> {
try {
latch.await();
long start = System.currentTimeMillis();
while (System.currentTimeMillis() - start < TimeUnit.SECONDS.toMillis(4L)) {
appender.append(logEvent);
}
} catch (InterruptedException ie) {
// Do nothing
}
};
final Thread thread = new Thread(threadGroup, runner, "TestThread-" + i);
threads[i] = thread;
thread.start();
}
latch.countDown();
for (int i = 0; i < threadCount; i++) {
threads[i].join(TimeUnit.SECONDS.toMillis(5L));
}
assertThat(threadGroup.getExceptionsInThreads().get()).isEqualTo(0);
}
use of org.apache.logging.log4j.core.LogEvent in project geode by apache.
the class CustomConfigWithLogServiceIntegrationTest method logEventShouldMatchCustomConfig.
@Test
public void logEventShouldMatchCustomConfig() throws Exception {
String logLogger = getClass().getName();
Level logLevel = Level.DEBUG;
String logMessage = "this is a log statement";
Logger logger = LogService.getLogger();
logger.debug(logMessage);
BasicAppender appender = BasicAppender.getInstance();
assertThat(appender).isNotNull();
assertThat(appender.events()).hasSize(1);
LogEvent event = appender.events().get(0);
assertThat(event.getLoggerName()).isEqualTo(logLogger);
assertThat(event.getLevel()).isEqualTo(logLevel);
assertThat(event.getMessage().getFormattedMessage()).isEqualTo(logMessage);
assertThat(systemOutRule.getLog()).contains(logLevel.name());
assertThat(systemOutRule.getLog()).contains(logMessage);
assertThat(systemOutRule.getLog()).contains(CONFIG_LAYOUT_PREFIX);
assertThat(systemOutRule.getLog()).matches(defineLogStatementRegex(logLevel, logMessage));
}
use of org.apache.logging.log4j.core.LogEvent in project geode by apache.
the class LogWriterAppenderJUnitTest method testLogWriterLevels.
/**
* Verifies that logging occurs at the levels set in the LogWriter
*/
@Test
public void testLogWriterLevels() throws IOException {
// this.getClass().getName();
final String loggerName = LogService.MAIN_LOGGER_NAME;
// Force logging to be initialized
LogService.getLogger();
// Create the LogWriterLogger that will be attached to the appender
final LogWriterLogger logWriterLogger = LogWriterLogger.create(loggerName, false);
logWriterLogger.setLevel(Level.INFO);
// Create the appender
final StringWriter stringWriter = new StringWriter();
final PureLogWriter logWriter = new PureLogWriter(InternalLogWriter.FINEST_LEVEL, new PrintWriter(stringWriter), "");
final AppenderContext[] contexts = new AppenderContext[2];
// root context
contexts[0] = LogService.getAppenderContext();
// "org.apache"
contexts[1] = LogService.getAppenderContext(LogService.BASE_LOGGER_NAME);
// context
this.appender = LogWriterAppender.create(contexts, loggerName, logWriter, null);
logWriter.finest("DIRECT MESSAGE");
assertTrue(Pattern.compile(".*\\[finest .*DIRECT MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
LogEvent event = Log4jLogEvent.newBuilder().setLevel(Level.INFO).setLoggerFqcn("NAME").setLoggerName("NAME").setMessage(new ParameterizedMessage("LOGEVENT MESSAGE")).build();
this.appender.append(event);
assertTrue(Pattern.compile(".*\\[info .*LOGEVENT MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
logWriterLogger.finest("FINEST MESSAGE");
assertFalse(Pattern.compile(".*\\[finest .*FINEST MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
logWriterLogger.fine("FINE MESSAGE");
assertFalse(Pattern.compile(".*\\[fine .*FINE MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
logWriterLogger.info("INFO MESSAGE");
assertTrue(stringWriter.toString(), Pattern.compile(".*\\[info .*INFO MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
// Change the level
logWriterLogger.setLevel(Level.DEBUG);
logWriterLogger.finest("FINEST MESSAGE");
assertFalse(Pattern.compile(".*\\[finest .*FINEST MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
logWriterLogger.fine("FINE MESSAGE");
assertTrue(Pattern.compile(".*\\[fine .*FINE MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
logWriterLogger.info("INFO MESSAGE");
assertTrue(Pattern.compile(".*\\[info .*INFO MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
this.appender.destroy();
}
use of org.apache.logging.log4j.core.LogEvent in project jmeter by apache.
the class TestGuiLogEventAppender method beforeClass.
/*
* Configure logging with GuiLogEventAppender for root logger, and override the handler of GuiLogEventAppender
* to see if there's any log4j2 AppenderControl level error (e.g, "Recursive call to appender gui-log-event").
*/
@BeforeClass
public static void beforeClass() {
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
builder.setPackages("org.apache.jmeter.gui.logging");
AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d %p %c{1.}: %m%n"));
builder.add(appenderBuilder);
appenderBuilder = builder.newAppender("gui-log-event", "GuiLogEvent");
appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d %p %c{1.}: %m%n"));
builder.add(appenderBuilder);
RootLoggerComponentBuilder rootLoggerBuilder = builder.newRootLogger(Level.INFO);
rootLoggerBuilder.add(builder.newAppenderRef("Stdout")).add(builder.newAppenderRef("gui-log-event"));
builder.add(rootLoggerBuilder);
final LoggerContext loggerContext = Configurator.initialize(builder.build());
final Appender guiLogEventAppender = loggerContext.getRootLogger().getAppenders().get("gui-log-event");
guiLogEventAppender.stop();
guiLogEventAppender.setHandler(new ErrorHandler() {
public void error(String msg) {
log4j2LevelErrorMessages.add(msg);
}
public void error(String msg, Throwable t) {
log4j2LevelErrorMessages.add(msg + " " + t);
}
public void error(String msg, LogEvent event, Throwable t) {
log4j2LevelErrorMessages.add(msg + " " + t);
}
});
guiLogEventAppender.start();
}
use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.
the class AsyncLoggerConfigDisruptor method enqueueEvent.
@Override
public void enqueueEvent(final LogEvent event, final AsyncLoggerConfig asyncLoggerConfig) {
// LOG4J2-639: catch NPE if disruptor field was set to null after our check above
try {
final LogEvent logEvent = prepareEvent(event);
enqueue(logEvent, asyncLoggerConfig);
} catch (final NullPointerException npe) {
// Note: NPE prevents us from adding a log event to the disruptor after it was shut down,
// which could cause the publishEvent method to hang and never return.
LOGGER.warn("Ignoring log event after log4j was shut down.");
}
}
Aggregations