Search in sources :

Example 41 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class ReusableLogEventFactoryTest method testCreateEventInitFieldsProperly.

@SuppressWarnings("deprecation")
@Test
public void testCreateEventInitFieldsProperly() throws Exception {
    final ReusableLogEventFactory factory = new ReusableLogEventFactory();
    final LogEvent event = callCreateEvent(factory, "logger", Level.INFO, new SimpleMessage("xyz"), null);
    ReusableLogEventFactory.release(event);
    assertNotNull(event.getContextMap());
    assertNotNull(event.getContextData());
    assertNotNull(event.getContextStack());
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 42 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 43 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)

Example 44 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class LoggerConfig method log.

/**
     * Logs an event.
     *
     * @param loggerName The name of the Logger.
     * @param fqcn The fully qualified class name of the caller.
     * @param marker A Marker or null if none is present.
     * @param level The event Level.
     * @param data The Message.
     * @param t A Throwable or null.
     */
@PerformanceSensitive("allocation")
public void log(final String loggerName, final String fqcn, final Marker marker, final Level level, final Message data, final Throwable t) {
    List<Property> props = null;
    if (!propertiesRequireLookup) {
        props = properties;
    } else {
        if (properties != null) {
            props = new ArrayList<>(properties.size());
            final LogEvent event = Log4jLogEvent.newBuilder().setMessage(data).setMarker(marker).setLevel(level).setLoggerName(loggerName).setLoggerFqcn(fqcn).setThrown(t).build();
            for (int i = 0; i < properties.size(); i++) {
                final Property prop = properties.get(i);
                final String value = // since LOG4J2-1575
                prop.isValueNeedsLookup() ? //
                config.getStrSubstitutor().replace(event, prop.getValue()) : prop.getValue();
                props.add(Property.createProperty(prop.getName(), value));
            }
        }
    }
    final LogEvent logEvent = logEventFactory.createEvent(loggerName, marker, fqcn, level, data, props, t);
    try {
        log(logEvent);
    } finally {
        // LOG4J2-1583 prevent scrambled logs when logging calls are nested (logging in toString())
        ReusableLogEventFactory.release(logEvent);
    }
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) PerformanceSensitive(org.apache.logging.log4j.util.PerformanceSensitive)

Example 45 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class Log4j1NdcPatternConverterTest method testConverter.

private void testConverter(final String expected) {
    final Log4j1NdcPatternConverter converter = Log4j1NdcPatternConverter.newInstance(null);
    final LogEvent event = Log4jLogEvent.newBuilder().setLoggerName("MyLogger").setLevel(Level.DEBUG).setMessage(new SimpleMessage("Hello")).build();
    final StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    assertEquals(expected, sb.toString());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage)

Aggregations

LogEvent (org.apache.logging.log4j.core.LogEvent)189 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 Appender (org.apache.logging.log4j.core.Appender)6 MapMessage (org.apache.logging.log4j.message.MapMessage)6 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)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