Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging - apply layout for log4j event #1241

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Serializable> layout;

public ApplicationInsightsLogEvent(LogEvent logEvent) {
public ApplicationInsightsLogEvent(LogEvent logEvent, Layout<? extends Serializable> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Appender> appenderMap = coreLogger.getAppenders();
ApplicationInsightsAppender appender = (ApplicationInsightsAppender) appenderMap.get("test");

ApplicationInsightsLogEvent event = new ApplicationInsightsLogEvent(logEvent,appender.getLayout());

assertEquals(expected, event.getNormalizedSeverityLevel());
}
Expand Down