Log4J: Strategies for creating Logger instances

后端 未结 10 620
不思量自难忘°
不思量自难忘° 2020-11-30 21:22

I decided to use Log4J logging framework for a new Java project. I am wondering what strategy should I use for creating/managing Logger instances and why?

10条回答
  •  猫巷女王i
    2020-11-30 21:54

    The strategy that is most used is to create a logger per class. If you create new threads give them a usefull name, so their logging is easily distinguishable.

    Creating loggers per class has the benefit of being able to switch on/off logging in the package structure of your classes:

    log4j.logger.org.apache = INFO
    log4j.logger.com.example = DEBUG
    log4j.logger.com.example.verbose = ERROR
    

    The above would set all apache library code to INFO level, switch logging from your own code to DEBUG level with the exception of the verbose package.

提交回复
热议问题