Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中以编程方式记录传递给方法的参数_Java_Logging - Fatal编程技术网

如何在Java中以编程方式记录传递给方法的参数

如何在Java中以编程方式记录传递给方法的参数,java,logging,Java,Logging,如何在运行时记录传递给方法的参数?是否有任何Java库可以对此或任何异常进行监视?我认为您可以注册MBean,然后只有您能够使用JMX进行检查 链接:您可以使用javassist的或在运行时更改以打印参数: 使用转换器(使用新的类加载器): 产出: ----- calling: test.Test$MyApp.main([Ljava.lang.String;@145e044) Inside: main method ----- calling: test.Test$MyApp.Test$MyA

如何在运行时记录传递给方法的参数?是否有任何Java库可以对此或任何异常进行监视?

我认为您可以注册
MBean
,然后只有您能够使用JMX进行检查

链接:

您可以使用javassist的或在运行时更改以打印参数:


使用
转换器
(使用新的
类加载器
): 产出:

----- calling: test.Test$MyApp.main([Ljava.lang.String;@145e044)
Inside: main method
----- calling: test.Test$MyApp.Test$MyApp()
Inside: MyApp constructor
----- calling: test.Test$MyApp.method(Hello World!, 4711)
Inside: MyApp method

使用
ProxyFactory

你应该尝试使用AOP。下面是一个或多或少能满足您需求的示例:

您说的
监控值是什么意思?
我不太清楚您在这里问什么。是否要检查方法的有效输入?以及如何处理糟糕的输入?@Kshitij我的意思是,我想在将值传递给正在运行的程序的方法后立即将其存储在文件中。@JimmyGustafsson假设我们在Sum.java类中有一个add方法(int a,int b)。例如,现在执行这个。在运行时,用户或其他一些方法会向该方法输入数据。我想记录这些输入值。ProxyFactory和转换器链接无效。@macawm,固定链接
public static void main(String[] args) throws Throwable {
    ClassPool cp = ClassPool.getDefault();
    Loader cl = new Loader(cp);
    cl.addTranslator(cp, new PrintArgumentsTranslator());
    cl.run("test.Test$MyApp", args);  // or whatever class you want to start with
}

public class MyApp {

    public MyApp() {
        System.out.println("Inside: MyApp constructor");
    }

    public static void main(String[] args) {
        System.out.println("Inside: main method");
        new MyApp().method("Hello World!", 4711);
    }

    public void method(String string, int i) {
        System.out.println("Inside: MyApp method");
    }
}
----- calling: test.Test$MyApp.main([Ljava.lang.String;@145e044)
Inside: main method
----- calling: test.Test$MyApp.Test$MyApp()
Inside: MyApp constructor
----- calling: test.Test$MyApp.method(Hello World!, 4711)
Inside: MyApp method
public class Test {

    public String method(String string, int integer) {
        return String.format("%s %d", string, integer);
    }

    public static void main(String[] args) throws Exception {

        ProxyFactory f = new ProxyFactory();
        f.setSuperclass(Test.class);

        Class<?> c = f.createClass();
        MethodHandler mi = new MethodHandler() {
            public Object invoke(
                    Object self, Method m, Method proceed, Object[] args)
                throws Throwable {

                System.out.printf("Method %s called with %s%n", 
                                  m.getName(), Arrays.toString(args));

                // call the original method
                return proceed.invoke(self, args);
            }
        };

        Test foo = (Test) c.newInstance();
        ((Proxy) foo).setHandler(mi);
        foo.method("Hello", 4711);
    }
}
Method method called with [Hello, 4711]