log4j as a static class ?  
Author Message
Matteo





PostPosted: 2008-6-4 17:30:00 Top

java-programmer, log4j as a static class ? Hello all,
every example I found for log4j tells you to declare a final static
variable to hold the reference to log4j Logger for each of your classes
-by means of Logger.getLogger(Myclass.class)

What about my approach?


package mypackage;

import org.apache.log4j.Logger;

public final class Log {
public static org.apache.log4j.Logger log =
Logger.getLogger(Log.class);

private Log(){
}
}

Then, from within any class I need to log something, I simply call
Log.log.myloglevel("something");


Is this wrong?


Thanks,
regards


Matteo


 
Lew





PostPosted: 2008-6-4 20:49:00 Top

java-programmer >> log4j as a static class ? Matteo wrote:
> Hello all,
> every example I found for log4j tells you to declare a final static
> variable to hold the reference to log4j Logger for each of your classes
> -by means of Logger.getLogger(Myclass.class)

I wish the books wouldn't illustrate class names in such wrong ways.

> What about my approach?
>
>
> package mypackage;
>
> import org.apache.log4j.Logger;
>
> public final class Log {
> public static org.apache.log4j.Logger log =
> Logger.getLogger(Log.class);
>
> private Log(){
> }
> }
>
> Then, from within any class I need to log something, I simply call
> Log.log.myloglevel("something");
>
>
> Is this wrong?

Yes.

The logger is supposed to tell you in what class the error occurs. Your
logger will think every error happened in Log, and you'll not know where
really it occurred.

I prefer to declare the logger as an instance variable.

public class Foo
{
private final Logger logger = Logger.getLogger( Foo.class );
...
}

--
Lew
 
Matteo





PostPosted: 2008-6-4 21:00:00 Top

java-programmer >> log4j as a static class ? Lew wrote:
> Matteo wrote:
>> Hello all,
>> every example I found for log4j tells you to declare a final static
>> variable to hold the reference to log4j Logger for each of your
>> classes -by means of Logger.getLogger(Myclass.class)
>
> I wish the books wouldn't illustrate class names in such wrong ways.

What is wrong with the class name in my example? (maybe Myclass should
have read MyClass ? in this case, sorry it was my mistake in typing)

>> Is this wrong?
>
> Yes.
>
> The logger is supposed to tell you in what class the error occurs. Your
> logger will think every error happened in Log, and you'll not know where
> really it occurred.
>

And that's why I posted this message (and why I'm a bit confused).
This is an extract from my log4j.properties file:

log4j.appender.file.layout.ConversionPattern=%d{DATE} %5p %l - %m%n

With this Pattern setting (the %l parameter), I can see the
caller/method name along with the line number where the log was called,
and I save myself from copy-pasting the Logger declaration in every class.

What do you think of this method then?

Thanks again,
Matteo

 
 
Lew





PostPosted: 2008-6-4 21:16:00 Top

java-programmer >> log4j as a static class ? Matteo wrote:
>>> classes -by means of Logger.getLogger(Myclass.class)

Lew wrote:
>> I wish the books wouldn't illustrate class names in such wrong ways.

Matteo wrote:
> What is wrong with the class name in my example? (maybe Myclass should
> have read MyClass ? in this case, sorry it was my mistake in typing)

The presence of the word "class" as part of the class name. It makes sense
for examples, so I don't fault you of course, but the books need to send a
better-practice message in case their readers imprint on what they do.

Matteo wrote:
>>> Is this wrong? [wrapping all logger calls through Log.logger]

Lew wrote:
>> Yes.
>>
>> The logger is supposed to tell you in what class the error occurs.
>> Your logger will think every error happened in Log, and you'll not
>> know where really it occurred.

Matteo wrote:
> And that's why I posted this message (and why I'm a bit confused).
> This is an extract from my log4j.properties file:
>
> log4j.appender.file.layout.ConversionPattern=%d{DATE} %5p %l - %m%n
>
> With this Pattern setting (the %l parameter), I can see the
> caller/method name along with the line number where the log was called,
> and I save myself from copy-pasting the Logger declaration in every class.
>
> What do you think of this method then?

That's fine, but it's not related to your first question.

--
Lew
 
 
Lew





PostPosted: 2008-6-4 21:19:00 Top

java-programmer >> log4j as a static class ? Matteo wrote:
>> And that's why I posted this message (and why I'm a bit confused).
>> This is an extract from my log4j.properties file:
>>
>> log4j.appender.file.layout.ConversionPattern=%d{DATE} %5p %l - %m%n
>>
>> With this Pattern setting (the %l parameter), I can see the
>> caller/method name along with the line number where the log was
>> called, and I save myself from copy-pasting the Logger declaration in
>> every class.
>>
>> What do you think of this method then?

Lew wrote:
> That's fine, but it's not related to your first question.

To elaborate:

Your message will still have some information that you need, but the logger
itself won't. That deprives you of the power to tune loggers at runtime.

The logger naming strategy is an important part of making logs useful. If you
make all loggers have the same name, you lose the power of that strategy.

--
Lew
 
 
Real Gagnon





PostPosted: 2008-6-5 11:04:00 Top

java-programmer >> log4j as a static class ? Matteo <email***@***.com> wrote in news:g263k5$oq0$email***@***.com:

> And that's why I posted this message (and why I'm a bit confused).
> This is an extract from my log4j.properties file:
>
> log4j.appender.file.layout.ConversionPattern=%d{DATE} %5p %l - %m%n
>
> With this Pattern setting (the %l parameter), I can see the
> caller/method name along with the line number where the log was
> called, and I save myself from copy-pasting the Logger declaration in
> every class.
>
> What do you think of this method then?

According to the Log4J javadoc :

"The location information can be very useful. However, it's generation is
extremely slow. It's use should be avoided unless execution speed is not
an issue."

http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayou
t.html

Bye.
--
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html