Whats the overhead of creating a Log4j Logger

风流意气都作罢 提交于 2019-12-09 14:47:45

问题


I have some webservices in my application and i want to log them to diferent files, based on the webservice name. For that i am creating loggers with

myLogger = Logger.getLogger(logKey);

I am wondering if i should cache these loggers to avoid creating them for every call, or can i ignore the overhead.


回答1:


Loggers are already cached by log4j using the default log repository (Hierarchy). In other words, it's just a hashtable lookup.

However, in my experience you tend to make the logger static, so it only ends up being called once per class anyway.




回答2:


This method Logger.getLogger(logKey) looks in logger cache for a logger with the name passed in logKey. If it doesn't exist it creates one. First call for a logger name, a Logger will be created but later calls will get it from cache so you don't need to handle this in your code.



来源:https://stackoverflow.com/questions/771675/whats-the-overhead-of-creating-a-log4j-logger

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!