Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make logging components visible across whole Spring Boot application

I'm running Spring Boot and I want to add logging to my application. All examples have the following for the main Application entrypoint:

    private static final Logger log = LoggerFactory.getLogger(Application.class);

However, I want the same log variable visible (as Singleton) across all my application components (services, controllers, etc.). How would I do that? Thanks!

like image 621
user1340582 Avatar asked Oct 21 '25 00:10

user1340582


1 Answers

You can use springs IoC container to achieve this.

just configure a bean like this in a @Configuration cass

@Bean
public Logger log() {
   return LoggerFactory.getLogger(AnyClassYouWant.class);
}

and inject it with @Autowired in your class

class WhatEver {
   @Autowired
   Logger log;

   //...
}
like image 109
David Steiman Avatar answered Oct 22 '25 19:10

David Steiman