1
17 package org.apache.catalina.startup;
18
19 import java.lang.management.ManagementFactory;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.SortedMap;
23 import java.util.TreeMap;
24
25 import org.apache.catalina.Lifecycle;
26 import org.apache.catalina.LifecycleEvent;
27 import org.apache.catalina.LifecycleListener;
28 import org.apache.catalina.util.ServerInfo;
29 import org.apache.juli.logging.Log;
30 import org.apache.juli.logging.LogFactory;
31 import org.apache.tomcat.util.res.StringManager;
32
33
36 public class VersionLoggerListener implements LifecycleListener {
37
38 private static final Log log = LogFactory.getLog(VersionLoggerListener.class);
39
40
43 protected static final StringManager sm = StringManager.getManager(Constants.Package);
44
45 private boolean logArgs = true;
46 private boolean logEnv = false;
47 private boolean logProps = false;
48
49
50 public boolean getLogArgs() {
51 return logArgs;
52 }
53
54
55 public void setLogArgs(boolean logArgs) {
56 this.logArgs = logArgs;
57 }
58
59
60 public boolean getLogEnv() {
61 return logEnv;
62 }
63
64
65 public void setLogEnv(boolean logEnv) {
66 this.logEnv = logEnv;
67 }
68
69
70 public boolean getLogProps() {
71 return logProps;
72 }
73
74
75 public void setLogProps(boolean logProps) {
76 this.logProps = logProps;
77 }
78
79
80 @Override
81 public void lifecycleEvent(LifecycleEvent event) {
82 if (Lifecycle.BEFORE_INIT_EVENT.equals(event.getType())) {
83 log();
84 }
85 }
86
87
88 private void log() {
89 log.info(sm.getString("versionLoggerListener.serverInfo.server.version",
90 ServerInfo.getServerInfo()));
91 log.info(sm.getString("versionLoggerListener.serverInfo.server.built",
92 ServerInfo.getServerBuilt()));
93 log.info(sm.getString("versionLoggerListener.serverInfo.server.number",
94 ServerInfo.getServerNumber()));
95 log.info(sm.getString("versionLoggerListener.os.name",
96 System.getProperty("os.name")));
97 log.info(sm.getString("versionLoggerListener.os.version",
98 System.getProperty("os.version")));
99 log.info(sm.getString("versionLoggerListener.os.arch",
100 System.getProperty("os.arch")));
101 log.info(sm.getString("versionLoggerListener.java.home",
102 System.getProperty("java.home")));
103 log.info(sm.getString("versionLoggerListener.vm.version",
104 System.getProperty("java.runtime.version")));
105 log.info(sm.getString("versionLoggerListener.vm.vendor",
106 System.getProperty("java.vm.vendor")));
107 log.info(sm.getString("versionLoggerListener.catalina.base",
108 System.getProperty("catalina.base")));
109 log.info(sm.getString("versionLoggerListener.catalina.home",
110 System.getProperty("catalina.home")));
111
112 if (logArgs) {
113 List<String> args = ManagementFactory.getRuntimeMXBean().getInputArguments();
114 for (String arg : args) {
115 log.info(sm.getString("versionLoggerListener.arg", arg));
116 }
117 }
118
119 if (logEnv) {
120 SortedMap<String, String> sortedMap = new TreeMap<>(System.getenv());
121 for (Map.Entry<String, String> e : sortedMap.entrySet()) {
122 log.info(sm.getString("versionLoggerListener.env", e.getKey(), e.getValue()));
123 }
124 }
125
126 if (logProps) {
127 SortedMap<String, String> sortedMap = new TreeMap<>();
128 for (Map.Entry<Object, Object> e : System.getProperties().entrySet()) {
129 sortedMap.put(String.valueOf(e.getKey()), String.valueOf(e.getValue()));
130 }
131 for (Map.Entry<String, String> e : sortedMap.entrySet()) {
132 log.info(sm.getString("versionLoggerListener.prop", e.getKey(), e.getValue()));
133 }
134 }
135 }
136 }
137