Java Logging – Is It Still Good Practice to Log Parameters and Returns?

javaloggingspring

Just a context, I am currently making a base code for a Java Spring Boot Application.

I am currently setting up the logging of the application and I stumbled upong this url and found it helpful. Although it is a little bit outdated.

Questions

  1. In the url, 7) Log method arguments and return values, is this still relevant today? Should I manually do it in my methods?

  2. In an MVC application, should I do it in all layers? (Controllers, Services, and DAOs)?

Best Answer

I don't think that article is implying that every method should should be logged that way, it's just saying when you're logging make sure you capture the context in which the log occurred.

For example, if you have a log which says only "User cannot be found", if you're actually trying to understand the scenario which lead to that log then you probably need to know the user ID at least. Typically function arguments have the most significant affect on the output of a function so that's the most important context to capture, but class or global variables might also be just as important depending on the situation.

In the case of a Spring web application, I definitely wouldn't be putting logging in every single function, but I would be logging where failures are likely. For example, if you make a call to an external service.

The article itself states:

Of course, you must be reasonable but every method that: accesses external system (including database), blocks, waits, etc. should be considered.

Although, some of this logging might already be provided by your ORM or RDBMS so I wouldn't go adding logging to every single database call either.

The article also states:

You should consider DEBUG or TRACE levels as best suited for these types of logs.

Which typically aren't logged in production, so you might consider it an alternative to temporary print/echo statements during development. Again, only where appropriate.