Log4J: Strategies for creating Logger instances

后端 未结 10 622
不思量自难忘°
不思量自难忘° 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条回答
  •  忘掉有多难
    2020-11-30 21:48

    Common convention is "a logger pr class and use the class name as its name". This is good advice.

    My personal experience is that this logger variable should NOT be declared static but an instance variable which is retrieved for each new. This allows the logging framework to treat two calls differently depending on where they come from. A static variable is the same for ALL instances of that class (in that class loader).

    Also you should learn all the possibilities with your logging backend of choice. You may have possibilities you did not expect possible.

提交回复
热议问题