I would like to know which is the best choice to implement loggers in Java.
Currently I have used dependency inversion as the following example:
@Controller
@RequestMapping("/");
public class HomeController
{
private final LoggerImpl logger;
@Autowired
public HomeController(LoggerImpl logger){
this.logger = logger;
}
@GetMapping
public ModelAndView home(){
this.logger.log(new LogBean("Ação"));
return new ModelAndView("index");
}
}
Next is the interface:
public interface Loggable
{
void log(LogBean logBean);
}
And its implementation:
@Component
public class LoggerImpl implements Loggable
{
private final LoggerRepository repository;
@Autowired
public LoggerImpl(LoggerRepository repository){
this.repository = repository;
}
public void log(LogBean logBean){
...
this.repository.save(logBean);
...
}
}
The way it works, but ... I find this model very repetitive. I would like someone to be able to give me a glimpse of what can be done or improved.