Creating Logger in base class

W

WJ

I'm creating a new project and want to create some base classes to provide
common functions to all classes extending it. One thing I'd like to do it
have the LogFactory available in every class.

So I'd want something like this:

public abstract class BaseClass {
protected static final Log log = LogFactory.getLog(this.class);
}

public class Class1 extends BaseClass {

//I have reference to the logger from the super class
log.debug("some statement);
}

But, as you know, I can't use this.class in the .getLog function in the Base
class. Is there a way to do this? I want the debug statement to show that
the debug came from Class1, not the BaseClass.

Thanks!
 
P

Paul Lutus

WJ said:
I'm creating a new project and want to create some base classes to provide
common functions to all classes extending it. One thing I'd like to do it
have the LogFactory available in every class.

So I'd want something like this:

public abstract class BaseClass {
protected static final Log log = LogFactory.getLog(this.class);
}

public class Class1 extends BaseClass {

//I have reference to the logger from the super class
log.debug("some statement);
}

But, as you know, I can't use this.class in the .getLog function in the
Base
class. Is there a way to do this? I want the debug statement to show
that the debug came from Class1, not the BaseClass.

log.debug(this, "debug message");

In other words, pass a reference to the originating class instance.
 
W

WJ

Paul Lutus said:
log.debug(this, "debug message");

In other words, pass a reference to the originating class instance.

The debug method does not seem to take params Object, String. It does take
Object, Throwable or just Object.

So I can do this:

log.debug(this, new Exception("debug message"));

But I can't do this:

log.debug(this, "debug message");

I'm using JDK 1.4.2
 
P

Paul Lutus

WJ wrote:

/ ...
The debug method does not seem to take params Object, String. It does
take Object, Throwable or just Object.

So I can do this:

log.debug(this, new Exception("debug message"));

But I can't do this:

log.debug(this, "debug message");

I'm using JDK 1.4.2

You need to tell us what you cannot do, what the problem is. I thought you
were writing the logging class, in which case you could control its
properties.
 
W

WJ

I am using the JDK 1.4 logging functionality. The typical syntax I use for
this is to declare a logger at the top of the class, like this:

public class MyClass1 {
protected static final Log log = LogFactory.getLog(MyClass1.class);
 
F

Filip Larsen

I am using the JDK 1.4 logging functionality. The typical syntax I use for
this is to declare a logger at the top of the class, like this:

public class MyClass1 {
protected static final Log log = LogFactory.getLog(MyClass1.class);
.
}
[...]
What I want is to be able to say log.debug(this, "some message"), so that on
the console, or other log file,
it will correctly show the classname actually throwing the log
message.

I see two way you can do it:

Make the log instance non-static, i.e. create one log for each instance
of the base class and call LogFactory with getLog(this). If you dont
have too many classes and if LogFactory can reuse log objects for the
same source this should work nicely. I use this pattern often with the
standard java Logger classes.

Or, you can keep log static but then make it private and let each
subclass create their own private static instance from the factory using
their own class. However, methods calling log will then report which
class they appear in rather than to which instance class they belong.
Depending on what you want that might actually be an advantage.


Regards,
 
F

Filip Larsen

I wrote
Make the log instance non-static, i.e. create one log for each instance
of the base class and call LogFactory with getLog(this).

That should be getLog(this.getClass()) of course, sorry.


Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,047
Members
48,769
Latest member
Clifft

Latest Threads

Top