You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by Chris Westin <ch...@gmail.com> on 2015/03/05 01:06:25 UTC

Having trouble creating a logger for a generic class?

In some files I've been working in, I just noticed that the loggers aren't
static. (This is a different topic than the previous one re making them
private).

I set out to make them private final static, and discovered the reason:
they're generics. There's no way to create class literals for them. That
is, you can't do this:

public class Foo<T> {
  private final static org.slf4j.Logger logger =
org.slf4j.LoggerFactory.getLogger(Foo.class);

Foo<T>.class doesn't work either. Searches reveal that there's no way to
create a class literal
for a generic. However, we are certainly not the only ones struggling with
this, and someone out
there came up with this solution that does work:

public class Foo<T> {
  private final static org.slf4j.Logger logger =
org.slf4j.LoggerFactory.getLogger(new Object()
{}.getClass().getEnclosingClass());

I'll start using this whenever I see the old pattern in a generic class
(which was to not declare the logger static, and to use this.getClass()). I
figure it's better to have some throwaway objects once at class load time
rather than having every instance have it's own (identical) logger.

Chris

Re: Having trouble creating a logger for a generic class?

Posted by Matt Burgess <ma...@gmail.com>.
This might be because of a "hidden" feature of the Java language, generic types are preserved in anonymous classes...

Sent from my iPhone

> On Mar 4, 2015, at 7:41 PM, Aditya <ad...@apache.org> wrote:
> 
> Nice.
> 
> On Wed, Mar 4, 2015 at 4:06 PM, Chris Westin <ch...@gmail.com>
> wrote:
> 
>> In some files I've been working in, I just noticed that the loggers aren't
>> static. (This is a different topic than the previous one re making them
>> private).
>> 
>> I set out to make them private final static, and discovered the reason:
>> they're generics. There's no way to create class literals for them. That
>> is, you can't do this:
>> 
>> public class Foo<T> {
>>  private final static org.slf4j.Logger logger =
>> org.slf4j.LoggerFactory.getLogger(Foo.class);
>> 
>> Foo<T>.class doesn't work either. Searches reveal that there's no way to
>> create a class literal
>> for a generic. However, we are certainly not the only ones struggling with
>> this, and someone out
>> there came up with this solution that does work:
>> 
>> public class Foo<T> {
>>  private final static org.slf4j.Logger logger =
>> org.slf4j.LoggerFactory.getLogger(new Object()
>> {}.getClass().getEnclosingClass());
>> 
>> I'll start using this whenever I see the old pattern in a generic class
>> (which was to not declare the logger static, and to use this.getClass()). I
>> figure it's better to have some throwaway objects once at class load time
>> rather than having every instance have it's own (identical) logger.
>> 
>> Chris
>> 

Re: Having trouble creating a logger for a generic class?

Posted by Aditya <ad...@apache.org>.
Nice.

On Wed, Mar 4, 2015 at 4:06 PM, Chris Westin <ch...@gmail.com>
wrote:

> In some files I've been working in, I just noticed that the loggers aren't
> static. (This is a different topic than the previous one re making them
> private).
>
> I set out to make them private final static, and discovered the reason:
> they're generics. There's no way to create class literals for them. That
> is, you can't do this:
>
> public class Foo<T> {
>   private final static org.slf4j.Logger logger =
> org.slf4j.LoggerFactory.getLogger(Foo.class);
>
> Foo<T>.class doesn't work either. Searches reveal that there's no way to
> create a class literal
> for a generic. However, we are certainly not the only ones struggling with
> this, and someone out
> there came up with this solution that does work:
>
> public class Foo<T> {
>   private final static org.slf4j.Logger logger =
> org.slf4j.LoggerFactory.getLogger(new Object()
> {}.getClass().getEnclosingClass());
>
> I'll start using this whenever I see the old pattern in a generic class
> (which was to not declare the logger static, and to use this.getClass()). I
> figure it's better to have some throwaway objects once at class load time
> rather than having every instance have it's own (identical) logger.
>
> Chris
>