|
|
a81985 |
diff --git a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/EventDataConverter.java b/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/EventDataConverter.java
|
|
|
a81985 |
deleted file mode 100644
|
|
|
a81985 |
index 620232a..0000000
|
|
|
a81985 |
--- a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/EventDataConverter.java
|
|
|
a81985 |
+++ /dev/null
|
|
|
a81985 |
@@ -1,50 +0,0 @@
|
|
|
a81985 |
-/*
|
|
|
a81985 |
- * Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
a81985 |
- * contributor license agreements. See the NOTICE file distributed with
|
|
|
a81985 |
- * this work for additional information regarding copyright ownership.
|
|
|
a81985 |
- * The ASF licenses this file to You under the Apache license, Version 2.0
|
|
|
a81985 |
- * (the "License"); you may not use this file except in compliance with
|
|
|
a81985 |
- * the License. You may obtain a copy of the License at
|
|
|
a81985 |
- *
|
|
|
a81985 |
- * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
a81985 |
- *
|
|
|
a81985 |
- * Unless required by applicable law or agreed to in writing, software
|
|
|
a81985 |
- * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
a81985 |
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
a81985 |
- * See the license for the specific language governing permissions and
|
|
|
a81985 |
- * limitations under the license.
|
|
|
a81985 |
- */
|
|
|
a81985 |
-package org.apache.logging.slf4j;
|
|
|
a81985 |
-
|
|
|
a81985 |
-import java.util.Map;
|
|
|
a81985 |
-
|
|
|
a81985 |
-import org.apache.logging.log4j.message.Message;
|
|
|
a81985 |
-import org.apache.logging.log4j.message.ParameterizedMessage;
|
|
|
a81985 |
-import org.apache.logging.log4j.message.StructuredDataMessage;
|
|
|
a81985 |
-import org.slf4j.ext.EventData;
|
|
|
a81985 |
-
|
|
|
a81985 |
-/**
|
|
|
a81985 |
- *
|
|
|
a81985 |
- */
|
|
|
a81985 |
-public class EventDataConverter {
|
|
|
a81985 |
-
|
|
|
a81985 |
- public Message convertEvent(final String message, final Object[] objects, final Throwable throwable) {
|
|
|
a81985 |
- try {
|
|
|
a81985 |
- final EventData data = objects != null && objects[0] instanceof EventData ?
|
|
|
a81985 |
- (EventData) objects[0] : new EventData(message);
|
|
|
a81985 |
- final StructuredDataMessage msg =
|
|
|
a81985 |
- new StructuredDataMessage(data.getEventId(), data.getMessage(), data.getEventType());
|
|
|
a81985 |
- for (final Map.Entry<String, Object> entry : data.getEventMap().entrySet()) {
|
|
|
a81985 |
- final String key = entry.getKey();
|
|
|
a81985 |
- if (EventData.EVENT_TYPE.equals(key) || EventData.EVENT_ID.equals(key)
|
|
|
a81985 |
- || EventData.EVENT_MESSAGE.equals(key)) {
|
|
|
a81985 |
- continue;
|
|
|
a81985 |
- }
|
|
|
a81985 |
- msg.put(key, String.valueOf(entry.getValue()));
|
|
|
a81985 |
- }
|
|
|
a81985 |
- return msg;
|
|
|
a81985 |
- } catch (final Exception ex) {
|
|
|
a81985 |
- return new ParameterizedMessage(message, objects, throwable);
|
|
|
a81985 |
- }
|
|
|
a81985 |
- }
|
|
|
a81985 |
-}
|
|
|
a81985 |
diff --git a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java b/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java
|
|
|
a81985 |
index 1fa8080..90cfe4e 100644
|
|
|
a81985 |
--- a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java
|
|
|
a81985 |
+++ b/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java
|
|
|
a81985 |
@@ -27,9 +27,7 @@ import org.apache.logging.log4j.message.Message;
|
|
|
a81985 |
import org.apache.logging.log4j.message.ParameterizedMessage;
|
|
|
a81985 |
import org.apache.logging.log4j.message.SimpleMessage;
|
|
|
a81985 |
import org.apache.logging.log4j.spi.ExtendedLogger;
|
|
|
a81985 |
-import org.apache.logging.log4j.util.LoaderUtil;
|
|
|
a81985 |
import org.slf4j.Marker;
|
|
|
a81985 |
-import org.slf4j.MarkerFactory;
|
|
|
a81985 |
import org.slf4j.impl.StaticMarkerBinder;
|
|
|
a81985 |
import org.slf4j.spi.LocationAwareLogger;
|
|
|
a81985 |
|
|
|
a81985 |
@@ -39,18 +37,13 @@ import org.slf4j.spi.LocationAwareLogger;
|
|
|
a81985 |
public class Log4jLogger implements LocationAwareLogger, Serializable {
|
|
|
a81985 |
|
|
|
a81985 |
public static final String FQCN = Log4jLogger.class.getName();
|
|
|
a81985 |
-
|
|
|
a81985 |
private static final long serialVersionUID = 7869000638091304316L;
|
|
|
a81985 |
- private static final Marker EVENT_MARKER = MarkerFactory.getMarker("EVENT");
|
|
|
a81985 |
- private static final EventDataConverter CONVERTER = createConverter();
|
|
|
a81985 |
|
|
|
a81985 |
- private final boolean eventLogger;
|
|
|
a81985 |
private transient ExtendedLogger logger;
|
|
|
a81985 |
private final String name;
|
|
|
a81985 |
|
|
|
a81985 |
public Log4jLogger(final ExtendedLogger logger, final String name) {
|
|
|
a81985 |
this.logger = logger;
|
|
|
a81985 |
- this.eventLogger = "EventLogger".equals(name);
|
|
|
a81985 |
this.name = name;
|
|
|
a81985 |
}
|
|
|
a81985 |
|
|
|
a81985 |
@@ -363,9 +356,7 @@ public class Log4jLogger implements LocationAwareLogger, Serializable {
|
|
|
a81985 |
return;
|
|
|
a81985 |
}
|
|
|
a81985 |
final Message msg;
|
|
|
a81985 |
- if (CONVERTER != null && eventLogger && marker != null && marker.contains(EVENT_MARKER)) {
|
|
|
a81985 |
- msg = CONVERTER.convertEvent(message, params, throwable);
|
|
|
a81985 |
- } else if (params == null) {
|
|
|
a81985 |
+ if (params == null) {
|
|
|
a81985 |
msg = new SimpleMessage(message);
|
|
|
a81985 |
} else {
|
|
|
a81985 |
msg = new ParameterizedMessage(message, params, throwable);
|
|
|
a81985 |
@@ -410,15 +401,6 @@ public class Log4jLogger implements LocationAwareLogger, Serializable {
|
|
|
a81985 |
aOutputStream.defaultWriteObject();
|
|
|
a81985 |
}
|
|
|
a81985 |
|
|
|
a81985 |
- private static EventDataConverter createConverter() {
|
|
|
a81985 |
- try {
|
|
|
a81985 |
- LoaderUtil.loadClass("org.slf4j.ext.EventData");
|
|
|
a81985 |
- return new EventDataConverter();
|
|
|
a81985 |
- } catch (final ClassNotFoundException cnfe) {
|
|
|
a81985 |
- return null;
|
|
|
a81985 |
- }
|
|
|
a81985 |
- }
|
|
|
a81985 |
-
|
|
|
a81985 |
private static Level getLevel(final int i) {
|
|
|
a81985 |
switch (i) {
|
|
|
a81985 |
case TRACE_INT:
|
|
|
a81985 |
diff --git a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/LoggerTest.java b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/LoggerTest.java
|
|
|
a81985 |
index 0524074..c60f1ad 100644
|
|
|
a81985 |
--- a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/LoggerTest.java
|
|
|
a81985 |
+++ b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/LoggerTest.java
|
|
|
a81985 |
@@ -21,7 +21,6 @@ import static org.junit.Assert.assertNotNull;
|
|
|
a81985 |
import static org.junit.Assert.assertTrue;
|
|
|
a81985 |
|
|
|
a81985 |
import java.util.List;
|
|
|
a81985 |
-import java.util.Locale;
|
|
|
a81985 |
|
|
|
a81985 |
import org.apache.logging.log4j.junit.LoggerContextRule;
|
|
|
a81985 |
import org.apache.logging.log4j.test.appender.ListAppender;
|
|
|
a81985 |
@@ -34,8 +33,6 @@ import org.slf4j.Logger;
|
|
|
a81985 |
import org.slf4j.LoggerFactory;
|
|
|
a81985 |
import org.slf4j.MDC;
|
|
|
a81985 |
import org.slf4j.Marker;
|
|
|
a81985 |
-import org.slf4j.ext.EventData;
|
|
|
a81985 |
-import org.slf4j.ext.EventLogger;
|
|
|
a81985 |
import org.slf4j.ext.XLogger;
|
|
|
a81985 |
import org.slf4j.ext.XLoggerFactory;
|
|
|
a81985 |
import org.slf4j.spi.LocationAwareLogger;
|
|
|
a81985 |
@@ -145,23 +142,6 @@ public class LoggerTest {
|
|
|
a81985 |
verify("List", "o.a.l.s.LoggerTest Hello, Log4j Log4j {} MDC{}" + Strings.LINE_SEPARATOR);
|
|
|
a81985 |
}
|
|
|
a81985 |
|
|
|
a81985 |
- @Test
|
|
|
a81985 |
- public void testEventLogger() {
|
|
|
a81985 |
- MDC.put("loginId", "JohnDoe");
|
|
|
a81985 |
- MDC.put("ipAddress", "192.168.0.120");
|
|
|
a81985 |
- MDC.put("locale", Locale.US.getDisplayName());
|
|
|
a81985 |
- final EventData data = new EventData();
|
|
|
a81985 |
- data.setEventType("Transfer");
|
|
|
a81985 |
- data.setEventId("Audit@18060");
|
|
|
a81985 |
- data.setMessage("Transfer Complete");
|
|
|
a81985 |
- data.put("ToAccount", "123456");
|
|
|
a81985 |
- data.put("FromAccount", "123457");
|
|
|
a81985 |
- data.put("Amount", "200.00");
|
|
|
a81985 |
- EventLogger.logEvent(data);
|
|
|
a81985 |
- MDC.clear();
|
|
|
a81985 |
- verify("EventLogger", "o.a.l.s.LoggerTest Transfer [Audit@18060 Amount=\"200.00\" FromAccount=\"123457\" ToAccount=\"123456\"] Transfer Complete" + Strings.LINE_SEPARATOR);
|
|
|
a81985 |
- }
|
|
|
a81985 |
-
|
|
|
a81985 |
private void verify(final String name, final String expected) {
|
|
|
a81985 |
final ListAppender listApp = ctx.getListAppender(name);
|
|
|
a81985 |
assertNotNull("Missing Appender", listApp);
|
|
|
a81985 |
@@ -177,6 +157,5 @@ public class LoggerTest {
|
|
|
a81985 |
public void cleanup() {
|
|
|
a81985 |
MDC.clear();
|
|
|
a81985 |
ctx.getListAppender("List").clear();
|
|
|
a81985 |
- ctx.getListAppender("EventLogger").clear();
|
|
|
a81985 |
}
|
|
|
a81985 |
}
|
|
|
a81985 |
diff --git a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/OptionalTest.java b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/OptionalTest.java
|
|
|
a81985 |
deleted file mode 100644
|
|
|
a81985 |
index a6e9fd5..0000000
|
|
|
a81985 |
--- a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/OptionalTest.java
|
|
|
a81985 |
+++ /dev/null
|
|
|
a81985 |
@@ -1,69 +0,0 @@
|
|
|
a81985 |
-/*
|
|
|
a81985 |
- * Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
a81985 |
- * contributor license agreements. See the NOTICE file distributed with
|
|
|
a81985 |
- * this work for additional information regarding copyright ownership.
|
|
|
a81985 |
- * The ASF licenses this file to You under the Apache license, Version 2.0
|
|
|
a81985 |
- * (the "License"); you may not use this file except in compliance with
|
|
|
a81985 |
- * the License. You may obtain a copy of the License at
|
|
|
a81985 |
- *
|
|
|
a81985 |
- * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
a81985 |
- *
|
|
|
a81985 |
- * Unless required by applicable law or agreed to in writing, software
|
|
|
a81985 |
- * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
a81985 |
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
a81985 |
- * See the license for the specific language governing permissions and
|
|
|
a81985 |
- * limitations under the license.
|
|
|
a81985 |
- */
|
|
|
a81985 |
-package org.apache.logging.slf4j;
|
|
|
a81985 |
-
|
|
|
a81985 |
-import java.util.List;
|
|
|
a81985 |
-
|
|
|
a81985 |
-import org.apache.logging.log4j.junit.LoggerContextRule;
|
|
|
a81985 |
-import org.apache.logging.log4j.test.appender.ListAppender;
|
|
|
a81985 |
-import org.apache.logging.log4j.util.Strings;
|
|
|
a81985 |
-import org.junit.Before;
|
|
|
a81985 |
-import org.junit.ClassRule;
|
|
|
a81985 |
-import org.junit.Test;
|
|
|
a81985 |
-import org.slf4j.Logger;
|
|
|
a81985 |
-import org.slf4j.LoggerFactory;
|
|
|
a81985 |
-import org.slf4j.MDC;
|
|
|
a81985 |
-import org.slf4j.Marker;
|
|
|
a81985 |
-import org.slf4j.MarkerFactory;
|
|
|
a81985 |
-
|
|
|
a81985 |
-import static org.junit.Assert.*;
|
|
|
a81985 |
-
|
|
|
a81985 |
-/**
|
|
|
a81985 |
- *
|
|
|
a81985 |
- */
|
|
|
a81985 |
-public class OptionalTest {
|
|
|
a81985 |
-
|
|
|
a81985 |
- private static final String CONFIG = "log4j-test1.xml";
|
|
|
a81985 |
-
|
|
|
a81985 |
- @ClassRule
|
|
|
a81985 |
- public static final LoggerContextRule CTX = new LoggerContextRule(CONFIG);
|
|
|
a81985 |
-
|
|
|
a81985 |
- Logger logger = LoggerFactory.getLogger("EventLogger");
|
|
|
a81985 |
- Marker marker = MarkerFactory.getMarker("EVENT");
|
|
|
a81985 |
-
|
|
|
a81985 |
- @Test
|
|
|
a81985 |
- public void testEventLogger() {
|
|
|
a81985 |
- logger.info(marker, "This is a test");
|
|
|
a81985 |
- MDC.clear();
|
|
|
a81985 |
- verify("EventLogger", "o.a.l.s.OptionalTest This is a test" + Strings.LINE_SEPARATOR);
|
|
|
a81985 |
- }
|
|
|
a81985 |
-
|
|
|
a81985 |
- private void verify(final String name, final String expected) {
|
|
|
a81985 |
- final ListAppender listApp = CTX.getListAppender(name);
|
|
|
a81985 |
- final List<String> events = listApp.getMessages();
|
|
|
a81985 |
- assertTrue("Incorrect number of messages. Expected 1 Actual " + events.size(), events.size()== 1);
|
|
|
a81985 |
- final String actual = events.get(0);
|
|
|
a81985 |
- assertEquals("Incorrect message. Expected " + expected + ". Actual " + actual, expected, actual);
|
|
|
a81985 |
- listApp.clear();
|
|
|
a81985 |
- }
|
|
|
a81985 |
-
|
|
|
a81985 |
- @Before
|
|
|
a81985 |
- public void cleanup() {
|
|
|
a81985 |
- CTX.getListAppender("List").clear();
|
|
|
a81985 |
- CTX.getListAppender("EventLogger").clear();
|
|
|
a81985 |
- }
|
|
|
a81985 |
-}
|
|
|
a81985 |
diff --git a/log4j-slf4j-impl/src/test/resources/log4j-test1.xml b/log4j-slf4j-impl/src/test/resources/log4j-test1.xml
|
|
|
a81985 |
index a64bdfa..07a2be6 100644
|
|
|
a81985 |
--- a/log4j-slf4j-impl/src/test/resources/log4j-test1.xml
|
|
|
a81985 |
+++ b/log4j-slf4j-impl/src/test/resources/log4j-test1.xml
|
|
|
a81985 |
@@ -6,9 +6,6 @@
|
|
|
a81985 |
<ThresholdFilter level="trace"/>
|
|
|
a81985 |
|
|
|
a81985 |
<Appenders>
|
|
|
a81985 |
- <List name="EventLogger">
|
|
|
a81985 |
- <PatternLayout pattern="%C{1.} %m%n"/>
|
|
|
a81985 |
- </List>
|
|
|
a81985 |
<Console name="STDOUT">
|
|
|
a81985 |
<PatternLayout pattern="%C{1.} %m MDC%X%n"/>
|
|
|
a81985 |
</Console>
|
|
|
a81985 |
@@ -24,10 +21,6 @@
|
|
|
a81985 |
</Appenders>
|
|
|
a81985 |
|
|
|
a81985 |
<Loggers>
|
|
|
a81985 |
- <Logger name="EventLogger" level="info" additivity="false">
|
|
|
a81985 |
- <AppenderRef ref="EventLogger"/>
|
|
|
a81985 |
- </Logger>>
|
|
|
a81985 |
-
|
|
|
a81985 |
<Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
|
|
|
a81985 |
<AppenderRef ref="File"/>
|
|
|
a81985 |
</Logger>
|