From a5624f7409ba44748c5b99c8374fa8fa8da6e9b8 Mon Sep 17 00:00:00 2001 From: Salah KAABAOUI Date: Wed, 13 May 2020 17:17:51 +0200 Subject: [PATCH 1/2] logging - apply layout for log event --- .../log4j/v2/ApplicationInsightsAppender.java | 3 ++- .../v2/internal/ApplicationInsightsLogEvent.java | 16 ++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/logging/log4j2/src/main/java/com/microsoft/applicationinsights/log4j/v2/ApplicationInsightsAppender.java b/logging/log4j2/src/main/java/com/microsoft/applicationinsights/log4j/v2/ApplicationInsightsAppender.java index 9217cf692d4..dc9fc086801 100644 --- a/logging/log4j2/src/main/java/com/microsoft/applicationinsights/log4j/v2/ApplicationInsightsAppender.java +++ b/logging/log4j2/src/main/java/com/microsoft/applicationinsights/log4j/v2/ApplicationInsightsAppender.java @@ -191,7 +191,8 @@ public void append(LogEvent event) { } try { - ApplicationInsightsLogEvent aiEvent = new ApplicationInsightsLogEvent(event); + //Formats a logging event using the layout + ApplicationInsightsLogEvent aiEvent = new ApplicationInsightsLogEvent(event,getLayout()); this.telemetryClientProxy.sendEvent(aiEvent); } catch (Exception e) { // Appender failure must not fail the running application. diff --git a/logging/log4j2/src/main/java/com/microsoft/applicationinsights/log4j/v2/internal/ApplicationInsightsLogEvent.java b/logging/log4j2/src/main/java/com/microsoft/applicationinsights/log4j/v2/internal/ApplicationInsightsLogEvent.java index b5d023595ca..73abf152a5b 100644 --- a/logging/log4j2/src/main/java/com/microsoft/applicationinsights/log4j/v2/internal/ApplicationInsightsLogEvent.java +++ b/logging/log4j2/src/main/java/com/microsoft/applicationinsights/log4j/v2/internal/ApplicationInsightsLogEvent.java @@ -21,29 +21,33 @@ package com.microsoft.applicationinsights.log4j.v2.internal; +import java.io.Serializable; import java.util.HashMap; import java.util.Map; import com.microsoft.applicationinsights.internal.common.ApplicationInsightsEvent; import com.microsoft.applicationinsights.internal.logger.InternalLogger; import com.microsoft.applicationinsights.telemetry.SeverityLevel; + +import org.apache.logging.log4j.core.Layout; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.spi.StandardLevel; public final class ApplicationInsightsLogEvent extends ApplicationInsightsEvent { private LogEvent logEvent; + private Layout layout; - public ApplicationInsightsLogEvent(LogEvent logEvent) { + public ApplicationInsightsLogEvent(LogEvent logEvent, Layout layout) { this.logEvent = logEvent; + this.layout = layout; } @Override public String getMessage() { - String message = this.logEvent.getMessage() != null ? - this.logEvent.getMessage().getFormattedMessage() : - "Log4j Trace"; - - return message; + // Serializes the given event using the appender's layout if present. + return layout != null ? layout.toSerializable(logEvent).toString() : + logEvent.getMessage() != null ? logEvent.getMessage().getFormattedMessage() : + "Log4j Trace"; } @Override From 9ae498d8612aebf7cd3add3748f62c4fb25811a1 Mon Sep 17 00:00:00 2001 From: Salah KAABAOUI Date: Fri, 15 May 2020 12:54:26 +0200 Subject: [PATCH 2/2] update ApplicationInsightsLogEventTest test --- .../internal/ApplicationInsightsLogEventTest.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/logging/log4j2/src/test/java/com/microsoft/applicationinsights/log4j/v2/internal/ApplicationInsightsLogEventTest.java b/logging/log4j2/src/test/java/com/microsoft/applicationinsights/log4j/v2/internal/ApplicationInsightsLogEventTest.java index 37928208e7f..d2aaa66851d 100644 --- a/logging/log4j2/src/test/java/com/microsoft/applicationinsights/log4j/v2/internal/ApplicationInsightsLogEventTest.java +++ b/logging/log4j2/src/test/java/com/microsoft/applicationinsights/log4j/v2/internal/ApplicationInsightsLogEventTest.java @@ -21,10 +21,15 @@ package com.microsoft.applicationinsights.log4j.v2.internal; +import com.microsoft.applicationinsights.log4j.v2.ApplicationInsightsAppender; import com.microsoft.applicationinsights.telemetry.SeverityLevel; import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.ThreadContext; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.Layout; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.impl.ThrowableProxy; import org.apache.logging.log4j.message.Message; @@ -77,7 +82,14 @@ public Level getLevel() { return level; } }; - ApplicationInsightsLogEvent event = new ApplicationInsightsLogEvent(logEvent); + + Logger logger = LogManager.getRootLogger(); + org.apache.logging.log4j.core.Logger coreLogger = (org.apache.logging.log4j.core.Logger)logger; + + Map appenderMap = coreLogger.getAppenders(); + ApplicationInsightsAppender appender = (ApplicationInsightsAppender) appenderMap.get("test"); + + ApplicationInsightsLogEvent event = new ApplicationInsightsLogEvent(logEvent,appender.getLayout()); assertEquals(expected, event.getNormalizedSeverityLevel()); }