You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2023/02/16 22:02:00 UTC
[logging-log4j-samples] 03/05: Migrate `log4j-samples-loggerProperties` module
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j-samples.git
commit 720714b353456a518ddb62d162ccfb6a84e3e13e
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Thu Feb 16 21:59:05 2023 +0100
Migrate `log4j-samples-loggerProperties` module
---
log4j-samples-loggerProperties/pom.xml | 47 ++++++++++++
.../apache/logging/log4j/lookup/CustomLookup.java | 84 ++++++++++++++++++++
.../logging/log4j/lookup/CustomMapMessage.java | 54 +++++++++++++
.../logging/log4j/lookup/MapMessageLookup.java | 76 ++++++++++++++++++
.../apache/logging/log4j/CustomPropertiesTest.java | 52 +++++++++++++
.../apache/logging/log4j/MapMessageLookupTest.java | 89 ++++++++++++++++++++++
.../src/test/resources/log4j2.xml | 48 ++++++++++++
pom.xml | 1 +
8 files changed, 451 insertions(+)
diff --git a/log4j-samples-loggerProperties/pom.xml b/log4j-samples-loggerProperties/pom.xml
new file mode 100644
index 0000000..8bba440
--- /dev/null
+++ b/log4j-samples-loggerProperties/pom.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.logging.log4j.samples</groupId>
+ <artifactId>log4j-samples</artifactId>
+ <version>${revision}</version>
+ </parent>
+ <artifactId>log4j-samples-loggerProperties</artifactId>
+ <packaging>jar</packaging>
+ <name>Apache Log4j Samples: LoggerProperties</name>
+ <properties>
+ <revision>2.19.1-SNAPSHOT</revision>
+ </properties>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/log4j-samples-loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomLookup.java b/log4j-samples-loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomLookup.java
new file mode 100644
index 0000000..be0fd71
--- /dev/null
+++ b/log4j-samples-loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomLookup.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.lookup;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.Marker;
+import org.apache.logging.log4j.MarkerManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.lookup.AbstractLookup;
+import org.apache.logging.log4j.core.lookup.StrLookup;
+import org.apache.logging.log4j.status.StatusLogger;
+
+/**
+ *
+ */
+
+@Plugin(name = "custom", category = StrLookup.CATEGORY)
+public class CustomLookup extends AbstractLookup {
+
+ private static final Logger LOGGER = StatusLogger.getLogger();
+ private static final Marker LOOKUP = MarkerManager.getMarker("LOOKUP");
+
+ private static ConcurrentMap<String, Map<String, String>> loggerProperties = new ConcurrentHashMap<>();
+
+ /**
+ * Looks up the value for the key using the data in the LogEvent.
+ * @param event The current LogEvent.
+ * @param key the key to be looked up, may be null
+ * @return The value associated with the key.
+ */
+ @Override
+ public String lookup(final LogEvent event, final String key) {
+ if (event == null) {
+ return null;
+ }
+ try {
+ final Map<String, String> properties = loggerProperties.get(event.getLoggerName());
+ if (properties == null) {
+ return "";
+ }
+ if (key == null || key.length() == 0 || key.equals("*")) {
+ final StringBuilder sb = new StringBuilder("{");
+ boolean first = true;
+ for (final Map.Entry<String, String> entry : properties.entrySet()) {
+ if (!first) {
+ sb.append(", ");
+ }
+ sb.append(entry.getKey()).append("=").append(entry.getValue());
+ first = false;
+ }
+ sb.append("}");
+ return sb.toString();
+ }
+ return properties.get(key);
+ } catch (final Exception ex) {
+ LOGGER.warn(LOOKUP, "Error while getting property [{}].", key, ex);
+ return null;
+ }
+ }
+
+ public static void setLoggerProperties(final String loggerName, final Map<String, String> properties) {
+ loggerProperties.put(loggerName, properties);
+ }
+
+}
diff --git a/log4j-samples-loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomMapMessage.java b/log4j-samples-loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomMapMessage.java
new file mode 100644
index 0000000..6efdff2
--- /dev/null
+++ b/log4j-samples-loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomMapMessage.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.lookup;
+
+import java.util.Map;
+
+import org.apache.logging.log4j.message.AsynchronouslyFormattable;
+import org.apache.logging.log4j.message.StringMapMessage;
+
+/**
+ *
+ */
+@AsynchronouslyFormattable
+public class CustomMapMessage extends StringMapMessage {
+
+ private static final long serialVersionUID = 1L;
+ private final String message;
+
+ public CustomMapMessage(final String msg, final Map<String, String> map) {
+ super(map);
+ this.message = msg;
+ }
+
+ @Override
+ public String getFormattedMessage(final String[] formats) {
+ if (message != null) {
+ return message;
+ }
+ return super.getFormattedMessage(formats);
+ }
+
+ @Override
+ public String getFormattedMessage() {
+ if (message != null) {
+ return message;
+ }
+ return super.getFormattedMessage();
+ }
+
+}
diff --git a/log4j-samples-loggerProperties/src/main/java/org/apache/logging/log4j/lookup/MapMessageLookup.java b/log4j-samples-loggerProperties/src/main/java/org/apache/logging/log4j/lookup/MapMessageLookup.java
new file mode 100644
index 0000000..cb6bbf9
--- /dev/null
+++ b/log4j-samples-loggerProperties/src/main/java/org/apache/logging/log4j/lookup/MapMessageLookup.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.lookup;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.Marker;
+import org.apache.logging.log4j.MarkerManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.lookup.AbstractLookup;
+import org.apache.logging.log4j.core.lookup.StrLookup;
+import org.apache.logging.log4j.message.MapMessage;
+import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.status.StatusLogger;
+
+/**
+ *
+ */
+
+@Plugin(name = "mapMessage", category = StrLookup.CATEGORY)
+public class MapMessageLookup extends AbstractLookup {
+
+ private static final Logger LOGGER = StatusLogger.getLogger();
+ private static final Marker LOOKUP = MarkerManager.getMarker("LOOKUP");
+
+ private static ConcurrentMap<String, Map<String, String>> loggerProperties = new ConcurrentHashMap<>();
+
+ /**
+ * Looks up the value for the key using the data in the LogEvent.
+ * @param event The current LogEvent.
+ * @param key the key to be looked up, may be null
+ * @return The value associated with the key.
+ */
+ @Override
+ public String lookup(final LogEvent event, final String key) {
+ if (event == null) {
+ return null;
+ }
+ final Message msg = event.getMessage();
+ if (msg instanceof MapMessage) {
+ try {
+ MapMessage<?, ?> mapMessage = (MapMessage) msg;
+ if (key == null || key.length() == 0 || key.equals("*")) {
+ return mapMessage.asString(MapMessage.MapFormat.JAVA_UNQUOTED.name());
+ }
+ return mapMessage.get(key);
+ } catch (final Exception ex) {
+ LOGGER.warn(LOOKUP, "Error while getting property [{}].", key, ex);
+ }
+ }
+ return null;
+ }
+
+ public static void setLoggerProperties(final String loggerName, final Map<String, String> properties) {
+ loggerProperties.put(loggerName, properties);
+ }
+
+}
diff --git a/log4j-samples-loggerProperties/src/test/java/org/apache/logging/log4j/CustomPropertiesTest.java b/log4j-samples-loggerProperties/src/test/java/org/apache/logging/log4j/CustomPropertiesTest.java
new file mode 100644
index 0000000..cae0f69
--- /dev/null
+++ b/log4j-samples-loggerProperties/src/test/java/org/apache/logging/log4j/CustomPropertiesTest.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.logging.log4j.lookup.CustomLookup;
+import org.apache.logging.log4j.lookup.CustomMapMessage;
+
+import org.junit.Test;
+
+/**
+ * Created by rgoers on 8/2/15.
+ */
+public class CustomPropertiesTest {
+
+ @Test
+ public void testProperties() throws Exception {
+ final Logger logger = LogManager.getLogger("TestProperties");
+ final Map<String, String> loggerProperties = new ConcurrentHashMap<>();
+ CustomLookup.setLoggerProperties("TestProperties", loggerProperties);
+ loggerProperties.put("key1", "CustomPropertiesTest");
+ loggerProperties.put("key2", "TestValue");
+ logger.debug("This is a test");
+ }
+
+ @Test
+ public void mapMessageProperties() throws Exception {
+ final Logger logger = LogManager.getLogger("MapProperties");
+ final Map<String, String> loggerProperties = new ConcurrentHashMap<>();
+ loggerProperties.put("key1", "CustomPropertiesTest");
+ loggerProperties.put("key2", "TestValue");
+ logger.debug(new CustomMapMessage("This is a test", loggerProperties));
+ }
+
+
+}
diff --git a/log4j-samples-loggerProperties/src/test/java/org/apache/logging/log4j/MapMessageLookupTest.java b/log4j-samples-loggerProperties/src/test/java/org/apache/logging/log4j/MapMessageLookupTest.java
new file mode 100644
index 0000000..c465e66
--- /dev/null
+++ b/log4j-samples-loggerProperties/src/test/java/org/apache/logging/log4j/MapMessageLookupTest.java
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.lookup.MapMessageLookup;
+import org.apache.logging.log4j.message.MapMessage;
+import org.apache.logging.log4j.message.StringMapMessage;
+import org.apache.logging.log4j.message.StructuredDataMessage;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Tests {@link MapMessageLookup}
+ */
+public class MapMessageLookupTest
+{
+ @Test
+ public void testStructuredDataMessageLookup() {
+ // GIVEN: A StructuredDataMessage object
+ final StructuredDataMessage message = new StructuredDataMessage("id", "msg", "type");
+
+ message.put("A", "a");
+ message.put("B", "b");
+ message.put("C", "c");
+
+ // AND: An event with that message
+ final LogEvent event = Log4jLogEvent.newBuilder().setLevel(Level.DEBUG).setMessage(message).build();
+
+ // AND: A MapMessageLookup object
+ final MapMessageLookup lookup = new MapMessageLookup();
+
+ // WHEN: Lookup is performed
+ final String a = lookup.lookup(event, "A");
+ final String b = lookup.lookup(event, "B");
+ final String c = lookup.lookup(event, "C");
+
+ // THEN: The looked up values are correct
+ assertEquals("a", a);
+ assertEquals("b", b);
+ assertEquals("c", c);
+ }
+
+ @Test
+ public void testStringMapMessageLookup() {
+ // GIVEN: A StringMapMessage object
+ final Map<String, String> values = new HashMap<>(3);
+ values.put("A", "a");
+ values.put("B", "b");
+ values.put("C", "c");
+ final MapMessage message = new StringMapMessage(values);
+
+ // AND: An event with that message
+ final LogEvent event = Log4jLogEvent.newBuilder().setLevel(Level.DEBUG).setMessage(message).build();
+
+ // AND: A MapMessageLookup object
+ final MapMessageLookup lookup = new MapMessageLookup();
+
+ // WHEN: Lookup is performed
+ final String a = lookup.lookup(event, "A");
+ final String b = lookup.lookup(event, "B");
+ final String c = lookup.lookup(event, "C");
+
+ // THEN: The looked up values are correct
+ assertEquals("a", a);
+ assertEquals("b", b);
+ assertEquals("c", c);
+ }
+}
diff --git a/log4j-samples-loggerProperties/src/test/resources/log4j2.xml b/log4j-samples-loggerProperties/src/test/resources/log4j2.xml
new file mode 100644
index 0000000..0aa688b
--- /dev/null
+++ b/log4j-samples-loggerProperties/src/test/resources/log4j2.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<Configuration name="XMLPerfTest" status="error">
+ <Appenders>
+ <File name="TestLogfile" fileName="target/testlog4j2.log" immediateFlush="false">
+ <PatternLayout>
+ <Pattern>%d %5p [%t] %c{1} - %m%n</Pattern>
+ </PatternLayout>
+ </File>
+ <RandomAccessFile name="RandomAccessLogFile" fileName="target/testCustomlog4j2.log" immediateFlush="false">
+ <PatternLayout>
+ <Pattern>%d %5p [%t] %c{1} %X{key1} %X{key2} - %m%n</Pattern>
+ </PatternLayout>
+ </RandomAccessFile>
+ </Appenders>
+ <Loggers>
+ <Logger name="TestProperties" level="debug" additivity="false">
+ <Property name="key1">$${custom:key1}</Property>
+ <Property name="key2">$${custom:key2}</Property>
+ <AppenderRef ref="RandomAccessLogFile"/>
+ </Logger>
+ <Logger name="MapProperties" level="debug" additivity="false">
+ <Property name="key1">$${mapMessage:key1}</Property>
+ <Property name="key2">$${mapMessage:key2}</Property>
+ <AppenderRef ref="RandomAccessLogFile"/>
+ </Logger>
+
+ <Root level="debug">
+ <AppenderRef ref="TestLogfile"/>
+ </Root>
+ </Loggers>
+</Configuration>
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 5e2ecfb..94346f5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -37,6 +37,7 @@
<modules>
<module>log4j-samples-configuration</module>
+ <module>log4j-samples-loggerProperties</module>
</modules>
<properties>