Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the overhead of creating a Log4j Logger

Tags:

java

log4j

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.

like image 519
Nuno Furtado Avatar asked Apr 21 '09 08:04

Nuno Furtado


People also ask

What is the purpose of creating a logger object?

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.

How does Log4j logging work?

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).

What is a logger in Log4j?

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.

Does logger use Log4j?

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.


2 Answers

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.

like image 56
Jon Skeet Avatar answered Nov 06 '22 15:11

Jon Skeet


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.

like image 22
Bhushan Bhangale Avatar answered Nov 06 '22 15:11

Bhushan Bhangale