简体   繁体   中英

convenient logging of method calls?

I'm looking for a way to automate an arduous mechanical procedure that I perform too often: implementing trace logging on a method .

Consider the following fictitious methods.

private void createBaz(String key, String databaseConfigKey, String query) {
    this.queries.put(key, query);
    this.databases.put(key, Database.forKey(databaseConfigKey));
}

private void createFrob(String key, String hostname) {
    this.frobs.put(key, query);
}

Now, I'll add some useful logging.

private void createBaz(String key, String databaseConfigKey, String query) {
    this.log.trace("createBaz(" + key + ", " + databaseConfigKey + ", " + query + ")");
    this.queries.put(key, query);
    this.databases.put(key, Database.forKey(databaseConfigKey));
}

private void createFrob(String key, String hostname) {
    this.log.trace("createFrob(" + key + ", " + hostname + ")");
    this.frobs.put(key, query);
}

Note the similarities of the two log.trace calls.

I've typed out a lot of log lines like that, and I'm tired of it. I'm looking for something like this:

private void createBaz(String key, String databaseConfigKey, String query) {
    doLogTrace();
    this.queries.put(key, query);
    this.databases.put(key, Database.forKey(databaseConfigKey));
}

private void createFrob(String key, String hostname) {
    doLogTrace();
    this.frobs.put(key, query);
}

I might need help refining my question!

It doesn't matter to me if doLogTrace(); happens to be a longer string like doLogTrace(this,System.foo(),#$^#$&^$#); . It could even be multiple lines, so long as it's the SAME string wherever I use it. Then I could just put that string in a keyboard macro.

Things I've considered

  • AspectJ Not for this project.
  • IDE magic. I could probably write a macro for emacs that could jump to the top of the method, copy the name and parameter list, drop one line down, paste it and automagically edit it into log.trace("<methodName>(" + <method1> + ["," + <methodN>] +")"); ... But, normally my .java files are only open in Eclipse... :/

It sounds like you want Aspect-Oriented Programming

Here's a good overview of doing AOP with Guice

From this post you can see how you can get the name of the currently executing method. Getting the name of the current executing method

Having that, you also got your each method parameters, so create a new method that will take the rest of parameters ( int... will allow you to enter any number of ints, you could use Object)

For example:

private void logTrace(Object ... args) {
    String methodName = stacktrace.methodName;
    String logmsg = methodName + "(";
    foreach(arg : args){
        logmsg+= arg + ", ";
    }
    logmsg+= ")";
    log(logmsg);
}

You could use Java's Proxy class to add some before / after code for each method. The benefit is you can avoid modifying your original code.

If you're looking for more of a quick and dirty solution, you could use Thread.currentThread().getStackTrace() and just display the one relevant frame (it won't get the parameter values though)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM