You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2020/03/28 07:52:10 UTC
[logging-log4j2] branch master updated: LOG4J2-2807 - Add
EventLookup to retrieve fields from the log event
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new a23aacf LOG4J2-2807 - Add EventLookup to retrieve fields from the log event
a23aacf is described below
commit a23aacf0f3195fbd5cfa9608430acaeab5d3f58e
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sat Mar 28 00:51:52 2020 -0700
LOG4J2-2807 - Add EventLookup to retrieve fields from the log event
---
.../logging/log4j/core/lookup/EventLookup.java | 72 +++++++++++++
.../logging/log4j/core/lookup/EventLookupTest.java | 117 +++++++++++++++++++++
src/changes/changes.xml | 3 +
src/site/asciidoc/manual/lookups.adoc | 74 +++++++++++++
src/site/site.xml | 3 +
5 files changed, 269 insertions(+)
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/EventLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/EventLookup.java
new file mode 100644
index 0000000..134de26
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/EventLookup.java
@@ -0,0 +1,72 @@
+/*
+ * 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.core.lookup;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.plugins.Plugin;
+
+/**
+ * Looks up values from the log event.
+ */
+@Plugin(name = "event", category = StrLookup.CATEGORY)
+public class EventLookup extends AbstractLookup {
+
+ /**
+ * Looks up the value from the logging event.
+ * @param event The current LogEvent.
+ * @param key the key to be looked up.
+ * @return The value of the specified log event field.
+ */
+ @Override
+ public String lookup(final LogEvent event, final String key) {
+ switch (key) {
+ case "Marker": {
+ return event.getMarker() != null ? event.getMarker().getName() : null;
+ }
+ case "ThreadName": {
+ return event.getThreadName();
+ }
+ case "Level": {
+ return event.getLevel().toString();
+ }
+ case "ThreadId": {
+ return Long.toString(event.getThreadId());
+ }
+ case "Timestamp": {
+ return Long.toString(event.getTimeMillis());
+ }
+ case "Exception": {
+ if (event.getThrown() != null) {
+ return event.getThrown().getClass().getSimpleName();
+ }
+ if (event.getThrownProxy() != null) {
+ return event.getThrownProxy().getName();
+ }
+ return null;
+ }
+ case "Logger": {
+ return event.getLoggerName();
+ }
+ case "Message": {
+ return event.getMessage().getFormattedMessage();
+ }
+ default: {
+ return null;
+ }
+ }
+ }
+}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/EventLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/EventLookupTest.java
new file mode 100644
index 0000000..96f6027
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/EventLookupTest.java
@@ -0,0 +1,117 @@
+/*
+ * 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.core.lookup;
+
+import org.apache.logging.log4j.Level;
+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.impl.Log4jLogEvent;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+/**
+ * Tests {@link MarkerLookup}.
+ *
+ * @since 2.4
+ */
+public class EventLookupTest {
+
+ private static final String ABSENT_MARKER_NAME = "NONE";
+ private final String markerName = "EventLookupTest";
+ private final StrLookup strLookup = new EventLookup();
+
+ @Test
+ public void testLookupEventMarker() {
+ final Marker marker = MarkerManager.getMarker(markerName);
+ final LogEvent event = Log4jLogEvent.newBuilder() //
+ .setLoggerName(this.getClass().getName()) //
+ .setMarker(marker) //
+ .setLoggerFqcn("org.apache.logging.log4j.core.Logger") //
+ .setLevel(Level.INFO) //
+ .setMessage(new SimpleMessage("Hello, world!")).build();
+ final String value = strLookup.lookup(event, "Marker");
+ assertEquals(markerName, value);
+ }
+
+ @Test
+ public void testLookupEventMessage() {
+ String msg = "Hello, world!";
+ final LogEvent event = Log4jLogEvent.newBuilder() //
+ .setLoggerName(this.getClass().getName()) //
+ .setLoggerFqcn("org.apache.logging.log4j.core.Logger") //
+ .setLevel(Level.INFO) //
+ .setMessage(new SimpleMessage(msg)).build();
+ final String value = strLookup.lookup(event, "Message");
+ assertEquals(msg, value);
+ }
+
+ @Test
+ public void testLookupEventLevel() {
+ String msg = "Hello, world!";
+ final LogEvent event = Log4jLogEvent.newBuilder() //
+ .setLoggerName(this.getClass().getName()) //
+ .setLoggerFqcn("org.apache.logging.log4j.core.Logger") //
+ .setLevel(Level.INFO) //
+ .setMessage(new SimpleMessage(msg)).build();
+ final String value = strLookup.lookup(event, "Level");
+ assertEquals(Level.INFO.toString(), value);
+ }
+
+ @Test
+ public void testLookupEventTimestamp() {
+ String msg = "Hello, world!";
+ long now = System.currentTimeMillis();
+ final LogEvent event = Log4jLogEvent.newBuilder() //
+ .setLoggerName(this.getClass().getName()) //
+ .setTimeMillis(now)
+ .setLoggerFqcn("org.apache.logging.log4j.core.Logger") //
+ .setLevel(Level.INFO) //
+ .setMessage(new SimpleMessage(msg)).build();
+ final String value = strLookup.lookup(event, "Timestamp");
+ assertEquals(Long.toString(now), value);
+ }
+
+ @Test
+ public void testLookupEventLogger() {
+ String msg = "Hello, world!";
+ final LogEvent event = Log4jLogEvent.newBuilder() //
+ .setLoggerName(this.getClass().getName()) //
+ .setLoggerFqcn("org.apache.logging.log4j.core.Logger") //
+ .setLevel(Level.INFO) //
+ .setMessage(new SimpleMessage(msg)).build();
+ final String value = strLookup.lookup(event, "Logger");
+ assertEquals(this.getClass().getName(), value);
+ }
+
+ @Test
+ public void testLookupEventThreadName() {
+ String msg = "Hello, world!";
+ final LogEvent event = Log4jLogEvent.newBuilder() //
+ .setLoggerName(this.getClass().getName()) //
+ .setThreadName("Main")
+ .setLoggerFqcn("org.apache.logging.log4j.core.Logger") //
+ .setLevel(Level.INFO) //
+ .setMessage(new SimpleMessage(msg)).build();
+ final String value = strLookup.lookup(event, "ThreadName");
+ assertEquals("Main", value);
+ }
+
+}
\ No newline at end of file
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c5ad908..f9994c9 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -169,6 +169,9 @@
</action>
</release>
<release version="2.13.2" date="2020-MM-DD" description="GA Release 2.13.2">
+ <action issue="LOG4J2-2807" dev="rgoers" type="add">
+ Added EventLookup to retrieve fields from the log event.
+ </action>
<action issue="LOG4J2-2805" dev="rgoers" type="fix">
TimeFilter did not handle daylight saving time transitions and did not support a range over 2 days.
</action>
diff --git a/src/site/asciidoc/manual/lookups.adoc b/src/site/asciidoc/manual/lookups.adoc
index 1efc99a..5201c8d 100644
--- a/src/site/asciidoc/manual/lookups.adoc
+++ b/src/site/asciidoc/manual/lookups.adoc
@@ -136,6 +136,80 @@ when the `USER` environment variable is undefined, the default value
</PatternLayout>
</File>
----
+[#EventLookup]
+== Event Lookup
+
+The EventLookup provides access to fields within the log event from the configuration.
+
+[cols="1m,4a"]
+|===
+|Key |Description
+
+|Exception
+|Returns the simple class name of the Exception, if one is included in the event.
+
+|Level
+|Returns the logging Level of the event.
+
+|Logger
+|Returns the name of the Logger.
+
+|Marker
+|Returns the name of the Marker associated with the log event, if one is present.
+
+|Message
+|Returns the formatted Message string.
+
+|ThreadId
+|Returns the thread id associated with the log event.
+
+|ThreadName
+|Returns the name of the thread associate with the log event.
+
+|Timestamp
+|Returns the time in milliseconds when the event occurred.
+
+|===
+
+In this example the RoutingAppender picks a route based on the presence of a Marker named "AUDIT" being
+present in the log event.
+[source,prettyprint,linenums]
+----
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="WARN" name="RoutingTest">
+ <Appenders>
+ <Console name="STDOUT" target="SYSTEM_OUT" />
+ <Flume name="AuditLogger" compress="true">
+ <Agent host="192.168.10.101" port="8800"/>
+ <Agent host="192.168.10.102" port="8800"/>
+ <RFC5424Layout enterpriseNumber="18060" includeMDC="true" appName="MyApp"/>
+ </Flume>
+ <Routing name="Routing">
+ <Routes>
+ <Route pattern="$${event:Marker}">
+ <RollingFile
+ name="Rolling-${mdc:UserId}"
+ fileName="${mdc:UserId}.log"
+ filePattern="${mdc:UserId}.%i.log.gz">
+ <PatternLayout>
+ <pattern>%d %p %c{1.} [%t] %m%n</pattern>
+ </PatternLayout>
+ <SizeBasedTriggeringPolicy size="500" />
+ </RollingFile>
+ </Route>
+ <Route ref="AuditLogger" key="AUDIT"/>
+ <Route ref="STDOUT" key="STDOUT"/>
+ </Routes>
+ <IdlePurgePolicy timeToLive="15" timeUnit="minutes"/>
+ </Routing>
+ </Appenders>
+ <Loggers>
+ <Root level="error">
+ <AppenderRef ref="Routing" />
+ </Root>
+ </Loggers>
+</Configuration>
+----
[#JavaLookup]
== Java Lookup
diff --git a/src/site/site.xml b/src/site/site.xml
index 63d7403..2558cb3 100644
--- a/src/site/site.xml
+++ b/src/site/site.xml
@@ -144,16 +144,19 @@
<item name="Date" href="/manual/lookups.html#DateLookup"/>
<item name="Docker" href="manual/lookups.html#DockerLookup"/>
<item name="Environment" href="/manual/lookups.html#EnvironmentLookup"/>
+ <item name="Event" href="/manual/lookups.html#EventLookup"/>
<item name="Java" href="/manual/lookups.html#JavaLookup"/>
<item name="JNDI" href="/manual/lookups.html#JndiLookup"/>
<item name="JVM Arguments" href="/manual/lookups.html#JmxRuntimeInputArgumentsLookup"/>
<item name="Kubernetes" href="/manual/lookups.html#KubernetesLookup"/>
<item name="Log4j Config" href="/manual/lookups.html#Log4jConfigLookup"/>
+ <item name="Lower" href="/manual/lookups.html#LowerLookup"/>
<item name="Main Arguments" href="/manual/lookups.html#AppMainArgsLookup"/>
<item name="Map" href="/manual/lookups.html#MapLookup"/>
<item name="Spring" href="/manual/lookups.html#SpringLookup"/>
<item name="Structured Data" href="/manual/lookups.html#StructuredDataLookup"/>
<item name="System Properties" href="/manual/lookups.html#SystemPropertiesLookup"/>
+ <item name="Upper" href="/manual/lookups.html#UpperLookup"/>
<item name="Web" href="/manual/lookups.html#WebLookup"/>
</item>