我有下面的btrace脚本。我想记录特定类中函数的进入和退出。
..
package com.sun.btrace.samples;
import com.sun.btrace.BTraceUtils;
import com.sun.btrace.Profiler;
import com.sun.btrace.annotations.*;
@BTrace class Profiling {
@Property
Profiler swingProfiler = BTraceUtils.Profiling.newProfiler();
@OnMethod(
clazz="com.pkg.classname",
method="/.*/")
void entry(@ProbeMethodName(fqn=true) String probeMethod) {
BTraceUtils.print("Entry" );
BTraceUtils.println(BTraceUtils.timestamp() );
BTraceUtils.println(probeMethod);
}
@OnMethod(
clazz="com.pkg.classname",
location=@Location(value=Kind.RETURN)
)
void exit(@ProbeMethodName(fqn=true) String probeMethod, @Duration long duration) {
BTraceUtils.print("Exit:" );
BTraceUtils.println(BTraceUtils.timestamp() );
BTraceUtils.println(probeMethod);
}
}这将在控制台上分发数据。我怎么能把结果写到文件里呢?Btrace不允许创建新对象。
(明显的解决办法是重定向到一个文件。另一种选择是使用VisualVM btrace插件-然后输出到visualVM窗口。请注意,确保它可以处理非常大的输出500Mb左右。)
谢谢
发布于 2010-12-06 16:52:07
您可以使用BTrace代理(http://kenai.com/projects/btrace/pages/UserGuide#btrace-agent)启动应用程序。然后,您可以为代理指定scriptOutputFile参数,调用、println、等生成的所有输出都将进入指定的文件,而不是标准输出。
发布于 2010-11-10 17:58:17
不,BTrace无法记录到文件,因为它需要尽可能轻量级,以便跟踪结果不会受到自己的日志记录的影响。您将不得不改为重定向到日志文件。
发布于 2013-01-13 17:35:35
您可以将控制台输出写入日志文件,如下所示:
Process p = Runtime.getRuntime().exec("cmd /c " + command);
StringBuffer output = new StringBuffer("");
if (p != null) {
BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
String buf = "";
try {
int count = 0;
while ((buf = is.readLine()) != null) {
output.append(buf);
output.append(System.getProperty("line.separator"));
if(++count % flushLineNumber == 0){
FileUtils.writeStringToFile(file, output.toString(), true);
output.setLength(0);
}
}
is.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}命令是brtrace.....我已经在我的个人项目中有了这个函数。
https://stackoverflow.com/questions/4142845
复制相似问题