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