Skip to content

Commit

Permalink
Merge pull request #111 from dmlloyd/logmgr-152
Browse files Browse the repository at this point in the history
[LOGMGR-152] Multi-process support
  • Loading branch information
jamezp committed Apr 26, 2017
2 parents d0ef9b1 + d08bab8 commit ac60c7a
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 86 deletions.
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<parent>
<groupId>org.jboss</groupId>
<artifactId>jboss-parent</artifactId>
<version>16</version>
<version>21</version>
</parent>

<licenses>
Expand All @@ -45,11 +45,17 @@
</licenses>

<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.wildfly.common</groupId>
<artifactId>wildfly-common</artifactId>
<version>1.2.0.Beta10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.modules</groupId>
<artifactId>jboss-modules</artifactId>
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/org/jboss/logmanager/ExtLogRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.jboss.logmanager;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.MessageFormat;
import java.util.Map;
Expand All @@ -28,6 +29,9 @@

import java.util.logging.LogRecord;

import org.wildfly.common.net.HostName;
import org.wildfly.common.os.Process;

/**
* An extended log record, which includes additional information including MDC/NDC and correct
* caller location (even in the presence of a logging facade).
Expand Down Expand Up @@ -80,6 +84,9 @@ public ExtLogRecord(final java.util.logging.Level level, final String msg, final
this.loggerClassName = loggerClassName;
ndc = NDC.get();
threadName = Thread.currentThread().getName();
hostName = HostName.getQualifiedHostName();
processName = Process.getProcessName();
processId = Process.getProcessId();
}

/**
Expand Down Expand Up @@ -111,6 +118,9 @@ public ExtLogRecord(final ExtLogRecord original) {
threadName = original.threadName;
resourceKey = original.resourceKey;
formattedMessage = original.formattedMessage;
hostName = original.hostName;
processName = original.processName;
processId = original.processId;
}

/**
Expand Down Expand Up @@ -141,12 +151,31 @@ public static ExtLogRecord wrap(LogRecord rec) {
private String resourceKey;
private String formattedMessage;
private String threadName;
private String hostName;
private String processName;
private long processId = -1;

private void writeObject(ObjectOutputStream oos) throws IOException {
copyAll();
oos.defaultWriteObject();
}

@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
final ObjectInputStream.GetField fields = ois.readFields();
ndc = (String) fields.get("ndc", null);
formatStyle = (FormatStyle) fields.get("formatStyle", FormatStyle.MESSAGE_FORMAT);
mdcCopy = (FastCopyHashMap<String, Object>) fields.get("mdcCopy", new FastCopyHashMap<>());
sourceLineNumber = fields.get("sourceLineNumber", -1);
sourceFileName = (String) fields.get("sourceFileName", null);
resourceKey = (String) fields.get("resourceKey", null);
formattedMessage = (String) fields.get("formattedMessage", null);
threadName = (String) fields.get("threadName", null);
hostName = (String) fields.get("hostName", null);
processName = (String) fields.get("processName", null);
processId = fields.get("processId", -1L);
}

/**
* Disable caller calculation for this record. If the caller has already been calculated, leave it; otherwise
* set the caller to {@code "unknown"}.
Expand Down Expand Up @@ -469,6 +498,60 @@ public void setThreadName(final String threadName) {
this.threadName = threadName;
}

/**
* Get the host name of the record, if known.
*
* @return the host name of the record, if known
*/
public String getHostName() {
return hostName;
}

/**
* Set the host name of the record.
*
* @param hostName the host name of the record
*/
public void setHostName(final String hostName) {
this.hostName = hostName;
}

/**
* Get the process name of the record, if known.
*
* @return the process name of the record, if known
*/
public String getProcessName() {
return processName;
}

/**
* Set the process name of the record.
*
* @param processName the process name of the record
*/
public void setProcessName(final String processName) {
this.processName = processName;
}

/**
* Get the process ID of the record, if known.
*
* @return the process ID of the record, or -1 if not known
*/
public long getProcessId() {
return processId;
}

/**
* Set the process ID of the record.
*
* @param processId the process ID of the record
*/
public void setProcessId(final long processId) {
this.processId = processId;
}

