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.
A Logger object is used to log messages for a specific system or application component. Loggers are normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component, such as java.net or javax.
Log4j allows logged messages to contain format strings that reference external information through the Java Naming and Directory Interface (JNDI). This allows information to be remotely retrieved across a variety of protocols, including the Lightweight Directory Access Protocol (LDAP).
log4j has three main components: loggers: Responsible for capturing logging information. appenders: Responsible for publishing logging information to various preferred destinations. layouts: Responsible for formatting logging information in different styles.
The configuration takes advantage of Log4j's ability to select a pattern based upon attributes of the log event. In this case %C, the class name pattern, is used when the CLASS Marker is present, and %c, the logger name is used when the CLASS marker is not present.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With