This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Log4j Wrapper Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Log4j Wrapper" Watch "Log4j Wrapper" New topic
Author

Log4j Wrapper

Arockia Raj
Ranch Hand

Joined: Dec 08, 2006
Posts: 37
Hi,
We are creating log4j wrapper for our application as follows.Will this create any threading issue..
public class LoggingUtil {
static Logger logger = Logger.getLogger(LoggingUtil.class);
public static void debug(Class<?> classNm,Object message)
{
if(logger.isDebugEnabled()){
logger.log(classNm.getName(), Level.DEBUG, message,null);}
}

}

Thanks and Regards
A.Raj
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

It's thread safe, because it has no state. Any class that has no state would be fundamentally thread safe.

On a side note, why would you want to create such a wrapper, it does not offer any benefit over the usual logging API!


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Niraj Jha
Ranch Hand

Joined: Feb 20, 2013
Posts: 63

@Arockia
Why do you think it will create any threding issue?
Arockia Raj
Ranch Hand

Joined: Dec 08, 2006
Posts: 37
Saifuddin Merchant wrote:It's thread safe, because it has no state. Any class that has no state would be fundamentally thread safe.

On a side note, why would you want to create such a wrapper, it does not offer any benefit over the usual logging API!



Thanks for the reply...The reason for this wrapper class is,we dont want to have 3rd libraray jars(com.apache.log4j) inside all over application codes and prefer to restrict to one wrapper class..so that in future if we decide to change log4j with any async implementation then the impact is only for the wrapper calss
Arockia Raj
Ranch Hand

Joined: Dec 08, 2006
Posts: 37
Niraj Jha wrote:@Arockia
Why do you think it will create any threding issue?

@Niraj :Thanks for the reply,the reason is this class is having static instance Logger variable..hence need to sure it will be thread safe..
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
the reason is this class is having static instance Logger variable..hence need to sure it will be thread safe..

The Log4J docs say "Calling the getLogger method with the same name will always return a reference to the exact same logger object." so it doesn't matter if you have a static reference to the logger or get a new reference each time one of your wrapper methods are called, for a given logger name you will always use the same logger object.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
The Logging package is an ultra-thin bridge between different logging implementations. A library that uses the commons-logging API can be used with any logging implementation at runtime. Commons-logging comes with support for a number of popular logging implementations, and writing adapters for others is a reasonably simple task.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Log4j Wrapper
 
Similar Threads
Configure two log4j loggers in one application
Using Log4j efficiently
Conditional Logging
Access HttpSession from static methods
log4j vs system.out.Println()