how to use log4j with multiple classes?

后端 未结 5 1927
无人及你
无人及你 2020-12-14 18:06

I\'m currently writing a big project in java, with numerous classes, some classes are quiet small, that simply represent objects with only few methods. I have a logger set i

5条回答
  •  既然无缘
    2020-12-14 18:31

    I recently found out this solution.

    Use @Log4j annotation with all the classes in which you wish using logger.

    Benefits:

    1. Easy to maintain, easy to track down.
    2. Only one object of the logger is created which will be used through out.

    How to do this?

    Pre-req: Install Lombok Plugin in your editor, add lombok dependency in your project.

    Now for the definition part:

    @Log4j2
    public class TestClass {
        TestClass() {
            log.info("Default Constructor"); 
            //With the help of lombok plugin, you'll be able to see the suggestions
        }
        
    }
    

提交回复
热议问题