1 18 19 package org.apache.jmeter.functions; 20 21 import java.io.Serializable ; 22 import java.util.Collection ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 26 import org.apache.jmeter.engine.util.CompoundVariable; 27 import org.apache.jmeter.samplers.SampleResult; 28 import org.apache.jmeter.samplers.Sampler; 29 import org.apache.jorphan.logging.LoggingManager; 30 import org.apache.log.Logger; 31 import org.apache.log.Priority; 32 33 46 public class LogFunction extends AbstractFunction implements Serializable 47 { 48 private static Logger log = LoggingManager.getLoggerForClass(); 49 50 private static final List desc = new LinkedList (); 51 private static final String KEY = "__log"; 52 53 private static final int MIN_PARAMETER_COUNT = 1; 55 private static final int MAX_PARAMETER_COUNT = 3; 56 static { 57 desc.add("String to be logged"); 58 desc.add("Log level (default INFO)"); 59 desc.add("Throwable text (optional)"); 60 } 61 private static final String DEFAULT_PRIORITY = "INFO"; 63 private Object [] values; 64 65 public LogFunction() 66 { 67 } 68 69 public Object clone() 70 { 71 return new LogFunction(); 72 } 73 74 public synchronized String execute( 75 SampleResult previousResult, 76 Sampler currentSampler) 77 throws InvalidVariableException 78 { 79 String stringToLog = ((CompoundVariable) values[0]).execute(); 80 81 String priorityString; 82 if (values.length > 1){ priorityString= ((CompoundVariable) values[1]).execute(); 84 if (priorityString.length()==0) priorityString= DEFAULT_PRIORITY; 85 } else { 86 priorityString = DEFAULT_PRIORITY; 87 } 88 89 Throwable t=null; 90 if (values.length > 2){ t = new Throwable (((CompoundVariable) values[2]).execute()); 92 } 93 94 logDetails(log,stringToLog,priorityString,t); 95 96 return stringToLog; 97 98 } 99 100 private static void printDetails(java.io.PrintStream ps,String s, Throwable t) 102 { 103 String tn = Thread.currentThread().getName(); 104 if (t != null) 105 { 106 ps.print("Log: "+ tn + " : " + s + " "); 107 t.printStackTrace(ps); 108 } 109 else 110 { 111 ps.println("Log: "+ tn + " : " + s); 112 } 113 } 114 115 static void logDetails(Logger l,String s,String prio,Throwable t) 117 { 118 if (prio.equalsIgnoreCase("OUT")) { 120 printDetails(System.out,s,t); 121 } 122 else 123 if (prio.equalsIgnoreCase("ERR")) { 125 printDetails(System.err,s,t); 126 } 127 else 128 { 129 Priority p = Priority.getPriorityForName(prio); 131 if (log.isPriorityEnabled(p)){ String tn = Thread.currentThread().getName(); 133 log.log(p,tn+" "+s,t); 134 } 135 } 136 137 } 138 139 public void setParameters(Collection parameters) 140 throws InvalidVariableException 141 { 142 143 values = parameters.toArray(); 144 145 if ((values.length < MIN_PARAMETER_COUNT) 146 || (values.length > MAX_PARAMETER_COUNT)) 147 { 148 throw new InvalidVariableException( 149 "Parameter Count not between " 150 + MIN_PARAMETER_COUNT 151 + " & " 152 + MAX_PARAMETER_COUNT); 153 } 154 155 } 156 157 public String getReferenceKey() 158 { 159 return KEY; 160 } 161 162 public List getArgumentDesc() 163 { 164 return desc; 165 } 166 167 } | Popular Tags |