1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
35
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
45
46
47 public String getLocale() {
48 return "default locale: " + Locale.getDefault() + ", platform encoding: " + getSystemProperty("file.encoding");
49 }
50
51
52
53
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
63
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
84
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
93
94
95
96
97
98
99
100 @Override
101 public String lookup(final LogEvent event, final String key) {
102
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 }