Skip to content

Commit

Permalink
Allow to log the system properties with VersionLoggerListener. This f…
Browse files Browse the repository at this point in the history
…eature is off by default.

Implementation note: this shall work even if keys/values in System properties are not String.
(A recent notable issue caused by non-String values: https://bugs.eclipse.org/bugs/show_bug.cgi?id=445122 )

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1654159 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Konstantin Kolinko committed Jan 23, 2015
1 parent 3f4c043 commit c401826
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
3 changes: 2 additions & 1 deletion java/org/apache/catalina/startup/LocalStrings.properties
Expand Up @@ -132,6 +132,7 @@ versionLoggerListener.vm.version =JVM Version: {0}
versionLoggerListener.vm.vendor =JVM Vendor: {0}
versionLoggerListener.catalina.base =CATALINA_BASE: {0}
versionLoggerListener.catalina.home =CATALINA_HOME: {0}
versionLoggerListener.env =Environment variable: {0} = {1}
versionLoggerListener.arg =Command line argument: {0}
versionLoggerListener.env =Environment variable: {0} = {1}
versionLoggerListener.prop =System property: {0} = {1}
webAnnotationSet.invalidInjection=Invalid method resource injection annotation.
32 changes: 26 additions & 6 deletions java/org/apache/catalina/startup/VersionLoggerListener.java
Expand Up @@ -19,8 +19,8 @@
import java.lang.management.ManagementFactory;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
Expand All @@ -44,6 +44,7 @@ public class VersionLoggerListener implements LifecycleListener {

private boolean logArgs = true;
private boolean logEnv = false;
private boolean logProps = false;


public boolean getLogArgs() {
Expand All @@ -66,6 +67,16 @@ public void setLogEnv(boolean logEnv) {
}


public boolean getLogProps() {
return logProps;
}


public void setLogProps(boolean logProps) {
this.logProps = logProps;
}


@Override
public void lifecycleEvent(LifecycleEvent event) {
if (Lifecycle.BEFORE_INIT_EVENT.equals(event.getType())) {
Expand Down Expand Up @@ -106,10 +117,19 @@ private void log() {
}

if (logEnv) {
Map<String,String> envs = System.getenv();
SortedSet<String> keys = new TreeSet<>(envs.keySet());
for (String key : keys) {
log.info(sm.getString("versionLoggerListener.env", key, envs.get(key)));
SortedMap<String, String> sortedMap = new TreeMap<>(System.getenv());
for (Map.Entry<String, String> e : sortedMap.entrySet()) {
log.info(sm.getString("versionLoggerListener.env", e.getKey(), e.getValue()));
}
}

if (logProps) {
SortedMap<String, String> sortedMap = new TreeMap<>();
for (Map.Entry<Object, Object> e : System.getProperties().entrySet()) {
sortedMap.put(String.valueOf(e.getKey()), String.valueOf(e.getValue()));
}
for (Map.Entry<String, String> e : sortedMap.entrySet()) {
log.info(sm.getString("versionLoggerListener.prop", e.getKey(), e.getValue()));
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions webapps/docs/config/listeners.xml
Expand Up @@ -446,6 +446,12 @@
<code>false</code> will be used.</p>
</attribute>

<attribute name="logProps" required="false">
<p>If <code>true</code>, the current Java system properties will be
logged. If not specified, the default value of
<code>false</code> will be used.</p>
</attribute>

</attributes>

</subsection>
Expand Down

0 comments on commit c401826

Please sign in to comment.