drill-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Aditya <...@apache.org>
Subject Re: Having trouble creating a logger for a generic class?
Date Thu, 05 Mar 2015 00:41:53 GMT
Nice.

On Wed, Mar 4, 2015 at 4:06 PM, Chris Westin <chriswestin42@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
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message