How can write logs to a file in btrace?

半世苍凉 提交于 2019-12-23 12:57:21

问题


I have following btrace script. I would like to record entry and exit of functions in a specific class.

..
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);
    }

}

This gives outout on the console. How could I write the result to a file? Btrace does not allow to create new objects.

(Obvious work around is redirect to a file. Another choice is to use VisualVM btrace plugin - the output then goes to visualVM window. Note sure if it an handle very large output 500Mb or so.)

Thanks


回答1:


You can start your application with BTrace agent (http://kenai.com/projects/btrace/pages/UserGuide#btrace-agent). Then you can specify scriptOutputFile argument to the agent and all output generated by calls to println etc. will go to the specified file instead of the stdout.




回答2:


No, BTrace cannot log to file because it needs to be as lightweight as possible so that tracing results are not affected by its own logging. You will have to redirect to a log file instead.




回答3:


You can write the console output into a log file like this:

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);
    }
}

The command is brtrace.....I already have this function in my personal project.



来源:https://stackoverflow.com/questions/4142845/how-can-write-logs-to-a-file-in-btrace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!