Log4J wrapper: looses location information.
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Peter_Friese
Posted On:   Monday, May 13, 2002 11:56 PM

Hi all, I am currently trying to write a wrapper for Log4J for our framework. The reason I write a wrapper is that we want to be flexible and be able to switch logging frameworks if we need to. I used a simple wrapper approach to encapsulate Log4J, and everything works fine, except for one thing: Log4J does not display the original location information. To help you understand the problem, here is the source for the wrapper: package com.foo.logging; public class FooLogger { private Logger logger; public FooLogger(Class clazz) { logger = Logger.getLogger(clazz); } public void debug(Object message) {    More>>

Hi all,

I am currently trying to write a wrapper for Log4J for our framework. The reason I write a wrapper is that we want to be flexible and be able to switch logging frameworks if we need to.


I used a simple wrapper approach to encapsulate Log4J, and everything works fine, except for one thing:


  • Log4J does not display the original location information.

To help you understand the problem, here is the source for the wrapper:
			
package com.foo.logging;

public class FooLogger {

private Logger logger;

public FooLogger(Class clazz) {
logger = Logger.getLogger(clazz);
}

public void debug(Object message) {
logger.debug(message);
}

public void debug(Object message, Throwable t) {
logger.debug(message, t);
}

// ... and so on


When using the logger like this:
			
public class FirstTry {

static JEFLogger logger = new JEFLogger(FirstTry.class);

public static void main(String args[]) {
PropertyConfigurator.configure(args[0]);
logger.debug("A debug message.");
}

...the log entries do NOT contain the name of the originator method ( FirstTry.main in this case), but the name of the wrapper method ( FooLogger.debug ).

Does anybody have any good ideas how to circumvent this behaviour?


Rgds,

Peter

   <<Less

Re: Log4J wrapper: looses location information.

Posted By:   Anonymous  
Posted On:   Tuesday, May 28, 2002 09:34 AM

Hello,
I had the same problem.

By searching in the Log4J sources or javadoc, you can find the following method in the category class :


public
void log(String callerFQCN, Priority level, Object message, Throwable t)


that is "intended to be invoked by wrapper classes"...


Log4J is not so bad ;-)

Re: Log4J wrapper: looses location information.

Posted By:   Christopher_Koenigsberg  
Posted On:   Tuesday, May 14, 2002 07:10 AM

In each method that might need to call the logger, to be completely reliable/portable in making sure the originating class name is always there in the log message, I have a standard pattern where I have a local variable, final Class mylogname="myclassname.mymethodname".



When calling logger I always prepend the "mylogname" onto the detail message string that is to be logged.

About | Sitemap | Contact