Nuno Furtado
Nuno Furtado

Reputation: 4568

Whats the overhead of creating a Log4j Logger

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.

Upvotes: 12

Views: 3996

Answers (2)

Bhushan Bhangale
Bhushan Bhangale

Reputation: 10987

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.

Upvotes: 9

Jon Skeet
Jon Skeet

Reputation: 1500495

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.

Upvotes: 24

Related Questions