You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2014/09/22 16:24:54 UTC
git commit: [LOG4J2-848] Add a Java lookup to provide nicely
formatted runtime version information.
Repository: logging-log4j2
Updated Branches:
refs/heads/master f418a57fd -> 637826464
[LOG4J2-848] Add a Java lookup to provide nicely formatted runtime
version information.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/63782646
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/63782646
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/63782646
Branch: refs/heads/master
Commit: 6378264646c309b0063472830062d32e663b188e
Parents: f418a57
Author: Gary Gregory <ga...@gmail.com>
Authored: Mon Sep 22 10:24:50 2014 -0400
Committer: Gary Gregory <ga...@gmail.com>
Committed: Mon Sep 22 10:24:50 2014 -0400
----------------------------------------------------------------------
.../logging/log4j/core/lookup/Interpolator.java | 1 +
.../logging/log4j/core/lookup/JavaLookup.java | 98 ++++++++++++++++++++
.../log4j/core/layout/PatternLayoutTest.java | 29 +++++-
.../log4j/core/lookup/InterpolatorTest.java | 13 +++
src/changes/changes.xml | 3 +
5 files changed, 141 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/63782646/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
index 0282bd0..c437ea8 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
@@ -85,6 +85,7 @@ public class Interpolator extends AbstractLookup {
lookups.put("sys", new SystemPropertiesLookup());
lookups.put("env", new EnvironmentLookup());
lookups.put("main", MapLookup.MAIN_SINGLETON);
+ lookups.put("java", new JavaLookup());
// JNDI
try {
// [LOG4J2-703] We might be on Android
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/63782646/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JavaLookup.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JavaLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JavaLookup.java
new file mode 100644
index 0000000..8ab1428
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JavaLookup.java
@@ -0,0 +1,98 @@
+/*
+ * 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 java.util.Locale;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.util.Strings;
+
+/**
+ * Looks up keys related to Java: Java version, JRE version, VM version, and so on.
+ */
+@Plugin(name = "java", category = "Lookup")
+public class JavaLookup extends AbstractLookup {
+
+ private final SystemPropertiesLookup spLookup = new SystemPropertiesLookup();
+
+ public String getHardware() {
+ return "processors: " + Runtime.getRuntime().availableProcessors() + ", architecture: "
+ + getSystemProperty("os.arch") + this.getSystemProperty("-", "sun.arch.data.model")
+ + this.getSystemProperty(", instruction sets: ", "sun.cpu.isalist");
+ }
+
+ public String getLocale() {
+ return "default locale: " + Locale.getDefault() + ", platform encoding: " + getSystemProperty("file.encoding");
+ }
+
+ public String getOperatingSystem() {
+ return getSystemProperty("os.name") + " " + getSystemProperty("os.version")
+ + getSystemProperty(" ", "sun.os.patch.level") + ", architecture: " + getSystemProperty("os.arch")
+ + getSystemProperty("-", "sun.arch.data.model");
+ }
+
+ public String getRuntime() {
+ return getSystemProperty("java.runtime.name") + " (build " + getSystemProperty("java.runtime.version")
+ + ") from " + getSystemProperty("java.vendor");
+ }
+
+ private String getSystemProperty(final String name) {
+ return spLookup.lookup(name);
+ }
+
+ private String getSystemProperty(final String prefix, final String name) {
+ final String value = getSystemProperty(name);
+ if (Strings.isEmpty(value)) {
+ return Strings.EMPTY;
+ }
+ return prefix + value;
+ }
+
+ public String getVirtualMachine() {
+ return getSystemProperty("java.vm.name") + " (build " + getSystemProperty("java.vm.version") + ", "
+ + getSystemProperty("java.vm.info") + ")";
+ }
+
+ /**
+ * Looks up the value of the environment variable.
+ *
+ * @param event
+ * The current LogEvent (is ignored by this StrLookup).
+ * @param key
+ * the key to be looked up, may be null
+ * @return The value of the environment variable.
+ */
+ @Override
+ public String lookup(final LogEvent event, final String key) {
+ // TODO Use a Java 7 switch
+ if ("version".equals(key)) {
+ return "Java version " + getSystemProperty("java.version");
+ } else if ("runtime".equals(key)) {
+ return getRuntime();
+ } else if ("vm".equals(key)) {
+ return getVirtualMachine();
+ } else if ("os".equals(key)) {
+ return getOperatingSystem();
+ } else if ("hw".equals(key)) {
+ return getHardware();
+ } else if ("locale".equals(key)) {
+ return getLocale();
+ }
+ throw new IllegalArgumentException(key);
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/63782646/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
index 0d12555..d75a573 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
@@ -162,7 +162,7 @@ public class PatternLayoutTest {
}
@Test
- public void testHeaderFooter() throws Exception {
+ public void testHeaderFooterThreadContext() throws Exception {
final PatternLayout layout = PatternLayout.newBuilder().withPattern("%d{UNIX} %m")
.withConfiguration(ctx.getConfiguration()).withHeader("${ctx:header}").withFooter("${ctx:footer}")
.build();
@@ -175,6 +175,29 @@ public class PatternLayoutTest {
}
@Test
+ public void testHeaderFooterJavaLookup() throws Exception {
+ // % does not work here.
+ String pattern = "%d{UNIX} MyApp%n${java:version}%n${java:runtime}%n${java:vm}%n${java:os}%n${java:hw}";
+ final PatternLayout layout = PatternLayout.newBuilder().withConfiguration(ctx.getConfiguration())
+ .withHeader(pattern).withFooter(pattern).build();
+ final byte[] header = layout.getHeader();
+ assertNotNull("No header", header);
+ String headerStr = new String(header);
+ assertTrue(headerStr, headerStr.contains("Java version "));
+ assertTrue(headerStr, headerStr.contains("(build "));
+ assertTrue(headerStr, headerStr.contains(" from "));
+ assertTrue(headerStr, headerStr.contains(" architecture: "));
+ //
+ final byte[] footer = layout.getFooter();
+ assertNotNull("No header", footer);
+ String footerStr = new String(header);
+ assertTrue(footerStr, footerStr.contains("Java version "));
+ assertTrue(footerStr, footerStr.contains("(build "));
+ assertTrue(footerStr, footerStr.contains(" from "));
+ assertTrue(footerStr, footerStr.contains(" architecture: "));
+ }
+
+ @Test
public void testSpecialChars() throws Exception {
final PatternLayout layout = PatternLayout.newBuilder().withPattern("\\\\%level\\t%msg\\n\\t%logger\\r\\n\\f")
.withConfiguration(ctx.getConfiguration()).build();
@@ -184,14 +207,14 @@ public class PatternLayoutTest {
assertEquals("\\INFO\tHello, world!\n\torg.apache.logging.log4j.core.layout.PatternLayoutTest\r\n\f",
new String(result));
}
-
+
@Test
public void testUsePlatformDefaultIfNoCharset() throws Exception {
final PatternLayout layout = PatternLayout.newBuilder().withPattern("%m")
.withConfiguration(ctx.getConfiguration()).build();
assertEquals(Charset.defaultCharset(), layout.getCharset());
}
-
+
@Test
public void testUseSpecifiedCharsetIfExists() throws Exception {
final PatternLayout layout = PatternLayout.newBuilder().withPattern("%m")
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/63782646/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java
index c5bbcea..a4f6a22 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java
@@ -87,6 +87,13 @@ public class InterpolatorTest {
assertEquals(TEST_CONTEXT_NAME, value);
}
+ private void assertLookupNotEmpty(StrLookup lookup, String key) {
+ final String value = lookup.lookup(key);
+ assertNotNull(value);
+ assertFalse(value.isEmpty());
+ System.out.println(key + " = " + value);
+ }
+
@Test
public void testLookupWithDefaultInterpolator() {
final StrLookup lookup = new Interpolator();
@@ -101,5 +108,11 @@ public class InterpolatorTest {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
final String today = format.format(new Date());
assertEquals(value, today);
+ assertLookupNotEmpty(lookup, "java:version");
+ assertLookupNotEmpty(lookup, "java:runtime");
+ assertLookupNotEmpty(lookup, "java:vm");
+ assertLookupNotEmpty(lookup, "java:os");
+ assertLookupNotEmpty(lookup, "java:locale");
+ assertLookupNotEmpty(lookup, "java:hw");
}
}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/63782646/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 2f4896b..cc1c94e 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -24,6 +24,9 @@
</properties>
<body>
<release version="2.1" date="2014-??-??" description="Bug fixes and enhancements">
+ <action issue="LOG4J2-848" dev="ggregory" type="add">
+ Add a Java lookup to provide nicely formatted runtime version information.
+ </action>
<action issue="LOG4J2-809" dev="mattsicker" type="add">
Move reflection utility class to API's private utility classes.
</action>