AspectJ for Logger on File

时间:2015-05-28 14:12:02

标签: java logging aspectj logfile

我正在尝试使用AspectJ创建日志文件, 输出应写入文件 日志文件具有特定语法:

START

"THREAD_ID";"METHOD_NAME";"begin_or_end METHOD";"instant time"

STOP

代码不起作用:(

你能帮帮我吗?

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.aspectj.lang.Signature;

aspect Trace{

    private static File salva;
    private static FileWriter fw;
    pointcut traceMethods() : (execution(* *(..))&& !cflow(within(Trace)));

    before(): traceMethods(){
        salva = new File(".\trace.csv");
        try {
            fw = new FileWriter(salva,true);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Signature sig = thisJoinPointStaticPart.getSignature();
        String line =""+ thisJoinPointStaticPart.getSourceLocation().getLine();
        String sourceName = thisJoinPointStaticPart.getSourceLocation().getWithinType().getCanonicalName();
        Logger.getLogger("Tracing").log(
                Level.INFO, 
                "Call from "
                    +  sourceName
                    +" line " +
                    line
                    +" to " +sig.getDeclaringTypeName() + "." + sig.getName()

        );
        writeFile(sig);
        try {
            fw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void writeFile(Signature sig){
        long temp = System.currentTimeMillis();
        //inizio esecuzione traccia:
        try {
            fw.write("START\n");
            fw.write("S02034;");
            fw.write(sig.getDeclaringTypeName()+"."+sig.getName()+";"+temp+";"+"a\n");
            fw.write("STOP\n");
            fw.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


}

0 个答案:

没有答案