logging setup best practices

230 views Asked by At

how do you go about structuring logging in your java projects? I have used log4j2, logback etc, however this is not a question about libraries but about setup and best practices.

I have tried:

  1. in filter

but it does not have enough detail. For example i cannot log method calls, input/output values in methods etc. Also it does not feel good when i need detailed logs of user activities.

  1. in each class

In each class, a static field for the logger, then in methods where needed i log what i need to. This way i have all the detail i want, but it's a lot pf work and it feels like the code is littered with log stuff.

What do you use in your projects?

Thanks

1

There are 1 answers

0
Tr1gZer0 On

I generally go with a similar approach to that of using static loggers in Class, but with a twist. Generally, I use only one static logger and a collection of Aspects. When a method requires to be logged, and usually it's input/output that needs to be logged, I simply use an @Around aspect. This keeps your code clean and separates the concerns of business logic with the concerns of the programmer (logging).

Here's a great place to start: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html