/**
* Set the raw message. Any cached formatted message is discarded. The parameter format is set to be
* {@link java.text.MessageFormat}-style.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,16 @@ public static FormatStep[] getSteps(final String formatString, ColorMap colors)
break;
}
case 'h': {
stepList.add(Formatters.hostnameFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth, false));
stepList.add(Formatters.hostnameFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth, argument == null ? "1" : argument));
break;
}
case 'H': {
stepList.add(Formatters.hostnameFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth, true));
stepList.add(Formatters.hostnameFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth, argument));
break;
}
case 'i': {
stepList.add(Formatters.processIdFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth));
}
case 'k': {
stepList.add(Formatters.resourceKeyFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth));
break;
Expand Down Expand Up @@ -145,6 +148,10 @@ public static FormatStep[] getSteps(final String formatString, ColorMap colors)
stepList.add(Formatters.lineSeparatorFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth));
break;
}
case 'N': {
stepList.add(Formatters.processNameFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth));
break;
}
case 'p': {
stepList.add(Formatters.levelFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth));
break;
Expand Down
111 changes: 52 additions & 59 deletions src/main/java/org/jboss/logmanager/formatters/Formatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
import static java.security.AccessController.doPrivileged;

import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.SimpleDateFormat;
import java.util.ArrayDeque;
Expand All @@ -36,7 +33,6 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;
import java.util.logging.Formatter;
import java.util.logging.Level;
Expand Down Expand Up @@ -429,6 +425,41 @@ public void renderRaw(final StringBuilder builder, final ExtLogRecord record) {
};
}

/**
* Create a format step which emits the source process name with the given justification rules.
*
* @param leftJustify {@code true} to left justify, {@code false} to right justify
* @param minimumWidth the minimum field width, or 0 for none
* @param truncateBeginning {@code true} to truncate the beginning, otherwise {@code false} to truncate the end
* @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
* @return the format step
*/
public static FormatStep processNameFormatStep(final boolean leftJustify, final int minimumWidth, final boolean truncateBeginning, final int maximumWidth) {
return new JustifyingFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth) {
public void renderRaw(final StringBuilder builder, final ExtLogRecord record) {
builder.append(record.getProcessName());
}
};
}

/**
* Create a format step which emits the source file line number with the given justification rules (NOTE: call stack
* introspection introduces a significant performance penalty).
*
* @param leftJustify {@code true} to left justify, {@code false} to right justify
* @param minimumWidth the minimum field width, or 0 for none
* @param truncateBeginning {@code true} to truncate the beginning, otherwise {@code false} to truncate the end
* @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
* @return the format step
*/
public static FormatStep processIdFormatStep(final boolean leftJustify, final int minimumWidth, final boolean truncateBeginning, final int maximumWidth) {
return new JustifyingFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth) {
public void renderRaw(final StringBuilder builder, final ExtLogRecord record) {
builder.append(record.getProcessId());
}
};
}

/**
* Create a format step which emits the hostname.
*
Expand All @@ -440,29 +471,23 @@ public void renderRaw(final StringBuilder builder, final ExtLogRecord record) {
* @return the format step
*/
public static FormatStep hostnameFormatStep(final boolean leftJustify, final int minimumWidth, final boolean truncateBeginning, final int maximumWidth, final boolean qualified) {
final Properties props;
final Map<String, String> env;
if (System.getSecurityManager() == null) {
props = System.getProperties();
env = System.getenv();
} else {
props = AccessController.doPrivileged(new PrivilegedAction<Properties>() {
@Override
public Properties run() {
return System.getProperties();
}
});
env = AccessController.doPrivileged(new PrivilegedAction<Map<String, String>>() {
@Override
public Map<String, String> run() {
return System.getenv();
}
});
}
final String hostname = findHostname(props, env, qualified);
return new JustifyingFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth) {
public void renderRaw(final StringBuilder builder, final ExtLogRecord record) {
builder.append(hostname);
return hostnameFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth, qualified ? null : "1");
}

/**
* Create a format step which emits the hostname.
*
* @param leftJustify {@code true} to left justify, {@code false} to right justify
* @param minimumWidth the minimum field width, or 0 for none
* @param truncateBeginning {@code true} to truncate the beginning, otherwise {@code false} to truncate the end
* @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
* @param precision the argument used for the class name, may be {@code null} or contain dots to format the class name
* @return the format step
*/
public static FormatStep hostnameFormatStep(final boolean leftJustify, final int minimumWidth, final boolean truncateBeginning, final int maximumWidth, final String precision) {
return new SegmentedFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth, precision) {
public String getSegmentedSubject(final ExtLogRecord record) {
return record.getHostName();
}
};
}
Expand Down Expand Up @@ -1066,38 +1091,6 @@ static Deque<String> parseCategorySegments(final String category) {
return categorySegments;
}

private static String findHostname(final Properties props, final Map<String, String> env, final boolean qualified) {
if (qualified) {
return findQualifiedHostname(props, env);
}
String hostname = props.getProperty("jboss.host.name");
if (hostname == null) {
final String qualifiedHostname = findQualifiedHostname(props, env);
final int index = qualifiedHostname.indexOf('.');
hostname = (index == -1 ? qualifiedHostname : qualifiedHostname.substring(0, index));
}
return hostname;
}

private static String findQualifiedHostname(final Properties props, final Map<String, String> env) {
// First check the system property
String qualifiedHostname = props.getProperty("jboss.qualified.host.name");
if (qualifiedHostname == null) {
qualifiedHostname = env.get("HOSTNAME");
if (qualifiedHostname == null) {
env.get("COMPUTERNAME");
}
if (qualifiedHostname == null) {
try {
qualifiedHostname = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException ignore) {
qualifiedHostname = "unknown-host.unknown-domain";
}
}
}
return qualifiedHostname;
}

static class Segment {
final int len;
final String text;
Expand Down

0 comments on commit ac60c7a

Please sign in to comment.