Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging a methods's name and parameters

Tags:

java

logging

I've been working with a company that, in this current project, has to implement a policy of writing lots of trace logging in code (JAVA) that has already been completed for some time.

I am trying to avoid changing every single method just to write a logger.log('desired values') like code line on all of them. it's just too counter-productive.

Is there a generic way to log a method name, the parameters that it received, without changing much of the code? I've been researching annotations and stuff like that but there are a lot of methods with different parameters so i haven't been able to come up with a good solution.

EDIT 1: The project is being developed on eclipse. I'm doing some changes in a portal using Liferay and JBoss.

EDIT 2: I've followed a solution given to me here and used interceptors. The only change i had to do to the existing methods was to add an annotation to them, which was quite acceptable. For more info search in this link: http://docs.oracle.com/javaee/6/tutorial/doc/gkeed.html

like image 776
Rafael El Bundas Fernandez Avatar asked Oct 30 '12 10:10

Rafael El Bundas Fernandez


People also ask

How do you log a list in Java?

You need to implement toString() method for your custom type to get expected output. List<Dog> dogList = new ArrayList<Dog>(); Now, if you want to print this List in LogCat properly then you need to implement toString() method in Dog class. Now, you will get proper result if you call list.

How do you log a string in Java?

The info() method of a Logger class is used to Log an INFO message. This method is used to forward logs to all the registered output Handler objects.

What is logging in Java examples?

Java Logging FormattersSimpleFormatter: This formatter generates text messages with basic information. ConsoleHandler uses this formatter class to print log messages to console. XMLFormatter: This formatter generates XML message for the log, FileHandler uses XMLFormatter as a default formatter.


3 Answers

The best way is to use AOP and Java annotations. I would recommend to use @Loggable annotation and an AspectJ aspect from jcabi-aspects (I'm a developer):

@Loggable(Loggable.DEBUG)
public String load(URL url) {
  return url.openConnection().getContent();
}
like image 93
yegor256 Avatar answered Sep 29 '22 07:09

yegor256


Go to Window->Preferences, select Java->Editor->Templates, create a new template named "logmeth" with Pattern i.e.:

if(logger.isDebug())logger.debug("${exception_variable_name} ${return_type} "+getClass().getName()+"${enclosing_method}(${enclosing_method_arguments})"+String.format("***",${enclosing_method_arguments}));

and press OK.

In java Editor write logmeth and press Strg+space+space and Enter and Eclipse will write i.e.:

if(logger.isDebug())logger.debug("e boolean hasFuture(man, woman)"
            + String.format("***", man, woman));

Eclipse is so cool.

like image 37
Grim Avatar answered Sep 29 '22 06:09

Grim


You can use interceptors http://docs.oracle.com/javaee/6/tutorial/doc/gkeed.html to intercept calls to public methods without any code changes, it is impossible to use this technique with non-public methods though.

like image 27
Germann Arlington Avatar answered Sep 29 '22 06:09

Germann Arlington