Search in sources :

Example 11 with LogEvent

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);
}
Also used : MemoryAppender(org.graylog2.log4j.MemoryAppender) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 12 with LogEvent

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));
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Level(org.apache.logging.log4j.Level) Logger(org.apache.logging.log4j.Logger) StatusLogger(org.apache.logging.log4j.status.StatusLogger) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 13 with LogEvent

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();
}
Also used : StringWriter(java.io.StringWriter) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) PureLogWriter(org.apache.geode.internal.logging.PureLogWriter) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) PrintWriter(java.io.PrintWriter) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 14 with LogEvent

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();
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) ErrorHandler(org.apache.logging.log4j.core.ErrorHandler) AppenderComponentBuilder(org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder) LogEvent(org.apache.logging.log4j.core.LogEvent) RootLoggerComponentBuilder(org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) BeforeClass(org.junit.BeforeClass)

Example 15 with LogEvent

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.");
    }
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent)

Aggregations

LogEvent (org.apache.logging.log4j.core.LogEvent)188 Test (org.junit.Test)150 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)127 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)94 Message (org.apache.logging.log4j.message.Message)33 Marker (org.apache.logging.log4j.Marker)16 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)11 HashMap (java.util.HashMap)9 LoggerContext (org.apache.logging.log4j.core.LoggerContext)8 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)8 File (java.io.File)7 IOException (java.io.IOException)7 Level (org.apache.logging.log4j.Level)7 MapMessage (org.apache.logging.log4j.message.MapMessage)6 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)5 Appender (org.apache.logging.log4j.core.Appender)5 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)5