Add optional comment to __log() function

git-svn-id: https://svn.apache.org/repos/asf/jakarta/jmeter/trunk@600613 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2007-12-03 17:29:54 +00:00
parent 4862e75ea3
commit e965ac7d28
4 changed files with 79 additions and 30 deletions

View File

@ -26,16 +26,25 @@ import java.util.List;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.apache.log.Priority;
/**
* Function to log a message
*
* Parameters: - string - log level (optional; defaults to INFO; or DEBUG if
* unrecognised) - throwable message (optional)
* <p>
* Function to log a message.
* </p>
*
* <p>
* Parameters:
* <ul>
* <li>string value</li>
* <li>log level (optional; defaults to INFO; or DEBUG if unrecognised; or can use OUT or ERR)</li>
* <li>throwable message (optional)</li>
* <li>comment (optional)</li>
* </ul>
* </p>
* Returns: - the input string
*
*/
@ -49,15 +58,18 @@ public class LogFunction extends AbstractFunction implements Serializable {
// Number of parameters expected - used to reject invalid calls
private static final int MIN_PARAMETER_COUNT = 1;
private static final int MAX_PARAMETER_COUNT = 3;
private static final int MAX_PARAMETER_COUNT = 4;
static {
desc.add("String to be logged");
desc.add("Log level (default INFO)");
desc.add("Throwable text (optional)");
desc.add(JMeterUtils.getResString("log_function_string_ret")); //$NON-NLS-1$
desc.add(JMeterUtils.getResString("log_function_level")); //$NON-NLS-1$
desc.add(JMeterUtils.getResString("log_function_throwable")); //$NON-NLS-1$
desc.add(JMeterUtils.getResString("log_function_comment")); //$NON-NLS-1$
}
private static final String DEFAULT_PRIORITY = "INFO"; //$NON-NLS-1$
private static final String DEFAULT_SEPARATOR = " : "; //$NON-NLS-1$
private Object[] values;
public LogFunction() {
@ -82,41 +94,66 @@ public class LogFunction extends AbstractFunction implements Serializable {
Throwable t = null;
if (values.length > 2) { // Throwable wanted
t = new Throwable(((CompoundVariable) values[2]).execute());
String value = ((CompoundVariable) values[2]).execute();
if (value.length() > 0) t = new Throwable(value);
}
logDetails(log, stringToLog, priorityString, t);
String comment = "";
if (values.length > 3) { // Comment wanted
comment = ((CompoundVariable) values[3]).execute();
}
logDetails(log, stringToLog, priorityString, t, comment);
return stringToLog;
}
// Common output function
private static void printDetails(java.io.PrintStream ps, String s, Throwable t) {
private static void printDetails(java.io.PrintStream ps, String s, Throwable t, String c) {
String tn = Thread.currentThread().getName();
StringBuffer sb = new StringBuffer(80);
sb.append("Log: ");
sb.append(tn);
if (c.length()>0){
sb.append(" ");
sb.append(c);
} else {
sb.append(DEFAULT_SEPARATOR);
}
if (t != null) {
ps.print("Log: " + tn + " : " + s + " ");
sb.append(" ");
ps.print(sb.toString());
t.printStackTrace(ps);
} else {
ps.println("Log: " + tn + " : " + s);
ps.print(sb.toString());
}
}
// Routine to perform the output (also used by __logn() function)
static void logDetails(Logger l, String s, String prio, Throwable t) {
static void logDetails(Logger l, String s, String prio, Throwable t, String c) {
if (prio.equalsIgnoreCase("OUT")) //$NON-NLS-1
{
printDetails(System.out, s, t);
printDetails(System.out, s, t, c);
} else if (prio.equalsIgnoreCase("ERR")) //$NON-NLS-1
{
printDetails(System.err, s, t);
printDetails(System.err, s, t, c);
} else {
// N.B. if the string is not recognised, DEBUG is assumed
Priority p = Priority.getPriorityForName(prio);
if (log.isPriorityEnabled(p)) {// Thread method is potentially
// expensive
if (log.isPriorityEnabled(p)) {// Thread method is potentially expensive
String tn = Thread.currentThread().getName();
log.log(p, tn + " " + s, t);
StringBuffer sb = new StringBuffer(40);
sb.append(tn);
if (c.length()>0){
sb.append(" ");
sb.append(c);
} else {
sb.append(DEFAULT_SEPARATOR);
}
sb.append(s);
log.log(p, sb.toString(), t);
}
}

View File

@ -26,17 +26,24 @@ import java.util.List;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
/**
* Function to log a message
* <p>
* Function to log a message.
* </p>
*
* Parameters: - string - log level (optional; defaults to INFO; or DEBUG if
* unrecognised) - throwable message (optional)
*
* Returns: - Empty String (so can be used where return value would be a
* nuisance)
* <p>
* Parameters:
* <ul>
* <li>string value</li>
* <li>log level (optional; defaults to INFO; or DEBUG if unrecognised; or can use OUT or ERR)</li>
* <li>throwable message (optional)</li>
* </ul>
* </p>
* Returns: - Empty String (so can be used where return value would be a nuisance)
*
*/
public class LogFunction2 extends AbstractFunction implements Serializable {
@ -51,9 +58,9 @@ public class LogFunction2 extends AbstractFunction implements Serializable {
private static final int MAX_PARAMETER_COUNT = 3;
static {
desc.add("String to be logged");
desc.add("Log level (default INFO)");
desc.add("Throwable text (optional)");
desc.add(JMeterUtils.getResString("log_function_string")); //$NON-NLS-1$
desc.add(JMeterUtils.getResString("log_function_level")); //$NON-NLS-1$
desc.add(JMeterUtils.getResString("log_function_throwable")); //$NON-NLS-1$
}
private static final String DEFAULT_PRIORITY = "INFO"; //$NON-NLS-1$
@ -85,7 +92,7 @@ public class LogFunction2 extends AbstractFunction implements Serializable {
t = new Throwable(((CompoundVariable) values[2]).execute());
}
LogFunction.logDetails(log, stringToLog, priorityString, t);
LogFunction.logDetails(log, stringToLog, priorityString, t, "");
return "";

View File

@ -49,6 +49,7 @@
<li>longSum() function added</li>
<li>Bug 43382 - configure Tidy output (warnings, errors) for XPath Assertion and Post-Processor</li>
<li>Bug 43984 - trim spaces from port field</li>
<li>Add optional comment to __log() function</li>
</ul>
<h4>Non-functional changes</h4>
@ -57,6 +58,7 @@
<li>Build process now detects missing 3rd party libraries and reports need for both binary and source archives</li>
<li>Skip BeanShell tests if jar is not present</li>
<li>Update to Xalan 2.7.1</li>
<li>Use properties for log/logn function descriptions</li>
</ul>
<!-- =================== -->

View File

@ -572,14 +572,17 @@ the second would return 1 and the last would return www.dummy.org
<property name="String to be logged" required="Yes">A string</property>
<property name="Log Level" required="No">OUT, ERR, DEBUG, INFO (default), WARN or ERROR</property>
<property name="Throwable text" required="No">If non-empty, creates a Throwable to pass to the logger</property>
<property name="Comment" required="No">If present, it is displayed in the string.
Useful for identifying what is being logged.</property>
</properties>
<p>The OUT and ERR log level names are used to direct the output to System.out and System.err respectively.
In this case, the output is always printed - it does not depend on the current log setting.
</p>
<pre>
For example:
${__log(Message)} - written to the log file
${__log(Message)} - written to the log file as "...thread Name : Message"
${__log(Message,OUT)} - written to console window
${__log(${VAR},,,VAR=)} - written to log file as "...thread Name VAR=value"
</pre>
</component>