本文整理匯總了Java中org.apache.logging.log4j.core.LogEvent類的典型用法代碼示例。如果您正苦於以下問題:Java LogEvent類的具體用法?Java LogEvent怎麽用?Java LogEvent使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
LogEvent類屬於org.apache.logging.log4j.core包,在下文中一共展示了LogEvent類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: test
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Test
public void test() {
{
Logger logger = LogManager.getLogger("test.logger");
logger.error("This is an error!");
logger.error("This is another error!");
logger.error("This is a third error!");
}
assertThat(collector.getLogs()).hasSize(3)
.contains("This is an error!", "This is another error!", "This is a third error!");
List<LogEvent> rawLogs = (List<LogEvent>) collector.getRawLogs();
assertThat(rawLogs).hasSize(3);
assertTrue(rawLogs.stream().allMatch(l -> l.getLevel() == Level.ERROR));
}
示例2: prepareLogEvent
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
/**
* Prepare log event log event.
*
* @param logEvent the log event
* @return the log event
*/
public static LogEvent prepareLogEvent(final LogEvent logEvent) {
final String messageModified = TicketIdSanitizationUtils.sanitize(logEvent.getMessage().getFormattedMessage());
final Message message = new SimpleMessage(messageModified);
final LogEvent newLogEvent = Log4jLogEvent.newBuilder()
.setLevel(logEvent.getLevel())
.setLoggerName(logEvent.getLoggerName())
.setLoggerFqcn(logEvent.getLoggerFqcn())
.setContextMap(logEvent.getContextMap())
.setContextStack(logEvent.getContextStack())
.setEndOfBatch(logEvent.isEndOfBatch())
.setIncludeLocation(logEvent.isIncludeLocation())
.setMarker(logEvent.getMarker())
.setMessage(message)
.setNanoTime(logEvent.getNanoTime())
.setSource(logEvent.getSource())
.setThreadName(logEvent.getThreadName())
.setThrownProxy(logEvent.getThrownProxy())
.setThrown(logEvent.getThrown())
.setTimeMillis(logEvent.getTimeMillis())
.build();
return newLogEvent;
}
示例3: appenderUsesProvidedLayoutWhenMessageOnlyIsSetToFalse
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Test
public void appenderUsesProvidedLayoutWhenMessageOnlyIsSetToFalse() throws IOException, NoSuchMethodException, SecurityException {
// given
AbstractStringLayout layout = PowerMockito.mock(AbstractStringLayout.class);
ElasticsearchAppender.Builder builder = ElasticsearchAppenderTest.createTestElasticsearchAppenderBuilder();
builder.withMessageOnly(false);
builder.withLayout(layout);
LogEvent logEvent = mock(LogEvent.class);
ElasticsearchAppender appender = builder.build();
// when
appender.append(logEvent);
// then
ArgumentCaptor<LogEvent> captor = ArgumentCaptor.forClass(LogEvent.class);
verify(layout, times(1)).toSerializable(captor.capture());
assertEquals(logEvent, captor.getValue());
}
示例4: resolvesAppenderRefOnlyOnce
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Test
public void resolvesAppenderRefOnlyOnce() {
// given
Appender appender = mock(Appender.class);
when(appender.isStarted()).thenReturn(true);
Configuration configuration = mock(Configuration.class);
String testAppenderRef = "testAppenderRef";
when(configuration.getAppender(testAppenderRef)).thenReturn(appender);
FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration);
String failedMessage = "test failed message";
// when
failoverPolicy.deliver(failedMessage);
failoverPolicy.deliver(failedMessage);
// then
verify(configuration, times(1)).getAppender(anyString());
verify(appender, times(2)).append(any(LogEvent.class));
}
示例5: append
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Override
public void append(LogEvent event) {
Level level = event.getLevel();
if (TRACE.equals(level)) {
TRACE_LABEL.inc();
} else if (DEBUG.equals(level)) {
DEBUG_LABEL.inc();
} else if (INFO.equals(level)) {
INFO_LABEL.inc();
} else if (WARN.equals(level)) {
WARN_LABEL.inc();
} else if (ERROR.equals(level)) {
ERROR_LABEL.inc();
} else if (FATAL.equals(level)) {
FATAL_LABEL.inc();
}
}
示例6: append
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Override
public void append(LogEvent event) {
lock.lock();
try {
String json = new String(getLayout().toByteArray(event));
buffered.add(getBulkItem(json));
this.check();
} catch (Exception ex) {
if (!ignoreExceptions()) {
throw new AppenderLoggingException(ex);
} else {
LOGGER.error("Failed to process event.", ex);
}
} finally {
lock.unlock();
}
}
示例7: append
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Override
public synchronized void append(final LogEvent event) {
final Layout<? extends Serializable> layout = getLayout();
if (layout == null) {
if (event instanceof MutableLogEvent) {
// must take snapshot or subsequent calls to logger.log() will modify this event
events.add(((MutableLogEvent) event).createMemento());
} else {
events.add(event);
}
} else if (layout instanceof SerializedLayout) {
final byte[] header = layout.getHeader();
final byte[] content = layout.toByteArray(event);
final byte[] record = new byte[header.length + content.length];
System.arraycopy(header, 0, record, 0, header.length);
System.arraycopy(content, 0, record, header.length, content.length);
data.add(record);
} else {
write(layout.toByteArray(event));
}
if (countDownLatch != null) {
countDownLatch.countDown();
}
}
示例8: watch
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
public void watch(Class<?> loggerClass, Level level) {
this.loggerClass = loggerClass;
Appender appender = new AbstractAppender(APPENDER_NAME, null, PatternLayout.createDefaultLayout()) {
@Override
public void append(LogEvent event) {
logEvents.add(event);
}
};
appender.start();
final LoggerContext ctx = getLoggerContext();
LoggerConfig loggerConfig = ctx.getConfiguration().getLoggerConfig(loggerClass.getName());
oldLevel = loggerConfig.getLevel();
loggerConfig.setLevel(level);
loggerConfig.addAppender(appender, level, null);
ctx.updateLoggers();
}
示例9: logEventShouldMatchCustomConfig
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@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));
}
示例10: append
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Override
public void append(LogEvent event)
{
if (reader != null)
{
try
{
Writer out = reader.getOutput();
out.write(RESET_LINE);
out.write(formatEvent(event));
reader.drawLine();
reader.flush();
}
catch (IOException ignored)
{
}
}
else
{
out.print(formatEvent(event));
}
}
示例11: deliversToAppenderRef
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Test
public void deliversToAppenderRef() {
// given
Appender appender = mock(Appender.class);
when(appender.isStarted()).thenReturn(true);
Configuration configuration = mock(Configuration.class);
String testAppenderRef = "testAppenderRef";
when(configuration.getAppender(testAppenderRef)).thenReturn(appender);
FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration);
String failedMessage = "test failed message";
// when
failoverPolicy.deliver(failedMessage);
// then
verify(appender, times(1)).append(any(LogEvent.class));
}
示例12: resolve
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Override
public JsonNode resolve(TemplateResolverContext context, LogEvent logEvent, String key) {
ThreadContext.ContextStack contextStack = logEvent.getContextStack();
if (contextStack.getDepth() == 0) {
return null;
}
Pattern itemPattern = context.getNdcPattern();
ArrayNode contextStackNode = context.getObjectMapper().createArrayNode();
for (String contextStackItem : contextStack.asList()) {
boolean matches = itemPattern == null || itemPattern.matcher(contextStackItem).matches();
if (matches) {
contextStackNode.add(contextStackItem);
}
}
return contextStackNode;
}
示例13: checkLogEvent
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
private void checkLogEvent(Configuration config, LogEvent logEvent, String lookupTestKey, String lookupTestVal) throws IOException {
Set<String> mdcKeys = logEvent.getContextData().toMap().keySet();
String firstMdcKey = mdcKeys.iterator().next();
String firstMdcKeyExcludingRegex = mdcKeys.isEmpty() ? null : String.format("^(?!%s).*$", Pattern.quote(firstMdcKey));
List<String> ndcItems = logEvent.getContextStack().asList();
String firstNdcItem = ndcItems.get(0);
String firstNdcItemExcludingRegex = ndcItems.isEmpty() ? null : String.format("^(?!%s).*$", Pattern.quote(firstNdcItem));
LogstashLayout layout = LogstashLayout
.newBuilder()
.setConfiguration(config)
.setTemplateUri("classpath:LogstashTestLayout.json")
.setStackTraceEnabled(true)
.setLocationInfoEnabled(true)
.setMdcKeyPattern(firstMdcKeyExcludingRegex)
.setNdcPattern(firstNdcItemExcludingRegex)
.build();
String serializedLogEvent = layout.toSerializable(logEvent);
JsonNode rootNode = OBJECT_MAPPER.readTree(serializedLogEvent);
checkConstants(rootNode);
checkBasicFields(logEvent, rootNode);
checkSource(logEvent, rootNode);
checkException(logEvent, rootNode);
checkContextData(logEvent, firstMdcKeyExcludingRegex, rootNode);
checkContextStack(logEvent, firstNdcItemExcludingRegex, rootNode);
checkLookupTest(lookupTestKey, lookupTestVal, rootNode);
}
示例14: test_lineSeparator_suffix
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Test
public void test_lineSeparator_suffix() {
// Create the log event.
SimpleMessage message = new SimpleMessage("Hello, World!");
LogEvent logEvent = Log4jLogEvent
.newBuilder()
.setLoggerName(LogstashLayoutTest.class.getSimpleName())
.setLevel(Level.INFO)
.setMessage(message)
.build();
// Check line separators.
SoftAssertions assertions = new SoftAssertions();
test_lineSeparator_suffix(logEvent, true, assertions);
test_lineSeparator_suffix(logEvent, false, assertions);
assertions.assertAll();
}
示例15: format
import org.apache.logging.log4j.core.LogEvent; //導入依賴的package包/類
@Override
public final String format(LogEvent event) {
long eventTimeInMillis = event.getTimeMillis();
// handle "old" events that arrived in separate threads after rollover
if (eventTimeInMillis < currentFileTime) {
return doFormat(indexName, eventTimeInMillis);
}
// rollover
if (eventTimeInMillis >= nextRolloverTime && rollingOver.compareAndSet(false, true)) {
rollover(indexName, eventTimeInMillis);
rollingOver.set(false);
}
// happy path - have to check for pending rollover to avoid race conditions
if (!rollingOver.get()) {
return currentName;
}
// fail-safe for pending rollover
return doFormat(indexName, eventTimeInMillis);
}