View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.lookup;
18  
19  import java.util.Locale;
20  
21  import org.apache.logging.log4j.core.LogEvent;
22  import org.apache.logging.log4j.core.config.plugins.Plugin;
23  import org.apache.logging.log4j.util.Strings;
24  
25  /**
26   * Looks up keys related to Java: Java version, JRE version, VM version, and so on.
27   */
28  @Plugin(name = "java", category = StrLookup.CATEGORY)
29  public class JavaLookup extends AbstractLookup {
30  
31      private final SystemPropertiesLookup spLookup = new SystemPropertiesLookup();
32  
33      /**
34       * Accessible through the Lookup key {@code hw}.
35       * @return hardware processor information.
36       */
37      public String getHardware() {
38          return "processors: " + Runtime.getRuntime().availableProcessors() + ", architecture: "
39                  + getSystemProperty("os.arch") + this.getSystemProperty("-", "sun.arch.data.model")
40                  + this.getSystemProperty(", instruction sets: ", "sun.cpu.isalist");
41      }
42  
43      /**
44       * Accessible through the Lookup key {@code locale}.
45       * @return system locale and file encoding information.
46       */
47      public String getLocale() {
48          return "default locale: " + Locale.getDefault() + ", platform encoding: " + getSystemProperty("file.encoding");
49      }
50  
51      /**
52       * Accessible through the Lookup key {@code os}.
53       * @return operating system information.
54       */
55      public String getOperatingSystem() {
56          return getSystemProperty("os.name") + " " + getSystemProperty("os.version")
57                  + getSystemProperty(" ", "sun.os.patch.level") + ", architecture: " + getSystemProperty("os.arch")
58                  + getSystemProperty("-", "sun.arch.data.model");
59      }
60  
61      /**
62       * Accessible through the Lookup key {@code runtime}.
63       * @return Java Runtime Environment information.
64       */
65      public String getRuntime() {
66          return getSystemProperty("java.runtime.name") + " (build " + getSystemProperty("java.runtime.version")
67                  + ") from " + getSystemProperty("java.vendor");
68      }
69  
70      private String getSystemProperty(final String name) {
71          return spLookup.lookup(name);
72      }
73  
74      private String getSystemProperty(final String prefix, final String name) {
75          final String value = getSystemProperty(name);
76          if (Strings.isEmpty(value)) {
77              return Strings.EMPTY;
78          }
79          return prefix + value;
80      }
81  
82      /**
83       * Accessible through the Lookup key {@code vm}.
84       * @return Java Virtual Machine information.
85       */
86      public String getVirtualMachine() {
87          return getSystemProperty("java.vm.name") + " (build " + getSystemProperty("java.vm.version") + ", "
88                  + getSystemProperty("java.vm.info") + ")";
89      }
90  
91      /**
92       * Looks up the value of the environment variable.
93       *
94       * @param event
95       *        The current LogEvent (is ignored by this StrLookup).
96       * @param key
97       *        the key to be looked up, may be null
98       * @return The value of the environment variable.
99       */
100     @Override
101     public String lookup(final LogEvent event, final String key) {
102         // TODO Use a Java 7 switch
103         if ("version".equals(key)) {
104             return "Java version " + getSystemProperty("java.version");
105         } else if ("runtime".equals(key)) {
106             return getRuntime();
107         } else if ("vm".equals(key)) {
108             return getVirtualMachine();
109         } else if ("os".equals(key)) {
110             return getOperatingSystem();
111         } else if ("hw".equals(key)) {
112             return getHardware();
113         } else if ("locale".equals(key)) {
114             return getLocale();
115         }
116         throw new IllegalArgumentException(key);
117     }
118 }